如何将parent / child类作为constructmap参数传递给structmap? [英] How to pass parent/child class as constructor parameter with structuremap?

查看:294
本文介绍了如何将parent / child类作为constructmap参数传递给structmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口:

public interface IPricingFactorsRepository
{
    IList<LrfInputRates> GetLeaseRateFactorList(
        string programCode, 
        string countryCode, 
        string currencyCode,
        string leaseTerm);
}

得到以下派生/实现的类:

Got below derived/implemented class:

public class PricingFactorsRepository : IPricingFactorsRepository 
{
}

public class OverridePricingFactorsRepository : PricingFactorsRepository
{
}

此外,还有类接受接口作为构造函数参数:

Outside, there is such class that accept the interface as constructor parameter:

public class PricingHandler
{
    public PricingHandler(IPricingFactorRepository pricingFactorRepository)
    {
    }
}

但是使用结构图,似乎只能使用一个选项来处理它:

But with structuremap, it seems I can handle it with only one option:

x.For<IPricingFactorsRepository>().Use<PricingFactorsRepository>();

在某些情况下,我希望传入的参数是 PricingFactorsRepository ,有时候应该是 OverridePricingFactorsRepository

In some case, I would like the passed in parameter to be instances of PricingFactorsRepository, some times, it should be OverridePricingFactorsRepository.

推荐答案

使用命名实例,您可以根据输入创建不同的对象:

Using named instances you can create different objects based on the input:

        ObjectFactory.Initialize(conf =>
        {
            conf.For<IPricingFactorsRepository>().Use<PricingFactorsRepository>();
            conf.For<PricingHandler>().Use<PricingHandler>().Named("Default");
            conf.For<PricingHandler>().Add<PricingHandler>().Named("Overriding")
                .Ctor<IPricingFactorsRepository>().Is<OverridePricingFactorsRepository>();
        });

现在,您可以通过名称获取不同的处理程序配置。默认值为$ code> PricingFactorsRepository 。

Now you can get the different handler configurations by name. The default is the one with the PricingFactorsRepository.

        var ph = ObjectFactory.GetInstance<PricingHandler>();
        var oph = ObjectFactory.GetNamedInstance<PricingHandler>("Overriding");

您希望将其与工厂方法相结合,其中对象取决于定价处理程序将获得基于用户输入的不同变体。

You'd want to combine this with a factory approach where the object depending on the pricing handler would get the different variants based on the user input.

public class PricingHandlerFactory
{
    public PricingHandlerFactory(IContainer container)
    {
        _container = container;
    }

    public PricingHandler Create(string type)
    {
         var instance = ObjectFactory.TryGetInstance<PricingHandler>(type);
         return instance ?? ObjectFactory.GetInstance<PricingHandler>();
    }
}

在需要它的地方注入PricingHandlerFactory(Structuremap将自动连线,所以不应该配置它),并用用户输入调用创建方法来获取一个 PricingHandler

Inject the PricingHandlerFactory where you need it (Structuremap will automatically wire it up, so there should be no need to configure it) and call the Create method with the user input to get a PricingHandler.

这篇关于如何将parent / child类作为constructmap参数传递给structmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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