Search code examples
c#unity-containercontainersdependency-propertiesproperty-injection

Unity Static Property Injection


I have two classes, one which sets up the container by registering types and one which contains a static property which I want to inject into. My issue is the property is never set by injection so when I call a method on it, the property is always null.

public class ClassOne
{
    public void Method()
    {
        Container.RegisterType<IClass, ClassImplOne>("ImplOne");
        Container.RegisterType<IClass, ClassImplTwo>("ImplTwo");
    }
}

public static class ClassTwo
{
    [Dependency]
    public static IClass SomeProperty { get; set; }

    public static void SomeOtherMethod()
    {
        SomeProperty.AnotherMethod();
    }
}

If I remove the Dependency attribute and in ClassOne do a simple

ClassTwo.SomeProperty = Container.Resolve<IClass>("ImplOne");

it works fine, but I want to know if it is possible to do this without explicitly assigning a value to the property (i.e. can the container inject through attributes)?

Edit:

Thanks. I have removed the static declaration from ClassTwo and in ClassOne added RegisterType and Resolve for ClassTwo and also added InjectionProperty:

Container.RegisterType<IClass, ClassImplOne>("ImplOne", new InjectionProperty("SomeProperty"));

but it still does not work :S


Solution

  • Unity inject dependencies when the class is resolved through Unity. A static class can not be created, so Unity can not inject dependencies.

    Instead of having a Static class, use Unity to resolve a pseudo-singleton class (ContainerControlledLifetimeManager) of ClassTwo. This way Unity injects IClass to ClassTwo when ClassTwo is created (resolved throug Unity container) and, as is configured as singleton, you always have the same instace of ClassTwo in the whole lifecicle of your application.

    You must resolve ClassTwo through Unity.

    Container.RegisterType<IClass, ClassImplOne>("ImplOne");
    Container.RegisterType<IClass, ClassImplTwo>("ImplTwo");
    Container.RegisterType<InterfaceImplemetedByClassTwo, ClassTwo>();
    
    //Simple example. Don't forget to use ContainerControlledLifetimeManager for ClassTwo to simulate sigleton.
    

    And when you need ClassTwo:

    Container.Resolve<InterfaceImplemetedByClassTwo>
    

    Having the config in ClassTwo:

    public class ClassTwo : InterfaceImplemetedByClassTwo
    {
        [Dependency("ImplOne")] //inject ClassImplOne
        public IClass SomeProperty { get; set; }
    

    BUT this is not a great solution, I think your problem is with the phylosophy of DI. You need to cascade dependencies from the top layer classes of your app. Resolve the top layer classes in a explicit way. (Container.Resolve) and dependecies injection cascade down thanks to the magic of Unity. When 2 classes (top layer or not) need to use the same instance of ClassTwo Unity do the dirty work if you configured ClassTwo with ContainerControlledLifetimeManager.

    In other words, you don't need static class, you inject the same instance of a class in other classes than need it.