选择性地统一解决同一接口的依赖关系
我们使用 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
}
所以现在我们要选择性地解析上面两个类中的 IInterface
。 Class1
和 Class2
。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
Try this