简体   繁体   English

c#autofac解决问题

[英]c# autofac resolve issue

I am new to Autofac and not sure how can i pass object in the builder. 我是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;
    }
}

Bootstrap 引导程序

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

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

        return builder.Build();
    }

When the app runs, i am loading affiliate first and want to pass the affiliate in OrderFactory 应用运行时,我先加载会员,并希望在OrderFactory中传递会员

Program 程序

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

so channel object is now populated with many properties and i want to access the Channel object in OrderFactory but i am getting affiliate null.. 所以通道对象现在填充有许多属性,我想在OrderFactory中访问Channel对象,但我得到的会员为null。

please advice 请指教

Well, i created Repository and getting the channel from there... 好吧,我创建了存储库并从那里获取频道...

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();
    }

so this means that whenever OrderFactory is returned, i get the channel from ChannelRepository... 所以这意味着每当返回OrderFactory时,我都会从ChannelRepository获取通道...

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

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