使用autofac注册多个dbcontext

时间:2014-05-19 17:36:38

标签: entity-framework ioc-container autofac

我们在带有autofac IOC容器的DDD架构的asp.net MVC中有应用程序。我们正在尝试使用不同的数据库连接注册两个dbcontext。但唯一的最后一个是有效的。我们正在使用Entity Framework 4.4.0。这是代码。

var masterDataSettingManager = new SaasDataSettingManager();

if (masterDataSettingManager.LoadSettings() != null)
{
    var masterProviderSettings = masterDataSettingManager.LoadSettings();
    builder.Register(c => masterDataSettingManager.LoadSettings()).As<DataSettings>();
    builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();

    builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency();
    builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IEfDataProvider>().InstancePerDependency();

    if (masterDataSettingManager != null && masterProviderSettings.IsValid())
    {
        var efDataProviderManager = new EfDataProviderManager(masterDataSettingManager.LoadSettings());
        var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider();
        dataProvider.InitConnectionFactory();

        var dbProviderFactory = efDataProviderManager.LoadDbProviderFactories();

        builder.Register<IDbContext>(c => new MyDbContext1(masterProviderSettings.DataConnectionString, dbProviderFactory)).InstancePerHttpRequest();
    }
    else
    {
        builder.Register<IDbContext>(c => new MyDbContext1(masterDataSettingManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest();
    }
}

//data layer
var dataSettingsManager = new DataSettingsManager();
var dataProviderSettings = dataSettingsManager.LoadSettings();
builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();

builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency();
builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IEfDataProvider>().InstancePerDependency();

if (dataProviderSettings != null && dataProviderSettings.IsValid())
{
    var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings());
    var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider();
    dataProvider.InitConnectionFactory();

    var dbProviderFactory = efDataProviderManager.LoadDbProviderFactories();

    builder.Register<IDbContext>(c => new MyDbContext2(dataProviderSettings.DataConnectionString, dbProviderFactory)).InstancePerHttpRequest();
}
else
{
    builder.Register<IDbContext>(c => new MyDbContext2(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest();
}

builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();

1 个答案:

答案 0 :(得分:3)

像这样注册:

builder
    .Register(c => 
        new MyDbContext1(dataProviderSettings.DataConnectionString, dbProviderFactory))
    .Named<IDbContext>("dbContext1").InstancePerHttpRequest();
builder
    .Register(c => 
        new MyDbContext2(dataProviderSettings.DataConnectionString, dbProviderFactory))
    .Named<IDbContext>("dbContext2").InstancePerHttpRequest();

然后解决它们:

IDbContext dbContext = ctx.ResolveNamed<IDbContext>("dbContext1");

如果你想在构造函数中注入它,你可以这样做:

builder
    .RegisterType<SomeService>()
    .WithParameter(
        (p, c) => p.Name == "dbContext",
        (p, c) => c.ResolveNamed<IDbContext>("dbContext1")
    );

除了命名注册pf dbContexts之外,您仍然可以与它们并行定义未命名的 - 它可以被视为默认值。然后,在您的应用程序中,您希望使用命名注册,然后以上述方式指定此愿望。