views:

245

answers:

3

Hi,

I want to isolate all my code from the IoC container library that I have chosen (Unity). To do so, I created an IContainer interface that exposes Register() and Resolve(). I created a class called UnityContainerAdapter that implements IContainer and that wraps the real container. So only the assembly where UnityContainerAdapter is defined knows about the Unity library.

I have a leak in my isolation thought. Unity searches for attributes on a type's members to know where to inject the dependencies. Most IoC libraries I have seen also support that. The problem I have is that I want to use that feature but I don’t want my classes to have a dependency on the Unity specific attribute.

Do you have any suggestions on how to resolve this issue?

Ideally I would create my own [Dependency] attribute and use that one in my code. But I would need to tell the real container the search for my attribute instead of its own.

+3  A: 

Check out the Common Service Locator project:

The Common Service Locator library contains a shared interface for service location which application and framework developers can reference. The library provides an abstraction over IoC containers and service locators. Using the library allows an application to indirectly access the capabilities without relying on hard references. The hope is that using this library, third-party applications and frameworks can begin to leverage IoC/Service Location without tying themselves down to a specific implementation.


Edit: This doesn't appear to solve your desire to use attribute-based declaration of dependency injection. You can either choose not to use it, or find a way to abstract the attributes to multiple injection libraries (like you mentioned).

That is the basic problem with declarative interfaces -- they are tied to a particular implementation.

Personally, I stick to constructor injection so I don't run into this issue.

Talljoe
Thanks for the link; very useful. They might end-up adding the attribute to use to declare dependencies on properties in that library. I'm going to keep an eye on that project.BTW, I want to use injection on properties because I have types for which I don't control the creation process. Another library creates and hands me the instance. I plan on calling IContainer.InjectDependencies(object target) on that instance.
Sly
A: 

Couldn´t you just setup your configuration without the attributes, in xml. That makes it a bit more "unclear" I know, personally I use a combination of xml and attributes, but at least it "solves" your dependency on unity thing.

Johan Leino
+2  A: 

I found the answer: Unity uses an extension to configure what they call "selector policies". To replace the attributes used by Unity, you just code your own version of the UnityDefaultStrategiesExtension class and register you own "selector policies" that use your own attributes.

See this post on the Unity codeplex site for details on how to do that.

I'm not sure that it's going to be easy to do the same if I switch to another IoC library but that solves my problem for now.

Sly