views:

52

answers:

2

I've got dependency injection working with Unity and it runs nicely but I want to reduce the amount of cruft in the app.config as it is getting huge.

This is an issue because the customer routinely 'cleans up' the config by removing comments etc and we end up with a few hundred lines of nasty XML without explanation.

So, is there a way to;

  1. Reduce the amount of clutter by using defaults?
  2. Move the configuration out to another .config file that will help isolate it?

I understand .Net 4.0 has some good stuff for reducing .config files, will this help Unity config though?

Thanks in advance,

Ryan

+1  A: 

From my point of view you have two options:

  • Move to another config file.
  • Write the IoC stuff in code, I like this way more because it is more readable. You can have a static container variable that you create through code: in the normal case by default or with mocks in unit tests.

RepositoryContainer.Contanier= ProductionRepositories(); RepositoryContainer.Container = MockRepositories();

Pablo Castilla
+2  A: 

Write the configuration in code instead. Unless you explicitly need to be able to vary the mappings from abstract to concrete types after compilation, using the config file only makes your system more brittle.

Unity doesn't have a lot of convention-based features, but you can always write some yourself. In code, that would allow you to simply scan a particular assembly for all public types that end with, say... "Repository"... and register them.

Both StructureMap and Windsor have features that makes this a lot easier, but in any case configuration in config should be left for the scenarios where you truly need to be able to swap out dependencies after compilation.

In most cases, this is only a small subset of the total amount of services registered with the container, if any at all.

Mark Seemann