Tutorial :Where to put IoC container configuration?



Question:

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.


Solution:1

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.


Solution:2

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


Solution:3

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.


Solution:4

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »