选择性地统一解决同一接口的依赖关系

发布于 2025-01-08 06:29:44 字数 961 浏览 0 评论 0原文

我们使用 Unity 框架进行依赖注入。我正在为同一个类注册两个具体实现。就像...

container.RegisterType<IInterface, MyFirstClass>("first");
container.RegisterType<IInterface, MySecondClass>("second");

现在很少有类具有上述接口的构造器注入..

 public class Class1
    {
       private IInterface _myClass
       public Class1 (IInterface myClass)
       {
         _myClass = myClass // // should be instance of MyFirstClass
       }
 //.. other code
    }

   public class Class2
    {
       private IInterface _myClass

       public Class2 (IInterface myClass)
       {
         _myClass = myClass // should be instance of MySecondClass
       }
 //.. other code
    }

所以现在我们要选择性地解析上面两个类中的 IInterfaceClass1Class2

Class1 应使用 MyFirstClass 注入,Class2 应使用 MyFirstClass 注入。这可以通过Unity框架实现吗?如果不支持,还有哪些其他 IoC 框架支持?

请提出您的意见。

We are using Unity framework for Dependency injection. I am registerring two concrete implementations for same class. like...

container.RegisterType<IInterface, MyFirstClass>("first");
container.RegisterType<IInterface, MySecondClass>("second");

Now there are few classes which has contructer injection for above interface..

 public class Class1
    {
       private IInterface _myClass
       public Class1 (IInterface myClass)
       {
         _myClass = myClass // // should be instance of MyFirstClass
       }
 //.. other code
    }

   public class Class2
    {
       private IInterface _myClass

       public Class2 (IInterface myClass)
       {
         _myClass = myClass // should be instance of MySecondClass
       }
 //.. other code
    }

So now we want to resolve IInterface selectivly in above two classes. Class1 and Class2.

Class1 should be injeceted with MyFirstClass and Class2 should be injected with MyFirstClass. Is this possible to achieve via Unity framework? If not which other IoC frameworks support this?

Please suggest your opinions.

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

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

发布评论

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

评论(1

影子是时光的心 2025-01-15 06:29:44

试试这个

var container = new UnityContainer();
container.RegisterType<IInterface, MyFirstClass>("first");
container.RegisterType<IInterface, MySecondClass>("second");
container.RegisterType<Class1>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "first")));
container.RegisterType<Class2>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "second")));
var class1 = container.Resolve<Class1>();
Assert.IsInstanceOfType(class1.MyClass, typeof(MyFirstClass));
var class2 = container.Resolve<Class2>();
Assert.IsInstanceOfType(class2.MyClass, typeof(MySecondClass));

public class MyFirstClass : IInterface
{
}

public class MySecondClass : IInterface
{
}

public interface IInterface
{
}

public class Class1
{
  public IInterface MyClass { get; set; }
  public Class1(IInterface myClass)
  {
    MyClass = myClass;
  }
}
public class Class2
{
  public IInterface MyClass { get; set; }
  public Class2(IInterface myClass)
  {
    MyClass = myClass;
  }
}

Try this

var container = new UnityContainer();
container.RegisterType<IInterface, MyFirstClass>("first");
container.RegisterType<IInterface, MySecondClass>("second");
container.RegisterType<Class1>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "first")));
container.RegisterType<Class2>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "second")));
var class1 = container.Resolve<Class1>();
Assert.IsInstanceOfType(class1.MyClass, typeof(MyFirstClass));
var class2 = container.Resolve<Class2>();
Assert.IsInstanceOfType(class2.MyClass, typeof(MySecondClass));

public class MyFirstClass : IInterface
{
}

public class MySecondClass : IInterface
{
}

public interface IInterface
{
}

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