如何将现有容器绑定到另一个容器?
我有从 UnityContainer
类继承的实例,并且想要创建额外的实例来“继承”前一个实例的所有注册?
我是否正确理解 AddExtension
方法执行了我所期望的操作?
public class MyUnityContainer : UnityContainer
{
public MyUnityContainer(MyUnityContainer containerParent)
{
if ( containerParent!=null )
this.AddExtention(containerParent);
}
public static void Test()
{
MyUnityContainer cont1 = new MyUnityContainer();
cont1.RegisterType<IA,A>();
MyUnityContainer cont2 = new MyUnityContainer(cont1);
IA a = cont2.Resolve<IA>();
}
这应该有效吗?如果没有,有什么方法可以实现这样的功能?
我可以使 MyUnityContainer
不继承自 UnityContainer
,但包含它(“has a”而不是“is a”),但我不想实现 我的
接口。MyUnityContainer
类中的 IUnityContainer
I have instance inherited from UnityContainer
class and want to create additional instance that will "inherit" all registrations of the previous one?
Am I correctly understand that AddExtension
method do what I expect?
public class MyUnityContainer : UnityContainer
{
public MyUnityContainer(MyUnityContainer containerParent)
{
if ( containerParent!=null )
this.AddExtention(containerParent);
}
public static void Test()
{
MyUnityContainer cont1 = new MyUnityContainer();
cont1.RegisterType<IA,A>();
MyUnityContainer cont2 = new MyUnityContainer(cont1);
IA a = cont2.Resolve<IA>();
}
Should this work? If not, what is a way to achieve such functionality?
I could make MyUnityContainer
not inherited from UnityContainer
, but containing it ("has a" instead of "is a"), but I don't want to implement IUnityContainer
interface in my MyUnityContainer
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不是扩展的作用。扩展向容器添加新功能或注册,而不是建立继承关系。
你想要的是一个子容器。您调用parent.CreateChildContainer,这应该会执行您想要的操作 - 只不过子级将是UnityContainer,而不是MyUnityContainer。
MyUnityContainer 是做什么的?也许有一种方法可以让它在没有子类的情况下工作?
No, that's not what extensions do. Extensions add new features or registrations to the container, not set up inheritance relationships.
What you want is a child container. You call parent.CreateChildContainer, and that should do what you want - except that the child will be a UnityContainer, not MyUnityContainer.
What does MyUnityContainer do? Perhaps there's a way to make it work without the subclass?