예제 #1
0
        public static ISessionFactory OpenSessionFact()
        {
            var configuration = new Configuration();

            // for nhibernate configuration files
            var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml");

            //var configurationPath = HttpContext.Current.Server.MapPath(@"~\hibernate.cfg.xml");
            configuration.Configure(configurationPath);

            // for entity config files
            //var employeeConfigurationFile = HttpContext.Current.Server.MapPath(@"~\Models\Nhibernate\Employee.hbm.xml");
            //configuration.AddFile(employeeConfigurationFile);
            var serializer = new HbmSerializer()
            {
                Validate = true
            };

            using (var stream = serializer.Serialize(typeof(ServiceDeviceData)))
            {
                configuration.AddInputStream(stream, typeof(ServiceDeviceData).Name);
            }

            ISessionFactory sesFactory = configuration.BuildSessionFactory();

            return(sesFactory);
            //return sessionFactory.OpenSession();
        }
예제 #2
0
    static void AddAttributeMappings(Configuration nhConfiguration)
    {
        var hbmSerializer = new HbmSerializer
        {
            Validate = true
        };

        using (var stream = hbmSerializer.Serialize(typeof(Program).Assembly))
        {
            nhConfiguration.AddInputStream(stream);
        }
    }
예제 #3
0
        public override Configuration Map(Configuration cfg)
        {
            HbmSerializer serializer = new HbmSerializer()
            {
                Validate = true
            };

            using (MemoryStream stream = serializer.Serialize(this.Assembly))
            {
                cfg.AddInputStream(stream);
            }

            return(cfg);
        }
        private static void AddAttributeMappings(Configuration nhConfiguration)
        {
            var hbmSerializer = new HbmSerializer {
                Validate = true
            };

            //hbmSerializer.Serialize(typeof(Program).Assembly, "nhmap.xml");

            using (var stream = hbmSerializer.Serialize(typeof(Program).Assembly))
            {
                Debug.Write(stream);
                nhConfiguration.AddInputStream(stream);
            }
        }
예제 #5
0
        /*
         *
         * public NHibernateFactory(string configFileFullName, Assembly typesAssembly)
         * {
         *  pConfigFileFullName = configFileFullName;
         *  pAssembly = typesAssembly;
         * }
         *
         */

        public virtual void Configure()
        {
            var configuration = new Configuration();

            //for nhibernate configuration properties
            configuration.SetProperties(pDriverSettings);

            //for nhibernate configuration file
            //var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml");
            //var configurationPath = HttpContext.Current.Server.MapPath(@"~\hibernate.cfg.xml");
            //configuration.Configure(configurationPath);

            // for entity config files
            //var employeeConfigurationFile = HttpContext.Current.Server.MapPath(@"~\Models\Nhibernate\Employee.hbm.xml");
            //configuration.AddFile(employeeConfigurationFile);

            var serializer = new HbmSerializer()
            {
                Validate = true
            };

            Type[] arr_typ = pAssembly.GetExportedTypes();//pAssembly.GetTypes();

            foreach (var item in arr_typ)
            {
                if (item.IsClass &&
                    item.GetInterfaces().Contains(typeof(IEntity)) &&
                    item.IsAbstract == false &&
                    typeof(IEntity).IsAssignableFrom(item)
                    )
                {
                    using (var stream = serializer.Serialize(item))
                    {
                        configuration.AddInputStream(stream, item.Name);
                    }
                }
            }

            //using (var stream = serializer.Serialize(typeof(ServiceDeviceData)))
            //{
            //    configuration.AddInputStream(stream, typeof(ServiceDeviceData).Name);
            //}

            sessionFactory = configuration.BuildSessionFactory();
        }
예제 #6
0
        /// <summary>
        /// updated the NHibernate confuguration
        /// </summary>
        private void updateConfuguration( )
        {
            try
            {
                cfg.DataBaseIntegration(x =>
                {
                    x.ConnectionString = DatabaseConfiguration.Instance
                                         .GetConnectionString();
                    x.Driver <OleDbDriver>();
                    x.Dialect <Oracle10gDialect>();

                    x.GetType()
                    .GetMethod("Driver")
                    .MakeGenericMethod(DatabaseConfiguration.Instance.Driver)
                    .Invoke(x, null);
                    x.GetType()
                    .GetMethod("Dialect")
                    .MakeGenericMethod(DatabaseConfiguration.Instance.Dialect)
                    .Invoke(x, null);
                });
                var serializer = new HbmSerializer()
                {
                    Validate = true
                };

                using (var stream = serializer.Serialize(DatabaseConfiguration.
                                                         Instance.GetAssembly()))
                {
                    cfg.AddInputStream(stream);
                }
            }
            catch (DatabaseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw (new DatabaseException(ex, "Updating the configuration failed!"));
            }
        }
예제 #7
0
        private static ISessionFactory CreateEntityManagerFactory(SchemaFactoryModeEnum mode, CategoryPropertyItem configItem)
        {
            CategoryPropertyItem item = ConfigurationAccessHelper.GetCategoryPropertyByPath(configItem.PropertyItems, "DatabaseManager");

            if (item != null && !string.IsNullOrEmpty(item.EntryValue))
            {
                Type databaseManagerType = null;
                try
                {
                    databaseManagerType = TypeHelper.GetTypeFromString(item.EntryValue);
                }
                catch (Exception ex)
                {
                    throw new InvalidConfigurationValueException(ex.Message, ex);
                }

                if (databaseManagerType != null)
                {
                    using (IDatabaseManager manager = (IDatabaseManager)databaseManagerType.GetConstructor(Type.EmptyTypes).Invoke(null))
                    {
                        manager.Initialize(item);
                        Dictionary <string, string> settings = new Dictionary <string, string>();
                        foreach (CategoryPropertyItem pi in ConfigurationAccessHelper.GetCategoryPropertyByPath(configItem.PropertyItems, "NHibernateSettings"))
                        {
                            settings[pi.Id] = pi.EntryValue;
                        }
                        manager.EnsureDatabaseIntegrity(SYSTEM_ID, settings, mode);
                    }
                }
            }

            string hbm2ddl = "hbm2ddl.auto";

            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            cfg.Properties.Clear();

            foreach (CategoryPropertyItem pi in ConfigurationAccessHelper.GetCategoryPropertyByPath(configItem.PropertyItems, "NHibernateSettings"))
            {
                if (!hbm2ddl.Equals(pi.Id.ToLower()))
                {
                    cfg.Properties[pi.Id] = pi.EntryValue;
                }
            }

            if (mode == SchemaFactoryModeEnum.Create)
            {
                cfg.Properties[hbm2ddl] = "create";
            }
            else if (mode == SchemaFactoryModeEnum.Create_And_Drop)
            {
                cfg.Properties[hbm2ddl] = "create-drop";
            }

            HbmSerializer serializer = new HbmSerializer();

            serializer.HbmAssembly      = typeof(PersistentStorageItem).Assembly.GetName().FullName;
            serializer.HbmAutoImport    = true;
            serializer.Validate         = true;
            serializer.WriteDateComment = false;
            serializer.HbmDefaultAccess = "field";

            cfg.AddInputStream(serializer.Serialize(typeof(PersistentStorageItem).Assembly));
            //cfg.Configure();

            if (mode == SchemaFactoryModeEnum.Validate)
            {
                SchemaValidator schemaValidator = new SchemaValidator(cfg);
                schemaValidator.Validate(); // validate the database schema
            }
            else if (mode == SchemaFactoryModeEnum.Update)
            {
                SchemaUpdate schemaUpdater = new SchemaUpdate(cfg); // try to update schema
                schemaUpdater.Execute(false, true);
                if (schemaUpdater.Exceptions.Count > 0)
                {
                    throw new Exception("FAILED TO UPDATE SCHEMA");
                }
            }

            return(cfg.BuildSessionFactory());
        }