Unity静态属性注入 [英] Unity Static Property Injection

查看:1191
本文介绍了Unity静态属性注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班,其中一个通过注册类型设置了容器和一个包含我希望注入静态属性。我的问题是物业从未被注射设置,所以当我调用一个方法就可以了,该属性始终为null。

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();
    }
}

如果我删除了依赖属性和ClassOne做简单

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)?

编辑:

感谢。我从ClassTwo去掉静态声明和ClassOne添加RegisterType并解决ClassTwo还增加InjectionProperty:

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

推荐答案

团结注入依赖上课的时候是通过统一解决。静态类不能被创建,因此团结不能注入依赖关系。

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

而不是有一个静态类的,用团结来解决伪单身类( ClassTwo的ContainerControlledLifetimeManager )。这样统一内喷射时会创建 ClassTwo 的iCLASS ClassTwo (解决通过量统一容器)和作为被配置为单,你总是有 ClassTwo 在应用程序的整个lifecicle相同instace。

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.

您必须通过统一解决ClassTwo

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.



当你需要ClassTwo:

And when you need ClassTwo:

Container.Resolve<InterfaceImplemetedByClassTwo>



具有ClassTwo的配置:

Having the config in ClassTwo:

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



但是,这不是一个很好的解决方案,我觉得你的问题是与DI的哲学思想。您需要从您的应用程序的顶层类级联依赖。解决一个明确的方式顶层类。 ( Container.Resolve )和依赖条件注射向下级联得益于统一的魔力。当2类(顶层或不)需要使用 ClassTwo 统一做肮脏的工作,如果你配置了相同的实例 ClassTwo ContainerControlledLifetimeManager

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.

这篇关于Unity静态属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆