/// <summary>
        /// Gets a mapping that can be used with NHibernate.
        /// </summary>
        /// <param name="additionalTypes">Additional Types that are to be added to the mapping, this is useful for adding your ApplicationUser class</param>
        /// <returns></returns>
        public static HbmMapping GetIdentityMappings(System.Type[] additionalTypes)
        {
            var baseEntityToIgnore = new[] {
                typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId <int>),
                typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId <string>),
            };

            var allEntities = new List <System.Type> {
                typeof(IdentityUser),
                typeof(IdentityRole),
                typeof(IdentityUserLogin),
                typeof(IdentityUserClaim),
            };

            allEntities.AddRange(additionalTypes);

            var mapper = new ConventionModelMapper();

            DefineBaseClass(mapper, baseEntityToIgnore.ToArray());
            mapper.IsComponent((type, declared) => typeof(NHibernate.AspNet.Identity.DomainModel.ValueObject).IsAssignableFrom(type));

            mapper.AddMapping <IdentityUserMap>();
            mapper.AddMapping <IdentityRoleMap>();
            mapper.AddMapping <IdentityUserClaimMap>();

            return(mapper.CompileMappingFor(allEntities));
        }
        /// <summary>
        /// constructor configures a SessionFactory based on the configuration passed in
        /// </summary>
        private SessionFactoryProvider()
        {
            Name = "NHibernate.AspNet.Identity";

            var baseEntityToIgnore = new[] {
                typeof(SharpArch.Domain.DomainModel.Entity),
                typeof(EntityWithTypedId <int>),
                typeof(EntityWithTypedId <string>),
            };

            var allEntities = new[] {
                typeof(IdentityUser),
                typeof(ApplicationUser),
                typeof(IdentityRole),
                typeof(IdentityUserLogin),
                typeof(IdentityUserClaim),
                typeof(Foo),
            };

            var mapper = new ConventionModelMapper();

            DefineBaseClass(mapper, baseEntityToIgnore);
            mapper.IsComponent((type, declared) => typeof(ValueObject).IsAssignableFrom(type));

            mapper.AddMapping <IdentityUserMap>();
            mapper.AddMapping <IdentityRoleMap>();
            mapper.AddMapping <IdentityUserClaimMap>();

            var mapping = mapper.CompileMappingForEach(allEntities);

            _configuration = new Configuration();
            _configuration.Configure("sqlite-nhibernate-config.xml");
            foreach (var map in mapping)
            {
                Console.WriteLine(map.AsString());
                _configuration.AddDeserializedMapping(map, null);
            }


            //log4net.Config.XmlConfigurator.Configure();
            SessionFactory = _configuration.BuildSessionFactory();
        }
Exemple #3
0
        private static HbmMapping ImplicitMapping()
        {
            var mapper = new ConventionModelMapper();

            mapper.IsEntity((t, d) => IsEntity(t));
            mapper.IsRootEntity((t, declared) => IsEntity(t) && !IsEntity(t.BaseType));
            mapper.IsComponent((t, d) => d);
            mapper.IsBag((mi, d) => d);

            mapper.IsPersistentProperty((mi, d) =>
            {
                if (mi.MemberType != MemberTypes.Property)
                {
                    return(false);
                }
                var pi = (PropertyInfo)mi;
                return(pi.CanRead && pi.CanWrite);
            });

            mapper.BeforeMapProperty += (model, member, prop) =>
            {
                var type = member.LocalMember.GetPropertyOrFieldType();
                if (type == typeof(DateTime))
                {
                    prop.Type <UtcDateTimeType>();
                }
                else if (type == typeof(JObject))
                {
                    prop.Type <JObjectType>();
                    prop.Length(65536); // medium clob
                }
            };

            mapper.BeforeMapSet += (model, member, set) =>
            {
                set.Key(k => k.Column(member.GetContainerEntity(model).Name + "Id"));
                set.Cascade(Cascade.All | Cascade.DeleteOrphans);
            };

            mapper.BeforeMapManyToOne += (model, member, mto) =>
            {
                mto.Column(member.LocalMember.Name + "Id");
            };

            mapper.BeforeMapClass += (model, type, cls) =>
            {
                cls.Id(id =>
                {
                    id.Column("Id");
                    id.Generator(Generators.HighLow, g => g.Params(new { max_lo = 100 }));
                });
                cls.Table(type.Name);
                cls.Lazy(false);
            };

            // add conformist mappings
            mapper.AddMappings(Assembly.GetAssembly(typeof(PersistentAttribute)).GetExportedTypes());

            // apply above conventions
            return(mapper.CompileMappingFor(typeof(PersistentAttribute).Assembly.GetExportedTypes().Where(IsEntity)));
        }
Exemple #4
0
        public static HbmMapping GetIdentityMappings(List <Type> entities)
        {
            var mapper = new ConventionModelMapper();

            // 1. Antes de mapear as propriedades, verificar atributos;
            mapper.BeforeMapProperty += (insp, prop, map) =>
            {
                var stringLength = prop.LocalMember.GetAttribute <StringLengthAttribute>();
                if (stringLength != null)
                {
                    map.Length(stringLength.MaximumLength);
                }

                var required = prop.LocalMember.GetAttribute <RequiredAttribute>();
                if (required != null)
                {
                    map.NotNullable(true);
                }

                var name = prop.LocalMember.GetAttribute <ColumnAttribute>();
                if (name != null)
                {
                    map.Column(name.Name);
                }
            };

            // 2. Antes de mapear as many-to-one e one-to-many, verificar Ids;
            mapper.BeforeMapManyToOne += (insp, prop, map) =>
            {
                map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };

            mapper.BeforeMapBag += (insp, prop, map) =>
            {
                map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
                map.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
                map.BatchSize(10);
            };

            // 3. Match
            Func <Type, bool, bool> matchRootEntity =
                (type, wasDeclared) => typeof(EntityWithTypedId <int>).Equals(type.BaseType) ||
                typeof(EntityWithTypedId <string>).Equals(type.BaseType);

            mapper.IsEntity((type, wasDeclared) => entities.Contains(type));
            mapper.IsRootEntity(matchRootEntity);
            mapper.IsComponent((type, declared) => entities.Contains(type));

            mapper.MapAllEnumsToStrings();

            List <Type> mappings =
                Assembly.GetAssembly(typeof(AlunoMap))
                .GetExportedTypes()
                .Where(t => t.BaseType.IsGenericType &&
                       (t.BaseType.GetGenericTypeDefinition().Equals(typeof(ClassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(SubclassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(JoinedSubclassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(UnionSubclassMapping <>))))
                .ToList();

            mapper.AddMappings(mappings);

            return(mapper.CompileMappingFor(entities));
        }