So I am working on a project where I get some data out of a database - there are two pieces of data that make this project nice, one I have the type (they call them events but it essentially just translates to .NET type I created) and then I have XML and I designed the objects so they just deserialize nicely. All this is wonderful, unit tested and all the classes and methods follow the single responsibility principle.
Where my architecture skills get fuzzy is when I am creating the factory to build the business logic to process the .NET objects I have created from the XML.
Basically this is what I have.
public class EventProcessorFactory : IEventProcessorFactory
{
private readonly List<IEventProcessor> _eventProcessors;
public EventProcessorFactory()
{
_eventProcessors = new List<IEventProcessor>();
}
public IEventProcessor GetProcessor(Type eventType)
{
var typeOfEventProcessor = GetProcessorFromEventType(eventType);
if (_eventProcessors.Any(x => x.GetType() == typeOfEventProcessor))
return _eventProcessors.Single(x => x.GetType() == typeOfEventProcessor);
var processor = BuildProcessorFromType(typeOfEventProcessor);
_eventProcessors.Add(processor);
return processor;
}
private static Type GetProcessorFromEventType(Type eventType)
{
if (eventType == typeof(EnrollmentEventType))
return typeof(EnrollmentEventProcessor);
if (eventType == typeof(ClaimantAccountInfoEventType))
return typeof(ClaimantAccountInfoEventProcessor);
if (eventType == typeof(PhoneUpdateEventType))
return typeof(PhoneUpdateEventProcessor);
if (eventType == typeof(AddressUpdateEventType))
return typeof(AddressUpdateEventProcessor);
if (eventType == typeof(ClientAccountInfoEventType))
return typeof(ClientAccountInfoEventProcessor);
return null;
}
private IEventProcessor BuildProcessorFromType(Type typeOfEventProcessor)
{
return ((IEventProcessor)Activator.CreateInstance(typeOfEventProcessor));
}
}
So that works but it seems pretty clunky. I have read some articles about using factories but either I didn't read the right one or I am not getting it. I have two problems with the code above.
1) If you add a new event you need to go modify it, I would the later developers to be able to just drop "MyCoolNewEventType" and "MyCoolNewEventProcessor" and not have to modify the method that matches up the event to the processor.
2) I right now when I create an instance am able to just call .CreateInstance(); That's fine cause I don't have any dependencies but the "event processors" are probably going to have dependencies at least on the data base. I am not 100% sure how to handle that, I don't want random calls to Container.Resolve().
If anyone can point in the right direction that would be tremendous.
What you're looking for is configurable factory. it can be done with dependency injection frameworks, or the easiest way - an external configuration (xml file comes to mind), which will store the list of ur possible implementations and will be loaded/modified on demand.
With this solution you will have something like
<Processors>
<Processor dllname='' classname=''>
....
</Processors>
and you will have to load it, by using reflection / any xml reading technique .net provides you with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With