Unity Uniqueness

Mar 18, 2008 at 2:50 PM
Is there anything that sets Unity apart from other IoC containers for .NET, technically?
Mar 18, 2008 at 3:41 PM
I don't have the full story on that, but up to this point I think of it is as just an alternative to other tools out there, not a replacement.

One functional difference that I enjoy, however, is the ability for Unity to create items that are not in its container. This especially comes in real handy when you want to create controllers in the ASP.NET MVC Framework or Presenters when using Model-View-Presenter. With most other IoC Containers, you would need to add controllers and/or presenters to the container before you could create them, which means you would need to manually specify them in a config file or add them programmatically through some type of reflection discovery during startup. With Unity you don't need to do that. You can see a bit of this in Chris' Blog post, Using Unity and the ASP.NET MVC Preview 2. I talk about this with MVP in the following post: ASP.NET Model-View-Presenter and Unity.

The other big advantage I see is if you use Enterprise Library or the Software Factories. EntLib 4 will have an optional configuration mechanism for wiring the blocks with Unity, and I see the software factories also moving to Unity. This means you can have one IoC Container for all your dependency injection needs.

Hope it helps.

Regards,

Dave
Mar 18, 2008 at 11:26 PM
Variety is definitely the spice of life. Not sure about the uniqueness of the automatic item creation feature though - Autofac 1.1 takes a slightly different route but can manage it quite reasonably with the following:

builder.RegisterTypesAssignableTo<IController>();

There is a more generic predicate-based mechanism behind this to choose types based on attributes or whatever other markers you'd like to use. The 'create everything automatically' approach, if you desire it, could be achieved substituting IController for object above, but I personally feel that is a path to madness (perhaps within the context of Autofac's other features and not necessarily in the context of Unity's.)

I can't say for certain but I suspect both Windsor and StructureMap now support this functionality too (please correct me if I'm wrong.) The ASP.NET MVC Preview 1 made this quite attractive, but Preview 2 had detracted from the usefulness of this somewhat.

Agreed on the EntLib 4 point; you could equally validly say that about Windsor and its affinity to the Castle stack, or Spring.NET and its associated framework.

Unity's extension mechanism is very elegant - while other containers achieve similar aims, it is a piece of engineering that I think warrants inclusion in your list.

Best regards,

Nick
Mar 19, 2008 at 12:36 AM
Hi Nick,

I think you are talking about configuration functionality from various tools that go out and discover types and add them to the container, which is a bit different from creating types that are not registered in the container.

Unity will try to create any type you throw at it whether it is in the container or not, like

UnityContainer.Resolve<ProductsController>();

I thought this was fairly unique and very useful in MVC and MVP to avoid run-time reflection and xml configuration hassles, but perhaps not.

In terms of type discovery, I do like the elegant way you mention how Autofac 1.1 does it. StructureMap uses attributes that constitute Plug-In Families and will reflect upon assemblies for you and register them into the container. I have not seen anything like this in Windsor, but if there isn't it wouldn't be too difficult to create such an interpreter. If there isn't one and Ayende reads this, he will probably create one faster than writing a response.

Regards,

Dave
Mar 19, 2008 at 2:57 AM
> If there isn't one and Ayende reads this, he will probably create one faster than writing a response

LOL :) I wonder if it is the same Ayende that checks the news group as the one that writes the code, or if the whole thing is distributed across some kind of hive-mind...

The Autofac snippet I posted is equivalent to the Unity behaviour - it works dynamically rather than at configuration time to handle requests for unregistered types (the IoC equivalent of 'method_missing' if you follow Ruby metaprogramming hacks...) I think Unity's approach fairly novel, but the general technique for handling requests for unregistered types exists in other containers AFAIK.

I'd like to see some means of filtering types appear in Unity - Resolve<ProductsController>() makes sense to me, but Resolve<SqlConnection>() when this is unregistered doesn't seem valid (for example.) Might already be in there, I haven't had a tinker with the latest version (Chris Tavares is as quick as Ayende when it comes to these things it seems.)

Regards,

Nick
Mar 19, 2008 at 1:03 PM
object creation is just one minor core piece of the IoC containers written for .net. ie. Spring.NET and Windsor both offer much functionality through the container. Object pooling, AOP Alliance Interceptors, Transaction support, NHibernate support, Pub/Sub capability, Remoting support through configuration, Web service support through configuration, Mixin support, etc... Ability to wire these components to do these things once they are in the container is very important to me.

When I evaluate a IoC container, I'm looking to see what is brings to the table. I don't know how much of this above is supported by Unity ? Obviously it is young and needs time to mature. That isn't a knock, just a reality. I hope to see it grow and mature.


Mar 19, 2008 at 3:52 PM

nblumhardt wrote:
> If there isn't one and Ayende reads this, he will probably create one faster than writing a response

LOL :) I wonder if it is the same Ayende that checks the news group as the one that writes the code, or if the whole thing is distributed across some kind of hive-mind...



It is the same one.
Mar 19, 2008 at 3:53 PM

If there isn't one and Ayende reads this, he will probably create one faster than writing a response.



It is there for about two years. :-)
May 6, 2008 at 10:46 AM


DavidHayden wrote:
I don't have the full story on that, but up to this point I think of it is as just an alternative to other tools out there, not a replacement.

One functional difference that I enjoy, however, is the ability for Unity to create items that are not in its container. This especially comes in real handy when you want to create controllers in the ASP.NET MVC Framework or Presenters when using Model-View-Presenter. With most other IoC Containers, you would need to add controllers and/or presenters to the container before you could create them, which means you would need to manually specify them in a config file or add them programmatically through some type of reflection discovery during startup. With Unity you don't need to do that. You can see a bit of this in Chris' Blog post, Using Unity and the ASP.NET MVC Preview 2. I talk about this with MVP in the following post: ASP.NET Model-View-Presenter and Unity.

The other big advantage I see is if you use Enterprise Library or the Software Factories. EntLib 4 will have an optional configuration mechanism for wiring the blocks with Unity, and I see the software factories also moving to Unity. This means you can have one IoC Container for all your dependency injection needs.

Hope it helps.

Regards,

Dave



Hello Dave,

You gave some presentations on MVC/MVP using asp.net and UNITY, great presentations and code in your blog, I tried to get the code to work and failed. Do you have a link that you could share so that I could download the code.

TIA
Dr Y Arezki