示例#1
0
        public static IDataAccessor CreateDataAccessor(string connectionString)
        {
            if (DataAccessor == null || DataAccessor.ContextName != connectionString)
            {
                lock (lock_flag)
                {
                    if (DataAccessor == null || DataAccessor.ContextName != connectionString)
                    {
                        DataAccessorConfiguration config = new DataAccessorConfiguration(); // TODO: 初始化为适当的值
                        config.MappingsAssemblies = new List <System.Reflection.Assembly>();
                        config.MappingsAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly());
                        config.MappingsAssemblies.AddRange(NS.Framework.Config.PlatformConfig.ServerConfig.DataBaseSetting.GetMappingAssemblys);
                        var configFileName = string.Format("{0}-{1}", connectionString, NS.Framework.Config.PlatformConfig.ServerConfig.DataBaseSetting.ConfigFile);

                        if (NS.Framework.Config.PlatformConfig.ServerConfig.ConfigFilePath.EndsWith("bin"))
                        {
                            config.ConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin", configFileName);
                        }
                        else
                        {
                            config.ConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, configFileName);
                        }

                        DataAccessorFactory target = new DataAccessorFactory(config);

                        DataAccessor             = target.CreateDataAccessor();
                        DataAccessor.ContextName = connectionString;
                    }
                }
            }

            return(DataAccessor);
        }
示例#2
0
        public static IDataAccessor CreateDataAccessor(params Assembly[] assemblys)
        {
            if (DataAccessor == null)
            {
                lock (lock_flag)
                {
                    if (DataAccessor == null)
                    {
                        DataAccessorConfiguration config = new DataAccessorConfiguration(); // TODO: 初始化为适当的值
                        config.MappingsAssemblies = new List <System.Reflection.Assembly>();
                        config.MappingsAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly());
                        config.MappingsAssemblies.AddRange(assemblys);

                        if (NS.Framework.Config.PlatformConfig.ServerConfig.ConfigFilePath.EndsWith("bin"))
                        {
                            config.ConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin", "hibernate.cfg.xml");
                        }
                        else
                        {
                            config.ConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml");
                        }

                        DataAccessorFactory target = new DataAccessorFactory(config);

                        DataAccessor = target.CreateDataAccessor();
                    }
                }
            }

            return(DataAccessor);
        }
示例#3
0
        /// <summary>
        /// 通过提供配置文件和映射信息程序集来创建持久化数据访问器
        /// </summary>
        /// <param name="path">配置文件全路径</param>
        /// <param name="assembly">NHibernate映射信息程序集</param>
        /// <returns>数据访问器接口</returns>
        public static IDataAccessor CreateDataAccessor(string path, System.Reflection.Assembly assembly)
        {
            DataAccessorConfiguration configuration = new DataAccessorConfiguration();

            configuration.ConfigFile = path;
            configuration.MappingsAssemblies.Add(assembly);
            DataAccessorFactory factory = new DataAccessorFactory(configuration);

            return(factory.CreateDataAccessor());
        }
示例#4
0
        /// <summary>
        /// 使用应用程序的基目录中的hibernate.cfg.xml来配置Nhibernate
        /// </summary>
        /// <param name="assembly">包含实体类以及映射文件的程序集</param>
        /// <returns>数据访问器接口</returns>
        public static IDataAccessor CreateDataAccessor(System.Reflection.Assembly assembly)
        {
            DataAccessorConfiguration configuration = new DataAccessorConfiguration();

            configuration.ConfigFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml");
            configuration.MappingsAssemblies.Add(assembly);

            DataAccessorFactory factory = new DataAccessorFactory(configuration);

            return(factory.CreateDataAccessor());
        }
示例#5
0
 /// <summary>
 /// 使用配置信息来创建工厂
 /// </summary>
 /// <param name="config">持久化数据访问的配置信息对象</param>
 public DataAccessorFactory(DataAccessorConfiguration config)
 {
     if (config == null || string.IsNullOrWhiteSpace(config.ConfigFile) || (config.MappingsAssemblies == null || config.MappingsAssemblies.Count == 0))
     {
         throw new ArgumentNullException();
     }
     if (!File.Exists(config.ConfigFile))
     {
         throw new ArgumentException();
     }
     this.config = config;
 }
示例#6
0
        public static IDataAccessor CreateDataAccessor()
        {
            if (DataAccessor == null)
            {
                lock (lock_flag)
                {
                    if (DataAccessor == null)
                    {
                        DataAccessorConfiguration config = new DataAccessorConfiguration(); // TODO: 初始化为适当的值
                        config.MappingsAssemblies = new List <System.Reflection.Assembly>();
                        config.MappingsAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly());
                        config.MappingsAssemblies.AddRange(NS.Framework.Config.PlatformConfig.ServerConfig.DataBaseSetting.GetMappingAssemblys);
                        config.ConfigFile = NS.Framework.Config.PlatformConfig.ServerConfig.DataBaseSetting.ConfigFile;

                        DataAccessorFactory target = new DataAccessorFactory(config);

                        DataAccessor = target.CreateDataAccessor();
                    }
                }
            }

            return(DataAccessor);
        }