Hello, I'm using structuremap to register a service in the controller and i need to register repositories in services too. How I'll do that since they are 2 different projects and need the exact same IOC registration. Actually I'm using It on the presentation layer and injecting services in the controller. I need to know a good pratice way to make, with the same IOC container, injection in the both projects.
views:
162answers:
2Put the IOC container, and any shared service implementations, into a third project (class library) that is referenced by both of the other two.
I tend to keep a StructureMap registry for each of my projects so they can handle their own configuration. My main project then adds references to each of its dependencies. In my main I usually have a bootstrapper class which is responsible for creating and configuring the container. (I avoid the static ObjectFactory when possible.)
public class ApplicationBootstraper
{
public IContainer Container { get; set; }
public ApplicationBootstraper()
{
Container = new Container(x=>
{
x.AddRegistry<SettingsRegistry>();
x.AddRegistry<ProjectRegistry>();
//more registries go here or application specific configuration.
});
}
}
Regarding having a project take a dependency on another project that is StructureMap aware. Version 2.5.4 added the ability to include another registry in your current registry.
For example I have a "Common" project which I need to take a dependency on I can do the following:
public class ProjectRegistry : Registry
{
public ProjectRegistry()
{
IncludeRegistry<CommonsRegistry>();
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
}
}
Hope this helps.