<commit_before>﻿using System;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Sparrow.Domain.Models;

namespace Sparrow.Web.App_Start
{
    public class NHibernateConfig
    {
        public static ISessionFactory BuildSessionFactory()
        {
            var configuration = Fluently
                .Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString(str => str.FromConnectionStringWithKey("sparrow"))
                        .ShowSql
                )
                .Mappings(m =>
                {
                    m.AutoMappings
                        .Add(
                            AutoMap.AssemblyOf<User>(new AutoMapConfiguration())
                                .IgnoreBase<EntityBase>()
                                .Conventions.Add(
                                    new NotNullConvention(),
                                    ConventionBuilder.Class.Always(c => c.Schema("dbo")),
                                    ConventionBuilder.Id.Always(id => id.GeneratedBy.GuidComb()),
                                    ConventionBuilder.HasMany.Always(many => many.Cascade.AllDeleteOrphan()),
                                    DefaultCascade.All(),
                                    PrimaryKey.Name.Is(id => "ID"),
                                    ForeignKey.EndsWith("ID"),
                                    DefaultAccess.CamelCaseField(CamelCasePrefix.Underscore)
                                )
                        );

                })
                .BuildConfiguration();

            // Uncomment to automatically generate database tables.
            // new SchemaExport(configuration).Create(false, true);

            return configuration.BuildSessionFactory();
        }

        private class NotNullConvention : IPropertyConvention, IPropertyConventionAcceptance
        {
            public void Apply(IPropertyInstance instance)
            {
                instance.Not.Nullable();
            }

            public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
            {
                criteria.Expect(x => x.Nullable && (x.Name == "Name" || x.Name == "Title"));
            }
        }

        private class AutoMapConfiguration : DefaultAutomappingConfiguration
        {
            public override bool ShouldMap(Member member)
            {
                Member backingField;
                return base.ShouldMap(member) && member.TryGetBackingField(out backingField);
            }

            public override bool ShouldMap(Type type)
            {
                return type.IsSubclassOf(typeof(EntityBase));
            }
        }
    }
}<commit_msg>Added EnumConvention<commit_after>﻿using System;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Sparrow.Domain.Models;

namespace Sparrow.Web.App_Start
{
    public class NHibernateConfig
    {
        public static ISessionFactory BuildSessionFactory()
        {
            var configuration = Fluently
                .Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString(str => str.FromConnectionStringWithKey("sparrow"))
                        .ShowSql
                )
                .Mappings(m =>
                {
                    m.AutoMappings
                        .Add(
                            AutoMap.AssemblyOf<User>(new AutoMapConfiguration())
                                .IgnoreBase<EntityBase>()
                                .Conventions.Add(
                                    new EnumConvention(),
                                    new NotNullConvention(),
                                    ConventionBuilder.Class.Always(c => c.Schema("dbo")),
                                    ConventionBuilder.Id.Always(id => id.GeneratedBy.GuidComb()),
                                    ConventionBuilder.HasMany.Always(many => many.Cascade.AllDeleteOrphan()),
                                    DefaultCascade.All(),
                                    PrimaryKey.Name.Is(id => "ID"),
                                    ForeignKey.EndsWith("ID"),
                                    DefaultAccess.CamelCaseField(CamelCasePrefix.Underscore)
                                )
                        );

                })
                .BuildConfiguration();

            // Uncomment to automatically generate database tables.
            // new SchemaExport(configuration).Create(false, true);

            return configuration.BuildSessionFactory();
        }

        /// <summary>
        /// Convention that makes all primitive types as well as Name and Title fields not nullable.
        /// </summary>
        private class NotNullConvention : IPropertyConvention, IPropertyConventionAcceptance
        {
            public void Apply(IPropertyInstance instance)
            {
                instance.Not.Nullable();
            }

            public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
            {
                criteria.Expect(x => x.Nullable && (x.Name == "Name" || x.Name == "Title" || x.Property.PropertyType.IsPrimitive));
            }
        }

        /// <summary>
        /// Convention to use enumeration as the property type (instead of default string).
        /// </summary>
        private class EnumConvention : IPropertyConvention, IPropertyConventionAcceptance
        {
            public void Apply(IPropertyInstance instance)
            {
                instance.CustomType(instance.Property.PropertyType);
            }

            public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
            {
                criteria.Expect(x => x.Property.PropertyType.IsEnum);
            }
        }

        private class AutoMapConfiguration : DefaultAutomappingConfiguration
        {
            public override bool ShouldMap(Member member)
            {
                Member backingField;
                return base.ShouldMap(member) && member.TryGetBackingField(out backingField);
            }

            public override bool ShouldMap(Type type)
            {
                return type.IsSubclassOf(typeof(EntityBase));
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07846219808489352, 'fraction_numerical': 0.0014291839359725598, 'mean_word_length': 1.49305308158176, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3945509', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1543, 'n_words': 287}