24

I am working on a project where the Unity framework is used as the IoC container. My question relates to injecting an optional dependency (in this case a logger) into several classes using property- or setter injection.

I do not want to clutter the constructors of all my classes with these optional dependencies, but I cannot find a good way to handle this in Unity. The way you would do this is, according to the MSDN documentation, by adding an attribute to the property:

private ILogger logger; 

[Dependency]
public ILogger Logger
{
get { return logger; }
  set { logger = value; }
}

I find this very ugly. In StructureMap one could do the following to set all properties of a given type:

SetAllProperties(policy => policy.OfType<ILog>());

Does anyone know if it is possible to do something similar in Unity?

Edit:

Kim Major suggests using this approach which can also be achieved through code.

I would be interested in examples of how to do this automatically for all matching properties.

8 Answers 8

19

I don't like those attributes also

You can do all using the Configure method of the unity container:

First register the type

unityContainer.RegisterType<MyInterface,MyImpl>(
                     new ContainerControlledLifetimeManager());

If you have multiple constructors you'll have to do this so Unity invokes the parameterless constructor (if none set Unity will go for the fattest one)

unityContainer.Configure<InjectedMembers>()
                            .ConfigureInjectionFor<MyImpl>(
                                new InjectionConstructor()); 

Setting property dependency

unityContainer.Configure<InjectedMembers>()
                    .ConfigureInjectionFor<MyImpl>(
                         new InjectionProperty(
                             "SomePropertyName",
                                new ResolvedParameter<MyOtherInterface>()));

Configuring method dependency

unityContainer.Configure<InjectedMembers>()
                    .ConfigureInjectionFor<MyImpl>(
                        new InjectionMethod(
                            "SomeMethodName",
                            new ResolvedParameter<YetAnotherInterface>()));
16

I am also not a big fan of using attributes but I also don't like the .Configure<InjectedMembers>() method because you're bound to a specific property name and specific value. The way I've found that gives you the most flexibility is to create your own builder strategy.

I created this simple class that iterates the properties of an object being built up and sets its property values if the type of that property has been registered with the unity container.

public class PropertyInjectionBuilderStrategy:BuilderStrategy
{
    private readonly IUnityContainer _unityContainer;

    public PropertyInjectionBuilderStrategy(IUnityContainer unityContainer)
    {
        _unityContainer = unityContainer;
    }

    public override void PreBuildUp(IBuilderContext context)
    {
        if(!context.BuildKey.Type.FullName.StartsWith("Microsoft.Practices"))
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(context.BuildKey.Type);

            foreach (PropertyDescriptor property in properties)
            {
                if(_unityContainer.IsRegistered(property.PropertyType)
                   && property.GetValue(context.Existing) == null)
                {
                    property.SetValue(context.Existing,_unityContainer.Resolve(property.PropertyType));
                }
            }
        }
    }
}

You register your BuilderStrategy by creating a UnityContainerExtension. Here is an example:

public class TestAppUnityContainerExtension:UnityContainerExtension
{
    protected override void Initialize()
    {
        Context.Strategies.Add(new PropertyInjectionBuilderStrategy(Container), UnityBuildStage.Initialization);
    }
}

That gets registered with the Unity container as such:

IUnityContainer container = new UnityContainer();
container.AddNewExtension<TestAppUnityContainerExtension>();

Hope this helps,
Matthew

4
  • That's a nice solution! I works pretty well for what I wanted to do. I use MEF for injection usually, but in a console application I want to use unity, now I don't have to mark all properties with unity imports! Commented Sep 22, 2011 at 14:08
  • 1
    It is a nicely structured solution. My only concern would be the performance. The "IsRegistered" function will actually try to resolve every single public property. A better way might be to pass a list of types to resolve into the strategy object constructor and only try to resolve the opted-in types.
    – Dmitry S.
    Commented Jul 19, 2012 at 13:56
  • 1
    This doesn't seem to work well in conjunction with PerResolveLifetimeManager since you are beginning a new "resolve" with each injected property. Any ideas for a workaround here? Commented Aug 13, 2013 at 19:22
  • This also doesn't work with HierarchicalLifetimeManager because child containers aren't available in PropertyInjectionBuilderStrategy :( Commented Feb 14, 2017 at 17:14
7

The original example you posted does look very cumbersome, but you can use auto-implemented properties like this to help clean that code up:

[Dependency]
public ILogger Logger { get; set; }
0
5

With Unity 2.1 this would work as well:

var container = new UnityContainer()
            .RegisterType<ILogger, Logger>()
            .RegisterType<ISomeInterface, SomeImplementaion>(
                                new InjectionProperty("Logger", new ResolvedParameter<ILogger>()));

The injected property of SomeImplementaion class is just

public ILogger Logger { get; set; }
4

You could try this:

this code in MyClass

[InjectionMethod]
public void Initialize(
                 [Dependency] ILogger logger

and then calling it by:

unitycontainer.BuildUp<MyClass>(new MyClass());

unity will then call Initialize method with the dependency from the container and then u can save it in a private variable in MyClass or something...

3

The following walkthrough shows one way of doing it through configuration. You can of course wire it through code as well. http://aardvarkblogs.wordpress.com/unity-container-tutorials/10-setter-injection/

2
  • Thank you for the tip. I guess I can use this as a fallback solution, but as far as I can see this requires you to create a setup for each class that uses the logger. I wondered if it was a way to just specify this once, so that all classes using the logger would be automatically injected. Commented Feb 5, 2009 at 8:51
  • I haven't tried the following so I'm not sure. I think you should be able to register all your classes. Then reflect over the registered classes and add the setter injection registration if the type has an ILogger Setter.
    – Kim Major
    Commented Feb 5, 2009 at 9:06
1

Have a look at the UnityConfiguration project for convention-based configuration. It works pretty well, though i have an issue while looking up multiple implementations using ResolveAll<IType>(). See this Question.

container.RegisterInstance(typeof (ILogger), LoggerService.GetLogger());
container.Configure(c => c.Scan(scan =>
                    {
                        scan.AssembliesInBaseDirectory(a => a.FullName.StartsWith("My.Company")); // Filter out everthing that are not from my assemblies
                        scan.InternalTypes();
                        scan.With<SetAllPropertiesConvention>().OfType<ILogger>();
                    }));
0

You can inject your class with unity container.

for example if you have a class "MyClass" and you have dependency on two interface "IExample1" and "IExample2" and you don't want to define parameter for them in constructor , then follow below steps

Step 1. Register both the interfaces in unity container

IUnityContainer container = new UnityContainer();
container.RegisterType<IExample1,Impl1>();
container.RegisterType<IExample2,Impl2>();
//resolve class MyClass
var myClass = container.Resolve<MyClass>();


Step 2. Your class should look like this

public class MyClass
{
     IExample1 ex1;
     IExample2 ex2
     public MyClass(IUnityContainer container)
     {
        /* This unity container is same unity container that we used in step 
           1 to register and resolve classes. So you can use this container to resolve
           all the dependencies. */
       ex1= container.Resolve<IExample1>(); // will give you object of Impl1
       ex2= container.Resolve<IExample2>(); // will give you object of Impl2
     }
}

Step 3. In this way you can resolve any number of dependencies without defining them in constructor.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.