tags:

views:

81

answers:

2

I'm working on an ASP.Net website along with a supporting Class Library for my Business Logic, Data Access code, etc.

I'm EXTREMELY new and unfamiliar with the Unity Framework and Dependency Injection as a whole. However, I've managed to get it working by following the source code for the ASP.NET 3.5 Portal Starter Kit on codeplex. But herein lies the problem:

The Class Library is setup with Unity and several of my classes have [Dependency] attributes on their properties (I'm exclusively using property setter injections for this). However, the Global.asax is telling Unity how to handle the injections....in the Class Library.

Is this best practice or should the Class Library be handle it's own injections so that I can re-use the library with other websites, webapps or applications? If that is indeed the case, where would the injection code go in this instance?

I'm not sure how clear the question is. Please let me know if I need to explain more.

+1  A: 

Though not familiar with Unity (StructureMap user) The final mappings should live in the consuming application. You can have the dll you are using define those mappings, but you also want to be able to override them when needed. Like say you need an instance of IFoo, and you have one mapped in your Class Library, but you've added a new one to use that just lives in the website. Having the mappings defined in the site allows you to keep things loosely coupled, or else why are you using a DI container?

Kevin
Thanks, that seems to make sense and I would agree if I didn't go that route, then having a DI container wouldn't really make much sense.Much appreciated.
WesleyJohnson
+1  A: 

Personally I try and code things to facilitate an IOC container but never will try and force an IOC container into a project.

My solution breakdown goes roughly: (Each one of these are projects).

  • Project.Domain
  • Project.Persistence.Implementation
  • Project.Services.Implementation
  • Project.DIInjectionRegistration
  • Project.ASPNetMVCFrontEnd (I use MVC, but it doesn't matter).

I try to maintain strict boundaries about projects references. The actual frontend project cannot contain any *.Implementation projects directly. (The *.implementation projects contain the actual implementations of the interfaces in domain in this case). So the ASPNetMVCFrontEnd has references to the Domain and the DIInjectionWhatever and to my DI container.

In the Project.DIInjectionWhatever I tie all the pieces together. So this project has all the references to the implementations and to the DI framework. It contains the code that does the registering of components. Autofac lets me breakdown component registration easily, so that's why I took this approach.

In the example here I don't have any references to the container in my implementation projects. There's nothing wrong with it, and if your implementation requires it, then go ahead.

Min
Thanks for the additional info. I'm a little lost on what all you said (again, I'm super new to all this), but I'm going to keep reading it until I get it. :)
WesleyJohnson
Well don't worry, someone will eventually write the 300 page manual on IoC containers that'll explain it to everyone.
Min