Configure Unity Injection for all descendants of some base class

Jul 8, 2010 at 12:05 PM

How to configure Unity so that any class derived from some base class would go through injection pipeline defined for base class.

public abstract class Base
    public IDependency Dependency {get;set;}

public class Derived1: Base

public class Derived2: Base

container.RegisterType<Base>(new InjectionProperty("Dependency", new ResolvedParameter<IDependency>()));
var d1 = container.Resolve<Derived1>();

Thus, I need to register base class with Unity while resolve derived classes so that all injections specified for base class would be applied to derived classes.

Decorating base class property with DependencyAttribute is not allowed.


Jul 9, 2010 at 5:16 AM

Decorating base class property with the Dependency attribute is allowed.  I'm not sure why you said that, could you post the error you're getting?

Anyway, you should be able to do what you want by creating registrations for each derive class?

container.RegisterType<Base, Derived1>(new InjectionProperty("Dependency", new ResolvedParameter<IDependency>()));
container.RegisterType<Base, Derived2>("derived2", new InjectionProperty("Dependency", new ResolvedParameter<IDependency>()));
var d1 = container.Resolve<Base>(); //resolves Derived1
var d2 = container.Resolve<Base>("derived2"); //resolves Derived2


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Jul 9, 2010 at 6:12 AM

Oh, thanks for your attention.

Saying that "Decorating base class property with DependencyAttribute is not allowed" I mean my project restrictions, not an error in Unity! I'm just not allowed to modify base class.

Registering every descendant class is a kind of workaround. I wouldn't like to register every new descendant class which appears during development.

In fact, at the moment I'm using workaround you suggest:

            var types =
                from type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                where type != null
                where type.IsClass
                where type.IsSubclassOf(typeof(BaseClass))
                select type;

            foreach (var type in types)
                    , new InjectionProperty("DocumentFactory")
                    , new InjectionProperty("AuthorizationService")

However, I would prefer to develop some custom extension for Unity to achieve my initial goal.

Jul 9, 2010 at 3:10 PM

Hi there


I was wondering this case

public class Base
    Public Base(IDependency1,IDependency1)    {...}

public class Derived1:Base
    void SomeMethod1ThatUsesDpendency1(){}

public class Derived2:Base
    void void SomeMethod2ThatUsesDpendency2(){}

If it´s still possible to inject the base class constructor a and call the methods on derived class through the container(the methods only belong to the derived classes)?


Jul 16, 2010 at 5:47 AM

It is impossible to have 0-arguments constructor in descendant class while ancestor class takes arguments in constructor.

Jul 16, 2010 at 11:47 AM


You are right. The problem is that if a iave a business layer with 20 classes or so and i want to inject dependencies i have to copy and paste  the ctor in each and every one of them.

So i think that with properties like you have is the way to go.

Jul 16, 2010 at 11:57 AM


And it would be perfect to configure Unity so that any class derived from specified Base would automatically go through defined injection pipeline.

I will explain.

I need not to specify explicitly that Unity has to inject dependencies into constructor of my derived class. It just works.


container.registerType<IDependency, DependencyImplementation>();

Does anybody know what is the extensibility point in Unity where I could plug needed Build-up pipeline behaviour?


Jul 16, 2010 at 8:36 PM

You would need to replace the default ConstructorSelectorPolicy with one that implemented whatever you want your rules to be. That's easy to do, just replace the policy in the policy list. Implementing your rules, on the other hand, depends on how complicated those rules end up getting.


Jul 19, 2010 at 6:03 PM
Why do I need to replace the default ConstructorSelectorPolicy if I don't want to change constructor selection logics? What policy, strategy, build plan or other stuff should I replace or add to make Unity take base class registration into account while resolving derived class?
Jul 19, 2010 at 7:03 PM

Umm, you do want to change constructor selection logics, don't you? You want to change the default selected one to match the one specified for the base class, don't you? I don't understand why you think this is different?

The current defaults - longest constructor, no properties, no methods, are all handled by the default ConstructorSelectorPolicy, PropertySelectorPolicy, and MethodSelectorPolicy. If you want to change the default injection behavior, that's what you need to change.


Jul 20, 2010 at 3:26 AM

In my first post there are definitions of my base class along with my derived classes. As you see there is the only one constructor both in base class and in derived class - the default parameterless constructor. So, why would I need to change constructor selection logics if I have the only one constructor?

On the other side, probably PropertySelectorPolicy is the thing to replace since I want inherited properties registered for injection with the base class to be injected into derived class. Am I right? Thanks for your help forward.