Beispiel #1
0
        /// <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(EntityWithTypedId<int>),
                typeof(EntityWithTypedId<string>)
            };

            // Modified for NS: PIMS.Infrastructure.NHibernate.NHAspNetIdentity
            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(ValueObject).IsAssignableFrom(type));

               // Modified for NS: PIMS.Infrastructure.NHibernate.NHAspNetIdentity
            mapper.AddMapping<IdentityUserMap>();
            mapper.AddMapping<IdentityRoleMap>();
            mapper.AddMapping<IdentityUserClaimMap>();

            return mapper.CompileMappingFor(allEntities);
        }
        public static void Configure(ISessionStorage storage)
        {
            var baseEntityToIgnore = new[] { 
                typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId<int>), 
                typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId<string>), 
            };

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

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

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

            var mapping = mapper.CompileMappingFor(allEntities);
            System.Diagnostics.Debug.WriteLine(mapping.AsString());

            var configuration = NHibernateSession.Init(storage, mapping);
            BuildSchema(configuration);
        }
        /// <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(ApplicationTenant),
                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>();
            mapper.AddMapping<ApplicationTenantMap>();

            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),                 
                typeof(ApplicationTenant),
            };

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

            mapper.AddMapping<ApplicationTenantMap>();
            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();
        }
Beispiel #5
0
        public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration)
        {
            var baseEntityType = typeof(Entity);

            mapper.IsEntity((type, declared) => IsEntity(type));
            mapper.IsComponent((type, b) => IsComponent(type));
            mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

            mapper.BeforeMapClass += (modelInspector, type, classCustomizer) =>
            {
                classCustomizer.Id(c => c.Column("Id"));
                classCustomizer.Id(c => c.Generator(Generators.HighLow));
                classCustomizer.Table(Inflector.Net.Inflector.Pluralize(type.Name.ToString()));
            };

            mapper.IsPersistentProperty((memberinfo, currentlyPersistent) =>
            {
                return(memberinfo.Name != "Owner");
            });

            mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) =>
            {
                map.Column(propertyPath.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };

            mapper.BeforeMapBag += (modelInspector, propertyPath, map) =>
            {
                map.Key(keyMapper => keyMapper.Column(propertyPath.GetContainerEntity(modelInspector).Name + "Id"));
                map.Cascade(Cascade.All);
            };

            mapper.BeforeMapProperty += (inspector, member, customizer) => {
                // This is pure guesswork, but seems to be the only way I can think of to alter
                // the column naming of a property mapped as part of a component
                if (typeof(IComponent).IsAssignableFrom(member.LocalMember.DeclaringType))
                {
                    if (member.LocalMember.Name == "Value")
                    {
                        customizer.Column(member.PreviousPath.LocalMember.Name);
                    }
                    else if (member.LocalMember.Name != "Owner")
                    {
                        customizer.Column(member.PreviousPath.LocalMember.Name + member.LocalMember.Name);
                    }
                }
            };


            mapper.Component <RestrictedVisibility <string> >(x => {
                x.Property(c => c.Value);
                x.Property(c => c.Visibility);
                x.Parent(c => c.Owner);
            });

            mapper.Component <RestrictedVisibility <bool> >(x => {
                x.Property(c => c.Value);
                x.Property(c => c.Visibility);
                x.Parent(c => c.Owner);
            });

            // The following probably works, but not if we stop "Owner" from being a persistent property
            // using mapping.IsPersistentProperty, and if we don't do that we get a Too Many Properties exception.
            // There's an old thread on nhusers about how there's no documentation in this area...
            //mapper.BeforeMapComponent += (inspector, member, customizer) => {
            //    if (member.LocalMember.Name == "Owner") {
            //        customizer.Parent(member.LocalMember);
            //    }
            //};

            AddConventionOverrides(mapper);

            HbmMapping mapping = mapper.CompileMappingFor(
                typeof(Entity).Assembly.GetExportedTypes().Where(IsEntity));

            configuration.AddDeserializedMapping(mapping, "MyStoreMappings");
        }