Unity 1.2 内部类型的依赖注入

发布于 2024-09-05 04:59:35 字数 834 浏览 5 评论 0原文

我在一个库中有一个外观,它通过一个简单的界面公开一些复杂的功能。我的问题是如何为外观中使用的内部类型进行依赖注入。假设我的 C# 库代码如下 -

public class XYZfacade:IFacade
{
    [Dependency]
    internal IType1 type1
    {
        get;
        set;
    }
    [Dependency]
    internal IType2 type2
    {
        get;
        set;
    }
    public string SomeFunction()
    {
        return type1.someString();
    }
}

internal class TypeA
{
....
}
internal class TypeB
{
....
}

我的网站代码如下 -

 IUnityContainer container = new UnityContainer();
 container.RegisterType<IType1, TypeA>();
 container.RegisterType<IType2, TypeB>();
 container.RegisterType<IFacade, XYZFacade>();
 ...
 ...
 IFacade facade = container.Resolve<IFacade>();

这里facade.SomeFunction() 抛出异常,因为facade.type1 和facade.type2 为null。任何帮助表示赞赏。

I have a facade in a library that exposes some complex functionality through a simple interface. My question is how do I do dependency injection for the internal types used in the facade. Let's say my C# library code looks like -

public class XYZfacade:IFacade
{
    [Dependency]
    internal IType1 type1
    {
        get;
        set;
    }
    [Dependency]
    internal IType2 type2
    {
        get;
        set;
    }
    public string SomeFunction()
    {
        return type1.someString();
    }
}

internal class TypeA
{
....
}
internal class TypeB
{
....
}

And my website code is like -

 IUnityContainer container = new UnityContainer();
 container.RegisterType<IType1, TypeA>();
 container.RegisterType<IType2, TypeB>();
 container.RegisterType<IFacade, XYZFacade>();
 ...
 ...
 IFacade facade = container.Resolve<IFacade>();

Here facade.SomeFunction() throws an exception because facade.type1 and facade.type2 are null. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-09-12 04:59:36

不推荐注入内部类。

我将在程序集中创建一个公共工厂类,其中声明了内部实现,可用于实例化这些类型:

public class FactoryClass
{
   public IType1 FirstDependency
   {
     get
     {
       return new Type1();
     }
   }

   public IType2 SecondDependency
   {
     get
     {
       return new Type2();
     }
   }
 }

并且 XYZFacade 中的依赖项将与 FactoryClass 类一起使用:

public class XYZfacade:IFacade
{
   [Dependency]
   public FactoryClass Factory
   {
      get;
      set;
   }
}

如果要使其可测试,请为以下对象创建一个接口:工厂类。

Injecting internal classes is not a recommended practice.

I'd create a public factory class in the assembly which the internal implementations are declared which can be used to instantiate those types:

public class FactoryClass
{
   public IType1 FirstDependency
   {
     get
     {
       return new Type1();
     }
   }

   public IType2 SecondDependency
   {
     get
     {
       return new Type2();
     }
   }
 }

And the dependency in XYZFacade would be with the FactoryClass class:

public class XYZfacade:IFacade
{
   [Dependency]
   public FactoryClass Factory
   {
      get;
      set;
   }
}

If you want to make it testable create an interface for the FactoryClass.

苹果你个爱泡泡 2024-09-12 04:59:36

如果容器创建代码位于内部类型的程序集之外,则 Unity 无法查看和创建它们,因此无法注入依赖项。

If the container creation code is outside the assembly of the internal types, Unity can't see and create them and thus can't inject the dependecies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文