2

I have troubles consuming unity container in ASP.NET MVC3 application.

I have several project with interfaces and their implementation. All interface to concreete type binding is performed in the application startup method.

I have several problems with this aproach:

1) How to handle registration of types that are not dirrectly required by MVC application but by classes that it using (Repository uses ContextManager to retrieve context instance). If this class is located in assembly that is not used by mvc app, I will have to add reference to it.

2) How to share configured container? Should I create separate assembly with static class wich will store created by mvc app container?

3) What kind of unity container usage can possibly bring cross thread problems? How to register singletons so that they will be avaliable only in this thread (web server call) etc.

v00d00
  • 3,095
  • 3
  • 28
  • 43

1 Answers1

2
  1. You should explicitly reference all assemblies to your ASP.NET MVC application. It is the outermost layer in the onion architecture and it is allowed to know about inner layers. All assemblies must be in the bin folder anyways so the ASP.NET MVC application will know about them one way or another. Just externalize your DI framework configuration into a single place in your ASP.NET MVC application.
  2. See 1.
  3. Per thread storage could be dangerous in ASP.NET. Per HTTP Context is better.
Community
  • 1
  • 1
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • So how to share configured container? For example I want to resolve instance of an object inside one of the assemblies. Is the aproach with static class holding container will be fine? – v00d00 Aug 31 '11 at 18:16
  • @Dmitry Kushnier, you simply reference this assembly into your ASP.NET MVC application and then configure the container: http://blog.janjonas.net/2011-03-12/asp_net-mvc_3-dependency-injection-unity_2 – Darin Dimitrov Aug 31 '11 at 18:19
  • Ok I just realized that the whole instance resolution will be initiated by web server call and all required instances will be resolved from up to down. I was asking this because I wanted to use something like : static class { static method { return ServiceLocator.Resolve(); }} – v00d00 Aug 31 '11 at 18:30
  • @Dmitry Kushnier, I consider Service Locator as an anti pattern and prefer dependency injection. – Darin Dimitrov Aug 31 '11 at 18:32