What are the advantages over config file?

Oct 19, 2009 at 4:16 AM
Edited Oct 21, 2009 at 1:30 AM

This sounds pretty cool and is probably worth a look for no other reason than learning more about predicates and lambda expressions. I'm sure I'm just missing it, but what is the advantage over just specifying the configuration in the application's config file other than it may prevent a client from messing up the application's configuration? If you want container autoregistration why not just use MEF (http://mef.codeplex.com/) that will be included in .NET 4.0?


Oct 31, 2009 at 4:38 AM
Edited Oct 31, 2009 at 4:39 AM

Hi mousedoc,

Thanks for your feedback.

Unity out-of-box application config (as well as Unity out-of-box fluent registration API) allows you to configure types registration, but not auto registration. It means that you can register types in config by listing each and every type you want to register there. Auto registration on the other hand allows you to only specify rules about what types you want to register. For example, using auto registration you can register 50 different View types of you project implementing IView interface by single code line, while with normal registration (using fluent API or config) you'll have to go through all types and specify them.

Considering MEF, AFAIK from my brief overview it is a bit different from IoC container and does different job. Also, AFAIK, MEF registers parts only based on attributes decoration that may not be always acceptable. Unity Auto Registration allows you to not only use attributes, but specify any rules for types you want to register (for example type name or namespace rules, certain interface or base class implementation etc).

Sep 7, 2010 at 4:13 AM

I inferred two different questions in your post:

1) why use code configuration rather than xml configuration files for your DI container?

It depends. Code configuration of DI containers benefits from intellisense and compiler checking to ensure you do not mis-type a type.  But if you need late binding, then xml files are the right choice. Mark Seeman outlines these tradeoffs nicely in his book http://www.manning.com/seemann/

2) what does AutoRegistration add to what is already in Unity? 

Automatic registration, which means registering by specifying assemblies and rules, is merely a convenience for developers working with complex applications. It is awfully nice to not have to add a new entry every time you add a new interface or class. If one has integration tests that test the configuration, as I am prone to do, then you can catch it during automated testing. But having just one fluent line gather it all up is really, really cool to me. It makes my configuration so simple.  These little tricks reduce friction and allow me to stay focussed on the story rather than the underlying mechanics.

And on teams of people where developers are of varying levels of skill, a novice can add a new interface to an existing well-known library that is already included, and it will get configured without drama.