I am using the Registry DSL example to configure structuremap. But doing this makes all of my registered types available in all layers of my application where I add a refernce to structure map. I dont want my business layer to know anything about my data access layer and vice versa. How do I get structuremap to only register specific types for each of my layers?
Here is the code in my global.asax file:
ObjectFactory.Initialize(x =>
{
x.AddRegistry<RegistryIOC>();
});
And here is my RegistryIOC class:
public class RegistryIOC : SMRegistry
{
public RegistryIOC()
{
For<IProfileService>.Use<ProfileService>();
For<IProctorService>().Use<ProctorService>();
//Business Logic Objects
For<IQual>().Use<Qual>();
For<ITest>().Use<Test>();
For<IBoldface>().Use<Boldface>();
For<ITrainingPlan>().Use<TrainingPlan>();
For<IUnit>().Use<Unit>();
//Data Transfer Objects
For<IGenericDTO>().Use<GenericDTO>();
For<IProfileDTO>().Use<ProfileDTO>();
For<IQualDTO>().Use<QualDTO>();
For<IPermissionDTO>().Use<PermissionDTO>();
//Repository Objects
For<IProctorRepository>().Use<ProctorRepository>();
For<IQualsRepository>().Use<QualsRepository>();
For<ITestRepository>().Use<TestRepository>();
For<IUnitRepository>().Use<UnitRepository>();
For<IUserRepository>().Use<UserRepository>();
}
}
Thanks for the help.
I am using reflection to accomplish this (and other) tasks. Let me show how that works.
The first thing to do is to define an interface that allows us to identify classes that perform initialization tasks:
Next, create one or more classes that implement this interface. These classes will be spread across all of your projects, which is another way of saying that you can put them “where they belong”.
The last piece of the puzzle is to find classes that implement the IConfigurationTask interface, create an instance of them and execute the Configure method. This is the purpose of the ConfigurationTaskRunner:
The code shown here uses a custom extension to iterate over all items in a list and execute an action for every item (the ForEach method). I am also using a reflection library to make the task of locating and instantiating the instances a one-liner (the CreateInstances method), but you could achieve the same using just plain reflection (as shown in the code below).
The final piece of the puzzle is to trigger the execution of the ConfigurationTaskRunner. For example, in a web application this would go into Application_Start in Global.asax:
I’ve also found it useful with a derived IPrioritizedConfigurationTask (that adds a Priority property) to allow proper ordering of the tasks before you execute them. This is not shown in the example code above, but is fairly trivial to add.
Hope this helps!