I have a factory that processes a custom message format. Several different message sets, based on this format, are defined in the system. I am attempting to restructure the message processing code so that message sets “auto-register” with the factory. This will allow us to only included needed message sets in various parts of the system and also allow message sets to be added without effecting existing code.
The main factory class with registration is as follows.
public class MessageFactory
{
internal static List<FactoryListEntry> _seriesFactories = new List<FactoryListEntry>();
public static void RegisterSeriesFactory( long seriesId, ushort seriesVersion,
ISeriesFactory factory )
{
MessageFactory._seriesFactories.Add(
new FactoryListEntry( seriesId, seriesVersion, factory ));
}
}
The auto register class for a unique series looks like this.
public class Series1AutoRegistration
{
private static Series1AutoRegistration register = new Series1AutoRegistration();
private Series1AutoRegistration()
{
MessageFactory.RegisterSeriesFactory( SERIES_ID,
SERIES_VERSION,
new Series1Factory() );
}
}
In C++ I would have just made a global static Series1AutoRegistration variable. However, c# does not guarantee initialization of static member until just before it is used so the auto-registration does not happen. Does anyone have ideas on how I can accomplish this?
Your can either
This stackoverflow question answers how to use MEF to implement a class factory