c#autofac解决问题

时间:2016-11-30 12:23:00

标签: c# dependency-injection autofac

我是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对象,但我得到了affiliate null ..

请咨询

1 个答案:

答案 0 :(得分:0)

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

00 (Jan)

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