简体   繁体   中英

c# autofac resolve issue

I am new to Autofac and not sure how can i pass object in the builder.

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

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..

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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