StructureMap单用途(A类实现两个接口) [英] StructureMap singleton usage (A class implementing two interface)

查看:129
本文介绍了StructureMap单用途(A类实现两个接口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public interface IInterface1
{
}

public interface IInterface2
{
}

public class MyClass : IInterface1, IInterface2
{
}

...

ObjectFactory.Initialize(x =>
{
    x.For<IInterface1>().Singleton().Use<MyClass>();
    x.For<IInterface2>().Singleton().Use<MyClass>();
});

var x = ObjectFactory.GetInstance<IInterface1>();
var y = ObjectFactory.GetInstance<IInterface2>();

我得到上述code两个不同的MyClass的实例。我怎样才能得到呢?

I get two different MyClass instances with the above code. How can I get one?

推荐答案

您可以使用转发&LT;,>()注册告诉StructureMap解决使用不同类型的分辨率的类型。这应该做你所期望的:

You can use the Forward<,>() registration to tell StructureMap to resolve a type using the resolution of a different type. This should do what you expect:

ObjectFactory.Initialize(x =>
{
    x.For<IInterface1>().Singleton().Use<MyClass>();
    x.Forward<IInterface1, IInterface2>();
});

这篇关于StructureMap单用途(A类实现两个接口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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