コード例 #1
0
        private void OnFilledType(MigratorFilledTypeRelationsEventArgs filledEventArgs)
        {
            if (FilledTypeRelations == null)
            {
                return;
            }

            FilledTypeRelations(this, filledEventArgs);
        }
コード例 #2
0
        private void FillRelationsOfEntity(Type entityType, IObjectContainer container)
        {
            var relations = GetRelationsOfEntity(entityType);

            if (relations == null || relations.Count == 0)
            {
                return;
            }

            var fillingEventArgs = new MigratorFillingTypeRelationsEventArgs
            {
                TypeRelationsToFill = relations,
                EntityType          = entityType
            };

            OnFillingType(fillingEventArgs);

            if (fillingEventArgs.Cancel ||
                (fillingEventArgs.TypeRelationsToFill == null || fillingEventArgs.TypeRelationsToFill.Count == 0))
            {
                return;
            }

            relations = fillingEventArgs.TypeRelationsToFill;

            var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            var fields     = entityType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            var entityQuery = container.Query();

            entityQuery.Constrain(entityType);

            var entityList      = entityQuery.Execute().ToList <object>(container, 1);
            var entityListCount = entityList == null ? 0 : entityList.Count;

            if (entityListCount == 0)
            {
                return;
            }

            var entityTempCount = 0;

            foreach (var entity in entityList)
            {
                foreach (var relation in relations)
                {
                    var propertyAttrib = relation.Key;
                    var relAttrib      = relation.Value;

                    if (!relAttrib.IsEntityParent)
                    {
                        #region FIND AND SET PARENT PROPERTY

                        var parentQuery = container.Query();
                        parentQuery.Constrain(propertyAttrib.PropertyType);


                        var colsCount = relAttrib.ParentColumnNames.Count();

                        for (var i = 0; i < colsCount; i++)
                        {
                            var propertyName    = relAttrib.PropertyNames[i];
                            var foreigFieldName = relAttrib.ForeignFieldNames[i];

                            var propertyInfo  = properties.Single(prop => prop.Name.Equals(propertyName));
                            var propertyValue = propertyInfo.GetValue(entity, null);

                            parentQuery.Descend(foreigFieldName).Constrain(propertyValue).Equal();
                        }

                        var parent = parentQuery.Execute().ToList <object>(container, 1).SingleOrDefault();

                        if (parent == null)
                        {
                            continue;
                        }

                        propertyAttrib.SetValue(entity, parent, null);

                        #endregion
                    }
                    else
                    {
                        #region FIND CHILD PROPERTIES

                        var collectionType  = propertyAttrib.PropertyType.GetGenericTypeDefinition();
                        var collectionField = fields.Single(fl => fl.Name.Equals(relAttrib.PrivateCollectionFieldName));
                        var childType       = propertyAttrib.PropertyType.GetGenericArguments()[0];

                        var childQuery = container.Query();

                        childQuery.Constrain(childType);

                        var colsCount = relAttrib.ChildColumnNames.Count();

                        for (var i = 0; i < colsCount; i++)
                        {
                            var propertyName    = relAttrib.PropertyNames[i];
                            var foreigFieldName = relAttrib.ForeignFieldNames[i];

                            var propertyInfo  = properties.Single(prop => prop.Name.Equals(propertyName));
                            var propertyValue = propertyInfo.GetValue(entity, null);

                            childQuery.Descend(foreigFieldName).Constrain(propertyValue).Equal();
                        }

                        var childs = childQuery.Execute().ToList <object>(container, 1);

                        if (childs == null || childs.Count == 0)
                        {
                            continue;
                        }

                        var constructedType = collectionType.MakeGenericType(childType);
                        var childCollection = Activator.CreateInstance(constructedType);
                        var addMehod        = childCollection.GetType().GetMethod("Add");

                        foreach (var child in childs)
                        {
                            addMehod.Invoke(childCollection, new[] { child });
                        }

                        collectionField.SetValue(entity, childCollection);

                        #endregion
                    }
                }

                entityTempCount++;

                if (entityTempCount != EntitiesCommitLimit)
                {
                    continue;
                }

                container.Commit();
                entityTempCount = 0;
            }

            if (entityTempCount > 0)
            {
                container.Commit();
            }

            var filledEventArgs = new MigratorFilledTypeRelationsEventArgs
            {
                EntityType = entityType
            };

            OnFilledType(filledEventArgs);
        }