如何使用 StructureMap 对类型进行分组?

发布于 2024-11-17 10:19:32 字数 1113 浏览 1 评论 0原文

我有一些类

public interface IProvider
{
    void Load(int id);
}
public class Provider1 : IProvider
{
     public void Load(int id) {} 
}
public class Provider2 : IProvider
{
    public void Load(int id) {}
} 
public interface IService
{
    void Load(int id);
}
public class Service : IService
{
    IProvider provider;
    public Service(IProvider provider) { this.provider=provider; }
    public void Load(int id) { provider.Load(id); }
}

,并且我像这样使用 StructureMap 注册了它们:

For<IService>().Use<Service>().Named("core"); 
For<IProvider>().Use<Provider1>().Named("core"); 
For<IService>().Use<Service>().Named("gen"); 
For<IProvider>().Use<Provider2>().Named("gen");

但是当我尝试通过

var service1 = ObjectFactory.GetNamedInstance<IService>("core"); 
var service2 = ObjectFactory.GetNamedInstance<IService>("gen");

在这两种情况下 IProvider 实例化到 Provider2 来获取服务时,为什么?

我预计第一种情况 IProvider 将实例化到 Provider1 对于 Provider2 的第二种情况

那么,我需要如何注册它们才能获得我想要的东西?

I have some classes

public interface IProvider
{
    void Load(int id);
}
public class Provider1 : IProvider
{
     public void Load(int id) {} 
}
public class Provider2 : IProvider
{
    public void Load(int id) {}
} 
public interface IService
{
    void Load(int id);
}
public class Service : IService
{
    IProvider provider;
    public Service(IProvider provider) { this.provider=provider; }
    public void Load(int id) { provider.Load(id); }
}

and I registered them with StructureMap like this:

For<IService>().Use<Service>().Named("core"); 
For<IProvider>().Use<Provider1>().Named("core"); 
For<IService>().Use<Service>().Named("gen"); 
For<IProvider>().Use<Provider2>().Named("gen");

but when I try to get Service by

var service1 = ObjectFactory.GetNamedInstance<IService>("core"); 
var service2 = ObjectFactory.GetNamedInstance<IService>("gen");

so in both cases IProvider instanciated to Provider2, why?

I expected for 1st case the IProvider will instanciate to Provider1
and for 2nd case to Provider2

So, how I need to register them in order to get what I want?

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

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

发布评论

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

评论(2

小帐篷 2024-11-24 10:19:32

您没有告诉结构图服务的不同实例具有哪些依赖关系。为了说明这一点,请使用 Ctor 方法指示创建实例时应传递哪些参数。

For<IProvider>().Add<Provider1>().Named("core");
For<IProvider>().Add<Provider2>().Named("gen");
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("core"))
    .Named("coreService");
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("gen"))
    .Named("genService");

请注意使用“添加”而不是“使用”。 add 表示我们正在添加一个新实例,use 表示该实例应该是默认的。因此,如果您希望能够将核心设置为默认值,您可以按如下方式注册:

For<IProvider>().Use<Provider1>();
For<IProvider>().Add<Provider2>().Named("gen");
For<IService>().Use<Service>();
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("gen"))
    .Named("genService");

并解析如下实例:

var score = ObjectFactory.GetInstance<IService>();
var sgen = ObjectFactory.GetNamedInstance<IService>("genService");

You're not telling structuremap what dependencies the different instances of Service are having. In order to tell, use the Ctor method that indicate what arguments should be passed when creating an instance.

For<IProvider>().Add<Provider1>().Named("core");
For<IProvider>().Add<Provider2>().Named("gen");
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("core"))
    .Named("coreService");
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("gen"))
    .Named("genService");

Note the use of Add instead of Use. The add indicates that we are adding a new instance, use indicates that a instance should be default. So if you want to be able to have core as default you can register as follows:

For<IProvider>().Use<Provider1>();
For<IProvider>().Add<Provider2>().Named("gen");
For<IService>().Use<Service>();
For<IService>().Add<Service>()
    .Ctor<IProvider>("provider").Is(c => c.GetInstance<IProvider>("gen"))
    .Named("genService");

and resolve the instances like this:

var score = ObjectFactory.GetInstance<IService>();
var sgen = ObjectFactory.GetNamedInstance<IService>("genService");
音栖息无 2024-11-24 10:19:32

您可以尝试告诉结构映射您想要 service1 的 IProvider 实例,名称如下:

For<IService>().Use<Service>().Named("core").Ctor<IProvider>().Is(y => y.GetInstance<IProvider>("core"));

You can try telling structuremap what instance of IProvider you want for service1 with the name like this:

For<IService>().Use<Service>().Named("core").Ctor<IProvider>().Is(y => y.GetInstance<IProvider>("core"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文