I have a data processing application, it has several workflows that extract data and generate a report.
The workflows take a list of data extractors and a reporting service.
The workflow to run is determined at runtime.
I have configured the workflow creation using structure map, given a workflow key it will generate an instance of a workflow. I am new to structure map and here the first configuration I have got to work.
Does this look correct or have I missed something? Is there a better way to configure this?
Here is the registry class:
class WorkflowRegistry : Registry
{
public WorkflowRegistry()
{
var container = new Container(x =>
{
x.For<DossierExtraction.Library.Interfaces.IProgressReporter>().Use<ProgressReporter>();
x.For<IDossierService>().Use<DossierService>();
x.For<IReportingService>().Use<VendorReportingService>().Named("VendorReport");
x.For<IReportingService>().Use<ClientReportingService>().Named("ClientReport");
});
For<IWorkflow>().Add(x => new GenericWorkflow(
new List<IExtractionService>() { container.GetInstance<DossierExtractor>(), container.GetInstance<NativeExtractor>(), container.GetInstance<TranslationExtractor>() },
container.GetInstance<IReportingService>("VendorReport")
)).Named("VendorWorkflow");
For<IWorkflow>().Add(x => new GenericWorkflow(
new List<IExtractionService>() { container.GetInstance<TranslationExtractor>() },
container.GetInstance<IReportingService>("ClientReport")
)).Named("Clientflow");
}
}
Here is a snippet showing the usage of the registry class in my application.
ObjectFactory.Initialize(x =>
{
x.AddRegistry<WorkflowRegistry>();
});
var workflow = ObjectFactory.GetNamedInstance<IWorkflow>(workflowKey);
workflow.Run();
I would say that you have a couple of things that can be changed.
The new container in the registry is not needed, inside the Add methods for the Workflows, you can get the instance using your lambda (
x.GetInstance<Type>("name").The
Usemethod is registering a default for a specific instance. If you don’t need a default, useAddinstead (IReportingService).I generally try to avoid calling specific constructors inside the configuration, since it makes it harder to add new parameters to a contructor. Prefer the
Ctormethod instead. In this case with the Lists being passed and if this is the entire application it might be overkill with theCtor/Iscombo.Here is my suggestion: