Unity Register For One Interface Multiple Object and Tell Unity Where to Inject them

In the example below you have an interface implemented twice and injected on demand into two different client classes, just as you request. The trick is to use named registrations. class Program { static void Main(string[] args) { IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo1>(“Foo1”); container.RegisterType<IFoo, Foo2>(“Foo2”); container.RegisterType<Client1>( new InjectionConstructor(new ResolvedParameter<IFoo>(“Foo1”))); container.RegisterType<Client2>( new InjectionConstructor(new ResolvedParameter<IFoo>(“Foo2”))); … 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 } });”