繁体   English   中英

c#autofac解决问题

[英]c# autofac resolve issue

我是Autofac的新手,不确定如何在构建器中传递对象。

public class OrderFactory : IOrderFactory {
    private readonly IAffiliate _affiliate;
    private readonly IGetNewOrdersToImport _ordersToImportHelper;
    private readonly IOrderProcessor _orderProcessor;

    public OrderFactory(IAffiliate affiliate, IGetNewOrdersToImport ordersToImport, IOrderProcessor orderProcessor) {
        _affiliate =  affiliate;
        _ordersToImportHelper = ordersToImport;
        _orderProcessor = orderProcessor;
    }
}

引导程序

    public IContainer Configure()
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<Channel>().As<IAffiliate>();
        builder.RegisterType<OrderFactory>().As<IOrderFactory>();

        return builder.Build();
    }

应用运行时,我先加载会员,并希望在OrderFactory中传递会员

程序

var channel = new Channel().Get(param);
var merchantOrderManager = _myContainer.Resolve<IOrderFactory>();
merchantOrderManager.ImportMerchantOrders();

所以通道对象现在填充有许多属性,我想在OrderFactory中访问Channel对象,但我得到的会员为null。

请指教

好吧,我创建了存储库并从那里获取频道...

public IContainer Configure(IAffiliate affiliate)
    {
        var builder = new ContainerBuilder();

        builder.Register(c => new ChannelRepository().Get(affiliate.Code)).As<IAffiliate>();
        builder.RegisterType<DbContext>().As<IDbContext>().InstancePerLifetimeScope();

        builder.RegisterType<OrderFactory>().As<IOrderFactory>();

        return builder.Build();
    }

所以这意味着每当返回OrderFactory时,我都会从ChannelRepository获取通道...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM