views:

77

answers:

4

Is it a good practice to create another project in your solution just to configure the container and for registries? I'm wondering where should I put the container configuration, I currently have all the code in my global asax but it's getting messy as I add more registries and more advanced injections.

+2  A: 

I usually create a Registry class in my web project where I tie everything together with StructureMap. See this: http://structuremap.github.com/structuremap/RegistryDSL.htm

If you have projects in your solution that have a lot of their own configuration, then you may want to give those projects their own registry, but don't actually register it until application_start in global.asax.

Part of IoC is waiting to tie everything together until you need to. That way it is more configurable. So if you can keep your registry in your website, you'll have the most flexibility.

Lance Fisher
A: 

We separate ours out into a set of classes, then instantiate the root class from Global.asax. For example:

  IPreferredContainerType container = new RootContainerFactory().Container;
  Application["Container"] = container;
  Container = container;

The root container can compose module-specific containers which allows you to easily mix and match. Just basic encapsulation.

Rob
A: 

I'd have each one of my projects have its own registry and the configuration (what Jeremy calls Bootstrapper) be in your main app (Win/Web/Console). That way, it's easy to swap your main app without touching any of the registries.

Praveen
A: 

This article uses Castle Windsor in examples but it's good advice applicable regardless of which container you use.

http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx

mattk