ryudice
ryudice

Reputation: 37366

Where to put IoC container configuration?

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.

Upvotes: 3

Views: 797

Answers (4)

mattk
mattk

Reputation: 1365

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

Upvotes: 2

Praveen
Praveen

Reputation: 772

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.

Upvotes: 0

Rob
Rob

Reputation: 5603

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.

Upvotes: 0

Lance Fisher
Lance Fisher

Reputation: 25813

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.

Upvotes: 2

Related Questions