使用带有DI的多数据库实例的nHibernate [英] using nHibernate with multi DB instances with DI

查看:125
本文介绍了使用带有DI的多数据库实例的nHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有多个数据库实例和一个Web应用程序。
取决于url名称,我知道我需要与哪个db实例交谈。
我想更改旧的丑陋DAL与nhibernate一起使用,我想用DI来完成。
所以,假设我有这个类:

  class CompanyDal(ISession session)// nhibernate session 
{
Save(ICompany company)
{
session.Save(company)
}
}

所以我在创建DAL对象时注入了hibernate会话。但是我的IOC需要知道注入哪个会话,这意味着哪个连接字符串。
如果我只有一个单身工厂,我会为不同的网站(不同的数据库)得到错误的会话。我希望我解释它,以便大家都能理解。
谢谢

解决方案

假设所有数据库都具有相同的模式,您可以使用自定义的DriverConnectionProvider 。在下面的代码中,我将web.config中的连接字符串加载到字典中,然后使用URL中的tenant路由值检索它们。

  public class TenantConnectionProvider:DriverConnectionProvider 
{
private IDictionary< string,string> _tenantConnectionStrings;

public override void Configure(IDictionary< string,string>设置)
{
//从配置文件加载连接字符串
_tenantConnectionStrings = new Dictionary< string,string> ;(StringComparer.OrdinalIgnoreCase);
foreach(ConfigurationManager.ConnectionStrings中的ConnectionStringSettings connectionStringSetting)
{
_tenantConnectionStrings.Add(connectionStringSetting.Name,connectionStringSetting.ConnectionString);
}

base.Configure(settings);
}

public override IDbConnection GetConnection()
{
var connectionString = GetConnectionString(); //未显示,我的Web API为
var connection = new SqlConnection(connectionString);
connection.Open();
返回连接;


然后我使用Fluent NHibernate配置它:

  //即使使用自定义提供程序,也必须设置连接字符串
var config = Fluently.Configure()
。数据库(MsSqlConfiguration.MsSql2008.ConnectionString(custom)。Provider< TenantConnectionProvider>)
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf< MyClass>() ;
});


So I have multi db instances and One web application. depending on the url name, I know what db instance I need to talk to. I want to change the old ugly DAL to work with nhibernate and I want to use DI to do that. so, let say I have this class:

class CompanyDal(ISession session) //nhibernate session
{
    Save(ICompany company)
    { 
        session.Save(company)
    }
}

so I am injecting the hibernate session when creating the DAL object. but my IOC needs to know which session to inject, meaning which connection string. If I have only one singleton factory I will get the wrong session for different urls (different db)

I hope I explained it so you all can understand. thank you

解决方案

Assuming all the databases have the same schema, you can dynamically switch the connection using a custom DriverConnectionProvider. In the code below I am loading the connection strings from web.config into a dictionary, then retrieving them using a "tenant" route value from the URL.

public class TenantConnectionProvider : DriverConnectionProvider
{
    private IDictionary<string, string> _tenantConnectionStrings;

    public override void Configure(IDictionary<string, string> settings)
    {
        // Load connection strings from config file
        _tenantConnectionStrings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        foreach (ConnectionStringSettings connectionStringSetting in ConfigurationManager.ConnectionStrings)
        {
            _tenantConnectionStrings.Add(connectionStringSetting.Name, connectionStringSetting.ConnectionString);
        }

        base.Configure(settings);
    }

    public override IDbConnection GetConnection()
    {
        var connectionString = GetConnectionString(); //not shown, mine is for Web API
        var connection = new SqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

I then configure this using Fluent NHibernate:

// connection string has to be set even though custom provider is used
var config = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008.ConnectionString("custom").Provider<TenantConnectionProvider>)
    .Mappings(m =>
    {
        m.FluentMappings.AddFromAssemblyOf<MyClass>();
    });

这篇关于使用带有DI的多数据库实例的nHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆