How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

Another approach, thanks to a suggestion from @DarkSquirrel42, is to use an InjectionFactory. The downside is that the code still needs updating every time a new constructor parameter is added to something in the chain. The advantages are much easier to understand code, and only a single registration into the container. Func<IUnityContainer,object> createChain = container … Read more

Comparing Castle Windsor, Unity and StructureMap

See here and here for a pretty thorough technical comparison of several IoC containers, although somewhat outdated by now (they’re from before Windsor 2.0) However, I don’t think there are really any vital features that Windsor offers and other containers don’t. Windsor, StructureMap, Spring.NET have all been around for several years and have been used … Read more

With Unity how do I inject a named dependency into a constructor?

You can configure dependencies with or without names in the API, attributes, or via the config file. You didn’t mention XML above, so I’ll assume you’re using the API. To tell the container to resolve a named dependency, you’ll need to use an InjectionParameter object. For your ClientModel example, do this: container.RegisterType<IClientModel, ClientModel>( new InjectionConstructor( … Read more

Is there a good/proper way of solving the dependency injection loop problem in the ASP.NET MVC ContactsManager tutorial?

As a general consideration, circular dependencies indicate a design flaw – I think I can safely say this since you are not the original author of the code 🙂 I wouldn’t consider an Initialize method a good solution. Unless you are dealing with an add-in scenario (which you aren’t), Method Injection is not the right … Read more

When to use PerThreadLifetimeManager?

The Per Thread Lifetime is a very dangerous lifestyle and in general you should not use it in your application, especially web applications. This lifestyle should be considered dangerous, because it is very hard to predict what the actual lifespan of a thread is. When you create and start a thread using new Thread().Start(), you’ll … Read more

Can I pass constructor parameters to Unity’s Resolve() method?

As of today they have added this functionality: It’s in the latest drop here: http://unity.codeplex.com/SourceControl/changeset/view/33899 Discussion on it here: http://unity.codeplex.com/Thread/View.aspx?ThreadId=66434 Example: container.Resolve<IFoo>(new ParameterOverrides<Foo> { { “name”, “bar” }, { “address”, 42 } });”

Unity Inject dependencies into MVC filter class with parameters

As per the post Passive Attributes, the DI-friendly solution is to separate the AuthorizeAttribute into 2 parts: An attribute that contains no behavior to flag your controllers and action methods with. A DI-friendly class that implements IAuthorizationFilter and contains the desired behavior. For our purposes, we just inherit AuthorizeAttribute to take advantage of some of … Read more