Ejemplo n.º 1
0
        private void BeforeBindMapping(object sender, BindMappingEventArgs e)
        {
            HbmProperty prop = e.Mapping.RootClasses[0].Properties.OfType <HbmProperty>().Single(p => p.Name == "NotNullData");

            prop.notnull          = true;
            prop.notnullSpecified = true;
        }
        public void Execute(object sender, BindMappingEventArgs e)
        {
            if (IsEdFiQueryMappingEvent(e))
            {
                return;
            }

            foreach (var classMapping in e.Mapping.Items.OfType <HbmClass>())
            {
                // Entities mapped with <version> are aggregate roots.
                if (classMapping.Version == null)
                {
                    continue;
                }

                // Maps the ChangeVersion column dynamically
                // Requires there be a property on the base entity already
                // nHibernate wraps property getter exception in PropertyAccessException if any
                // underlying mapped properties are set to access "none", due to an invoke exception being triggered.
                // generated = "always" to avoid nHibernate trying to set values for it
                // <property name="ChangeVersion" column="ChangeVersion" type="long" not-null="true" generated="always" />
                var changeVersionProperty = new HbmProperty
                {
                    name   = ChangeQueriesDatabaseConstants.ChangeVersionColumnName,
                    column = ChangeQueriesDatabaseConstants.ChangeVersionColumnName,
                    type   = new HbmType
                    {
                        name = "long"
                    },
                    notnull   = true,
                    generated = HbmPropertyGeneration.Always
                };
                classMapping.Items = classMapping.Items.Concat(changeVersionProperty).ToArray();
            }
        }
    static void PrefixMapping(BindMappingEventArgs e, string prefix)
    {
        var c = e.Mapping.RootClasses.Single();

        c.table = prefix + (c.table ?? c.Name);
        foreach (var prop in c.Properties.OfType <HbmProperty>())
        {
            foreach (var column in prop.Columns)
            {
                if (!string.IsNullOrEmpty(column.index))
                {
                    column.index = prefix + column.index;
                }
            }
        }
    }
        private void Configuration_BeforeBindMapping(object sender, BindMappingEventArgs e)
        {
            // When core mapping file loaded, attach any extensions to their core entity counterpart
            if (IsEdFiStandardMappingEvent(e))
            {
                var classMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                               .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x);

                var joinedSubclassMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                                        .SelectMany(i => i.JoinedSubclasses)
                                                        .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x);

                var subclassJoinMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                                      .SelectMany(i => i.Subclasses)
                                                      .Where(sc => sc.Joins.Count() == 1)
                                                      .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x.Joins.Single());

                MapExtensionsToCoreEntity(
                    classMappingByEntityName, joinedSubclassMappingByEntityName, subclassJoinMappingByEntityName);

                MapJoinedSubclassesToCoreEntity(
                    classMappingByEntityName, joinedSubclassMappingByEntityName, subclassJoinMappingByEntityName);

                MapDescriptorToCoreDescriptorEntity(classMappingByEntityName);

                MapDerivedEntityToCoreEntity(classMappingByEntityName);
            }

            foreach (var beforeBindMappingActivity in _beforeBindMappingActivities)
            {
                beforeBindMappingActivity.Execute(sender, e);
            }
        }
 private static bool IsEdFiStandardMappingEvent(BindMappingEventArgs e)
 {
     return([email protected](Namespaces.Entities.NHibernate.BaseNamespace) &&
            e.Mapping.assembly.Equals(Namespaces.Standard.BaseNamespace));
 }
Ejemplo n.º 6
0
 private static void OnBeforeBindMapping(object sender, BindMappingEventArgs bindMappingEventArgs)
 {
     // Force using the fully qualified type name instead of just the class name.
     // This will get rid of any duplicate mapping/class name issues.
     bindMappingEventArgs.Mapping.autoimport = false;
 }
 private static bool IsEdFiQueryMappingEvent(BindMappingEventArgs e)
 {
     return(([email protected](Namespaces.Entities.NHibernate.QueryModels.BaseNamespace) ||
             [email protected](Namespaces.Entities.NHibernate.QueryModels.Views)) &&
            e.Mapping.assembly.Equals(Namespaces.Standard.BaseNamespace));
 }
Ejemplo n.º 8
0
 /// <summary>
 ///     强制使用完全限定名
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private static void Configuration_BeforeBindMapping(object sender, BindMappingEventArgs e)
 {
     e.Mapping.autoimport = false;
 }
Ejemplo n.º 9
0
        private void Configuration_BeforeBindMapping(object sender, BindMappingEventArgs e)
        {
            // When core mapping file loaded, attach any extensions to their core entity counterpart
            if (IsEdFiStandardMappingEvent())
            {
                var classMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                               .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x);

                var joinedSubclassMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                                        .SelectMany(i => i.JoinedSubclasses)
                                                        .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x);

                var subclassJoinMappingByEntityName = e.Mapping.Items.OfType <HbmClass>()
                                                      .SelectMany(i => i.Subclasses)
                                                      .Where(sc => sc.Joins.Count() == 1)
                                                      .ToDictionary(
                    x => x.Name.Split('.')
                    .Last(),
                    x => x.Joins.Single());

                MapExtensionsToCoreEntity(
                    classMappingByEntityName, joinedSubclassMappingByEntityName, subclassJoinMappingByEntityName);

                MapJoinedSubclassesToCoreEntity(
                    classMappingByEntityName, joinedSubclassMappingByEntityName, subclassJoinMappingByEntityName);

                MapDescriptorToCoreDescriptorEntity(classMappingByEntityName);

                MapDerivedEntityToCoreEntity(classMappingByEntityName);
            }

            foreach (var beforeBindMappingActivity in _beforeBindMappingActivities)
            {
                beforeBindMappingActivity.Execute(sender, e);
            }

            void MapDerivedEntityToCoreEntity(Dictionary <string, HbmClass> classMappingByEntityName)
            {
                foreach (string entityName in _extensionDerivedEntityByEntityName.Keys)
                {
                    if (!classMappingByEntityName.TryGetValue(entityName, out HbmClass classMapping))
                    {
                        throw new MappingException(
                                  $"The subclass extension to entity '{entityName}' could not be applied because the class mapping could not be found.");
                    }

                    var hbmSubclasses = _extensionDerivedEntityByEntityName[entityName].Select(x => (object)x).ToArray();

                    classMapping.Items1 = (classMapping.Items1 ?? new object[0]).Concat(hbmSubclasses).ToArray();
                }
            }

            void MapDescriptorToCoreDescriptorEntity(Dictionary <string, HbmClass> classMappingByEntityName)
            {
                // foreach entity name, look in core mapping file (e.mapping) for core entity mapping and if found
                // concat new extension HbmJoinedSubclass to current set of Ed-Fi entity HbmJoinedSubclasses.
                foreach (string entityName in _extensionDescriptorByEntityName.Keys)
                {
                    if (!classMappingByEntityName.TryGetValue(entityName, out HbmClass classMapping))
                    {
                        throw new MappingException(
                                  $"The subclass extension to entity '{entityName}' could not be applied because the class mapping could not be found.");
                    }

                    var hbmJoinedSubclasses = _extensionDescriptorByEntityName[entityName]
                                              .Select(x => (object)x)
                                              .ToArray();

                    classMapping.Items1 = (classMapping.Items1 ?? new object[0]).Concat(hbmJoinedSubclasses)
                                          .ToArray();
                }
            }

            void MapJoinedSubclassesToCoreEntity(Dictionary <string, HbmClass> classMappingByEntityName,
                                                 Dictionary <string, HbmJoinedSubclass> joinedSubclassMappingByEntityName,
                                                 Dictionary <string, HbmJoin> subclassJoinMappingByEntityName)
            {
                // foreach entity name, look in core mapping file (e.mapping) for core entity mapping and if found
                // concat new extension HbmDynamicComponent to current set of items.
                foreach (string entityName in _aggregateExtensionHbmBagsByEntityName.Keys)
                {
                    HbmJoinedSubclass joinedSubclassMapping = null;
                    HbmJoin           subclassJoinMapping   = null;

                    if (!classMappingByEntityName.TryGetValue(entityName, out HbmClass classMapping) &&
                        !joinedSubclassMappingByEntityName.TryGetValue(entityName, out joinedSubclassMapping) &&
                        !subclassJoinMappingByEntityName.TryGetValue(entityName, out subclassJoinMapping))
                    {
                        throw new MappingException(
                                  $"The aggregate extensions to entity '{entityName}' could not be applied because the class mapping could not be found.");
                    }

                    var extensionComponent = new HbmDynamicComponent
                    {
                        name  = AggregateExtensionMemberName,
                        Items = _aggregateExtensionHbmBagsByEntityName[entityName]
                                .Select(x => (object)x)
                                .ToArray()
                    };

                    if (classMapping != null)
                    {
                        classMapping.Items = classMapping.Items.Concat(extensionComponent)
                                             .ToArray();
                    }
                    else if (joinedSubclassMapping != null)
                    {
                        joinedSubclassMapping.Items = joinedSubclassMapping.Items.Concat(extensionComponent)
                                                      .ToArray();
                    }
                    else if (subclassJoinMapping != null)
                    {
                        subclassJoinMapping.Items = subclassJoinMapping.Items.Concat(extensionComponent)
                                                    .ToArray();
                    }
                }
            }

            void MapExtensionsToCoreEntity(Dictionary <string, HbmClass> classMappingByEntityName,
                                           Dictionary <string, HbmJoinedSubclass> joinedSubclassMappingByEntityName,
                                           Dictionary <string, HbmJoin> subclassJoinMappingByEntityName)
            {
                // foreach entity name, look in core mapping file (e.mapping) for core entity mapping and if found
                // concat new extension HbmDynamicComponent to current set of items.
                foreach (string entityName in _entityExtensionHbmBagsByEntityName.Keys)
                {
                    HbmJoinedSubclass joinedSubclassMapping = null;
                    HbmJoin           subclassJoinMapping   = null;

                    if (!classMappingByEntityName.TryGetValue(entityName, out HbmClass classMapping) &&
                        !joinedSubclassMappingByEntityName.TryGetValue(entityName, out joinedSubclassMapping) &&
                        !subclassJoinMappingByEntityName.TryGetValue(entityName, out subclassJoinMapping))
                    {
                        throw new MappingException(
                                  $"The entity extension to entity '{entityName}' could not be applied because the class mapping could not be found.");
                    }

                    var extensionComponent = new HbmDynamicComponent
                    {
                        name  = EntityExtensionMemberName,
                        Items = _entityExtensionHbmBagsByEntityName[entityName]
                                .Select(x => (object)x)
                                .ToArray()
                    };

                    if (classMapping != null)
                    {
                        classMapping.Items = classMapping.Items.Concat(extensionComponent)
                                             .ToArray();
                    }
                    else if (joinedSubclassMapping != null)
                    {
                        joinedSubclassMapping.Items = joinedSubclassMapping.Items.Concat(extensionComponent)
                                                      .ToArray();
                    }
                    else if (subclassJoinMapping != null)
                    {
                        subclassJoinMapping.Items = subclassJoinMapping.Items.Concat(extensionComponent)
                                                    .ToArray();
                    }
                }
            }

            bool IsEdFiStandardMappingEvent()
            {
                return([email protected](Namespaces.Entities.NHibernate.BaseNamespace) &&
                       e.Mapping.assembly.Equals(Namespaces.Standard.BaseNamespace));
            }
        }