How to use NHibernate with both MySQL server and Microsoft SQL server 2008

后端 未结 2 1245
慢半拍i
慢半拍i 2021-01-29 03:44

How can I configure NHibernate to connect to both a MySQLserver and a Microsoft SQL server 2008? I do want to copy data from one server to another. I heard of NHibernate shared.

2条回答
  •  终归单人心
    2021-01-29 04:34

    I've struggled quite a bit few months ago. My problem was with MS Sql Server and Oracle.

    What I've done is to create two separate config files for nhibernate:

    sql.nhibernate.config

    
        
          
          
            NHibernate.Connection.DriverConnectionProvider
            NHibernate.Driver.SqlClientDriver
            NHibernate.Dialect.MsSql2008Dialect
            
            web
            100
            120
            3
            true
            true 1, false 0, yes 'Y', no 'N'
            NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
            
          
    
    

    ora.nhibernate.config

    
    
      
      
        NHibernate.Connection.DriverConnectionProvider
        NHibernate.Driver.OracleDataClientDriver
        NHibernate.Dialect.Oracle10gDialect
        
        web
        100
        120
        3
        true
        true 1, false 0, yes 'Y', no 'N'
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
        
      
    
    

    I use this simple class to build my nhibernate SessionFactory:

        public class NHibernateSessionFactory
        {
            private ISessionFactory sessionFactory;
    
            private readonly string ConnectionString = "";
            private readonly string nHibernateConfigFile = "";
    
            public NHibernateSessionFactory(String connectionString, string nHConfigFile)
            {
                this.ConnectionString = connectionString;
                this.nHibernateConfigFile = nHConfigFile;
            }
    
            public ISessionFactory SessionFactory
            {
                get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); }
            }
    
            private ISessionFactory CreateSessionFactory()
            {
                Configuration cfg;
                cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.nHibernateConfigFile));
    
                // With this row below Nhibernate searches for the connection string inside the App.Config.
                // cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
                cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, this.ConnectionString);
    
    #if DEBUG
                cfg.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true");
                cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true");
    #endif
    
                return (cfg.BuildSessionFactory());
            }
        }
    

    As you can see I pass to my NHibernateSessionFactory a connection string (I prefer to save it in my app config file) and the name (without the path) of the nhibernate config file.

    I personally use a DI container (StructureMap) and you can achieve something very cool defining a registry class:

    public class NhibernateRegistry : Registry
    {
        public NhibernateRegistry()
        {
    
            For()
            .Singleton()
            .Add(new NHibernateSessionFactory(, "ora.nhibernate.config").SessionFactory)
            .Named("OracleSF");
    
            For()
            .HybridHttpOrThreadLocalScoped()
            .Add(o => o.GetInstance("OracleSF").OpenSession())
            .Named("OracleSession");
    
            For()
            .Singleton()
            .Add(new NHibernateSessionFactory(, "sql.nhibernate.config").SessionFactory)
            .Named("MsSqlSF");
    
            For()
            .HybridHttpOrThreadLocalScoped()
            .Add(o => o.GetInstance("MsSqlSF").OpenSession())
            .Named("MsSqlSession");
        }
    }
    

    in which you can use named instances. My services layer than uses a StructureMap registry class where you can define the constructors:

    this.For()
         .HybridHttpOrThreadLocalScoped()
         .Use()
         .Ctor("sessionMDII").Is(x => x.TheInstanceNamed("OracleSession"))
         .Ctor("sessionSpedizioni").Is(x => x.TheInstanceNamed("MsSqlSession"));
    

    For your Service implementation:

    public class OrdersService : IOrdersService
    {
            private readonly ISession SessionMDII;
            private readonly ISession SessionSpedizioni;
    
            public OrdersService(ISession sessionMDII, ISession sessionSpedizioni)
            {
                this.SessionMDII = sessionMDII;
                this.SessionSpedizioni = sessionSpedizioni;
            }
    
        ...
    }
    

提交回复
热议问题