Пример #1
0
		public virtual void MapTypes(List<Type> allTypes, NHibernate.Cfg.Configuration cfg, Func<string, string> formatter)
		{
			var m = new HbmMapping();
			m.Items = allTypes.Select(t =>
				{
					var sc = new HbmSubclass();
					sc.name = GetName(t);
					sc.extends = GetName(t.BaseType);
					sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name;
					sc.lazy = false;
					sc.lazySpecified = true;

					var propertyMappings = GetPersistables(t)
						.Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter))
						.ToList();
					if (propertyMappings.Count > 0)
					{
						if (sc.Items == null)
							sc.Items = propertyMappings.ToArray();
						else
							sc.Items = sc.Items.Union(propertyMappings).ToArray();
					}
					logger.DebugFormat("Generating subclass {0} with discriminator {1} extending {2} with {3} items ({4} property mappings)", sc.name, sc.discriminatorvalue, sc.extends, sc.Items != null ? sc.Items.Length.ToString() : "(null)", propertyMappings.Count);
					return sc;
				}).ToArray();
			if (Debugger.IsAttached)
			{
				var dbg = m.AsString();
			}
			cfg.AddDeserializedMapping(m, "N2");
		}
Пример #2
0
        public virtual void MapTypes(List<Type> allTypes, NHibernate.Cfg.Configuration cfg, Func<string, string> formatter)
        {
            var m = new HbmMapping();
            m.Items = allTypes.Select(t =>
                {
                    var sc = new HbmSubclass();
                    sc.name = GetName(t);
                    sc.extends = GetName(t.BaseType);
                    sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name;
                    sc.lazy = false;
                    sc.lazySpecified = true;

                    var propertyMappings = GetPersistables(t)
                        .Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter))
                        .ToList();
                    if (propertyMappings.Count > 0)
                    {
                        if (sc.Items == null)
                            sc.Items = propertyMappings.ToArray();
                        else
                            sc.Items = sc.Items.Union(propertyMappings).ToArray();
                    }

                    return sc;
                }).ToArray();
            var dbg = m.AsString();
            cfg.AddDeserializedMapping(m, "N2");
        }
        public override void AlterConfiguration(NHibernate.Cfg.Configuration cfg)
        {
            ModelMapper mm = new ModelMapper();
            mm.Class<FileSystemItem>(FileSystemItemCustomization);
            mm.Class<FileSystemChunk>(FileSystemChunkCustomization);

            var compiledMapping = mm.CompileMappingForAllExplicitlyAddedEntities();
            cfg.AddDeserializedMapping(compiledMapping, "N2");
        }
		public override void AlterConfiguration(NHibernate.Cfg.Configuration cfg)
		{
            ModelMapper mm = new ModelMapper();
			mm.Class<Bucket>(BucketCustomization);
			mm.Class<Statistic>(StatisticCustomization);
			

			var compiledMapping = mm.CompileMappingForAllExplicitlyAddedEntities();
			cfg.AddDeserializedMapping(compiledMapping, "N2");
		}
Пример #5
0
        public override void ConfigureMapping(NHibernate.Cfg.Configuration configuration)
        {
            var mapper = new ConventionModelMapper();

            mapper.BeforeMapProperty += (inspector, member, customizer) =>
            {
                if (member.LocalMember.GetPropertyOrFieldType() == typeof(string))
                {
                    customizer.Length(50);
                }
            };
            mapper.BeforeMapClass += (inspector, type, customizer) => customizer.Id(im => im.Generator(Generators.GuidComb));
            // define the mapping shape

            mapper.Class<Hotel>(ca => ca.List(item => item.Reservations, cm => cm.Key(km => km.Column("HotelId"))));
            mapper.Class<Room>(ca => ca.List(item => item.Reservations, cm => cm.Key(km =>
                                                                                               	{
                                                                                               		km.Column("RoomId");
                                                                                               		km.OnDelete(OnDeleteAction.NoAction);
                                                                                               	})));
            mapper.Class<Reservation>(ca =>
                                          	{
                                                ca.ManyToOne(item => item.Hotel, m => { m.Column("HotelId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy);} );
                                                ca.ManyToOne(item => item.Room, m => { m.Column("RoomId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy); });
                                          	});

            // list all the entities we want to map.
            IEnumerable<Type> baseEntities = GetMappingTypes();

            // compile the mapping for the specified entities
            HbmMapping mappingDocument = mapper.CompileMappingFor(baseEntities);

            // inject the mapping in NHibernate
            configuration.AddDeserializedMapping(mappingDocument, "Domain");
            // fix up the schema
            SchemaMetadataUpdater.QuoteTableAndColumns(configuration);

            SessionFactory.SessionFactoryInstance = configuration.BuildSessionFactory();
        }
        public override void ConfigureMapping(NHibernate.Cfg.Configuration configuration)
        {
            var orm = new ObjectRelationalMapper();
            var mapper = new Mapper(orm);

            mapper.AddPropertyPattern(mi => TypeExtensions.GetPropertyOrFieldType(mi) == typeof(string), pm => pm.Length(50));
            orm.Patterns.PoidStrategies.Add(new NativePoidPattern());
            orm.Patterns.PoidStrategies.Add(new GuidOptimizedPoidPattern());

            // list all the entities we want to map.
            IEnumerable<Type> baseEntities = GetMappingTypes();

            // we map all classes as Table per class
            orm.TablePerClass(baseEntities);

            mapper.Customize<Hotel>(ca => ca.Collection(item => item.Reservations, cm => cm.Key(km => km.Column("HotelId"))));
            mapper.Customize<Room>(ca => ca.Collection(item => item.Reservations, cm => cm.Key(km =>
            {
                km.Column("RoomId");
                km.OnDelete(OnDeleteAction.NoAction);
            })));
            mapper.Customize<Reservation>(ca =>
            {
                ca.ManyToOne(item => item.Hotel, m => { m.Column("HotelId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy); });
                ca.ManyToOne(item => item.Room, m => { m.Column("RoomId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy); });
            });

            // compile the mapping for the specified entities
            HbmMapping mappingDocument = mapper.CompileMappingFor(baseEntities);

            // inject the mapping in NHibernate
            configuration.AddDeserializedMapping(mappingDocument, "Domain");
            // fix up the schema
            SchemaMetadataUpdater.QuoteTableAndColumns(configuration);

            SessionFactory.SessionFactoryInstance = configuration.BuildSessionFactory();
        }
		/// <summary>Adds mappings to the configuration.</summary>
		/// <param name="cfg">The configuration to add the mappings to.</param>
		/// <param name="name">The resource name of the embedded resource.</param>
		protected virtual void AddMapping(NHibernate.Cfg.Configuration cfg, string name)
		{
			if (!string.IsNullOrEmpty(name))
			{
				using (Stream stream = GetStreamFromName(name))
				{
					if (stream == null) throw new ArgumentException("Could not read stream from embedded resource '" + name + "'", "name");

					using (StreamReader reader = new StreamReader(stream))
					{
						var mappingXml = reader.ReadToEnd();
						mappingXml = FormatMapping(mappingXml);

						var xmlReader = new XmlTextReader(mappingXml, XmlNodeType.Document, null);
						var mappingDocument = cfg.LoadMappingDocument(xmlReader, "N2");
						cfg.AddDeserializedMapping(mappingDocument.Document, mappingDocument.Name);
					}
				}
			}
		}
		protected virtual void AddDefaultMapping(NHibernate.Cfg.Configuration cfg)
		{
			ModelMapper mm = new ModelMapper();

			mm.Class<ContentItem>(ContentItemCustomization);
			mm.Class<ContentDetail>(ContentDetailCustomization);
			mm.Class<DetailCollection>(DetailCollectionCustomization);
			mm.Class<AuthorizedRole>(AuthorizedRoleCustomization);
            mm.Class<ContentVersion>(ContentVersionCustomization);

			var compiledMapping = mm.CompileMappingForAllExplicitlyAddedEntities();
			cfg.AddDeserializedMapping(compiledMapping, "N2");
		}