Exemple #1
0
        public void InitializeFactory()
        {
            if (!_isInitialized)
            {
                FluentConfiguration conf = Fluently.Configure()
                                           .Database(MySQLConfiguration.Standard.ConnectionString(_connection))
                                           .CurrentSessionContext <ThreadLocalSessionContext>()
                                           .Mappings(map =>
                {
                    map.FluentMappings.Conventions.Add(
                        new EnumConvention(),
                        new CascadeAllConvention(),
                        new ColumnNameConvention(),
                        new TableNameConvention(),
                        LazyLoad.Never()
                        );
                    map.FluentMappings.AddFromAssemblyOf <EmployeeMap>();
                })
                                           .ExposeConfiguration(x =>
                {
                    x.SetInterceptor(new SqlStatementInterceptor());
                    x.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[] { new EntityService() };
                });

                if (!_writeOnly)
                {
                    conf.ExposeConfiguration(config => new SchemaUpdate(conf.BuildConfiguration()).Execute(true, true));
                }

                _sessionFactory = conf.BuildSessionFactory();

                _isInitialized = true;
            }
        }
        public static ISessionFactory BuildSessionFactory(
            Configuration configuration,
            Assembly mappingsAssembly,
            Type idUserTypeGenericType)
        {
            // todo: check args

            return(Fluently.Configure(configuration)
                   .Mappings(m => m.FluentMappings.AddFromAssembly(mappingsAssembly)
                             .Conventions.Add(ForeignKey.Format((p, t) =>
            {
                if (p == null)
                {
                    return t.Name.Underscore() + "_id";
                }

                return p.Name.Underscore() + "_id";
            }))
                             .Conventions.Add(LazyLoad.Never())
                             .Conventions.Add(Table.Is(x => x.TableName.Underscore().ToUpper()))
                             .Conventions.Add(ConventionBuilder.Property.Always(x => x.Column(x.Property.Name.Underscore())))
                             .Conventions.Add(new IdUserTypeConvention(idUserTypeGenericType))
                             )
                   .BuildSessionFactory());
        }
        /// <summary>
        /// Fluent NHibernate 에서 제공하는 Convention 설정에 따른 <see cref="IConvention"/> 인스턴스를 빌드하여 제공합니다.
        /// </summary>
        /// <param name="options">ConventionOptions 인스턴스</param>
        /// <returns>Convention 설정 정보를 기초로 만든 <see cref="IConvention"/>의 인스턴스 배열</returns>
        public static IList <IConvention> ToConventions(ConventionOptions options)
        {
            options.ShouldNotBeNull("optioons");

            if (IsDebugEnabled)
            {
                log.Debug("ConventionOptions 정보로 IConvention[]을 빌드합니다...  " + options);
            }

            var conventions = new List <IConvention>();

            conventions.Add((options.DefaultLazy) ? LazyLoad.Always() : LazyLoad.Never());

            if (options.DynamicInsert)
            {
                conventions.Add(FluentNHibernate.Conventions.Helpers.DynamicInsert.AlwaysTrue());
            }
            if (options.DynamicUpdate)
            {
                conventions.Add(FluentNHibernate.Conventions.Helpers.DynamicUpdate.AlwaysTrue());
            }

            if (options.TableNamePrefix.IsNotWhiteSpace())
            {
                conventions.Add(Table.Is(x => options.TableNamePrefix + x.EntityType.Name));
            }
            else if (options.TableNamePrefix.IsNotWhiteSpace())
            {
                conventions.Add(Table.Is(x => x.EntityType.Name + options.TableNameSurfix));
            }
            else
            {
                conventions.Add(Table.Is(x => x.EntityType.Name));
            }

            if (options.PrimaryKeyName.IsNotWhiteSpace())
            {
                conventions.Add(PrimaryKey.Name.Is(x => options.PrimaryKeyName));
            }
            else if (options.PrimaryKeySurfix.IsNotWhiteSpace())
            {
                conventions.Add(PrimaryKey.Name.Is(x => x.EntityType.Name + options.PrimaryKeySurfix));
            }

            if (options.ForeignKeySurfix.IsNotWhiteSpace())
            {
                conventions.Add(ForeignKey.EndsWith(options.ForeignKeySurfix));

                conventions.Add(ConventionBuilder.HasMany.Always(x => x.Key.Column(x.EntityType.Name + options.ForeignKeySurfix)));
                conventions.Add(ConventionBuilder.Reference.Always(x => x.Column(x.Property.Name + options.ForeignKeySurfix)));

                conventions.Add(ConventionBuilder.HasManyToMany.Always(x => {
                    x.Key.Column(x.EntityType.Name + options.PrimaryKeySurfix);
                    x.Relationship.Column(x.ChildType.Name +
                                          options.ForeignKeySurfix);
                }));
            }

            return(conventions);
        }
        private ISessionFactory BuildSessionFactory(Configuration configuration, Assembly mappingsAssembly)
        {
            return(Fluently.Configure(configuration)
                   .Mappings(m => m.FluentMappings.AddFromAssembly(mappingsAssembly)
                             .Conventions.Add(ForeignKey.Format((p, t) =>
            {
                if (p == null)
                {
                    return t.Name.Underscore() + "_id";
                }

                return p.Name.Underscore() + "_id";
            }))
                             .Conventions.Add(LazyLoad.Never())
                             .Conventions.Add(Table.Is(x => x.TableName.Underscore().ToUpper()))
                             .Conventions.Add(ConventionBuilder.Property.Always(x => x.Column(x.Property.Name.Underscore())))
                             .Conventions.Add(typeof(IdUserTypeConvention), new IdUserTypeConvention(this.GetIdUserTypeGeneric()))
                             )
                   .BuildSessionFactory());
        }