Beispiel #1
0
 public static void RegisterBuilders(this ISecurityObjectRepository securityObjectRepository, IEnumerable <SecurityObjectBuilder> builders)
 {
     foreach (SecurityObjectBuilder builder in builders)
     {
         securityObjectRepository.RegisterBuilder(builder);
     }
 }
        private IEnumerable <SecurityObjectBuilder> PrepareAddedObjects(IEnumerable <EntityEntry> entitiesEntry)
        {
            List <SecurityObjectBuilder> securityObjectBuilders = new List <SecurityObjectBuilder>();

            foreach (EntityEntry entityEntry in entitiesEntry)
            {
                SecurityObjectBuilder securityObjectMetaData = securityObjectRepository.GetObjectMetaData(entityEntry.Entity);
                if (securityObjectMetaData == null)
                {
                    securityObjectMetaData = new SecurityObjectBuilder();
                    securityObjectMetaData.SecurityObject = entityEntry.Entity;
                    securityObjectRepository.RegisterBuilder(securityObjectMetaData);
                    securityObjectMetaData.CreateRealObject(securityDbContext.Model, securityObjectRepository);
                }
                securityObjectBuilders.Add(securityObjectMetaData);
            }
            return(securityObjectBuilders);
        }
 private void ApplyModification(IEnumerable <EntityEntry> entitiesEntry, IEnumerable <ModifiedObjectMetadata> modifyObjectsMetada)
 {
     foreach (var entityEntry in entitiesEntry)
     {
         ModifiedObjectMetadata modifyObjectMetada     = modifyObjectsMetada.First(p => p.Object == entityEntry.Entity);
         SecurityObjectBuilder  securityObjectMetaData = securityObjectRepository.GetObjectMetaData(entityEntry.Entity);
         if (securityObjectMetaData == null)
         {
             securityObjectMetaData                = new SecurityObjectBuilder();
             securityObjectMetaData.RealObject     = securityDbContext.RealDbContext.GetObject(entityEntry.Entity);
             securityObjectMetaData.SecurityObject = entityEntry.Entity;
             securityObjectRepository.RegisterBuilder(securityObjectMetaData);
         }
         ApplyModyfication(securityObjectMetaData.RealObject, modifyObjectMetada);
     }
 }
        public object CreateRealObject(IModel model, ISecurityObjectRepository securityObjectRepository)
        {
            Type targetType = SecurityObject.GetType();

            RealObject = Activator.CreateInstance(SecurityObject.GetType());
            IEntityType entityType = model.FindEntityType(targetType);
            IEnumerable <PropertyInfo> properiesInfo = targetType.GetRuntimeProperties();
            IEnumerable <INavigation>  navigations   = entityType.GetNavigations();

            IReadOnlyList <IProperty> primaryKeyProperties = entityType.FindPrimaryKey().Properties;

            foreach (PropertyInfo propertyInfo in properiesInfo)
            {
                object defaultValue = propertyInfo.GetValue(RealObject);
                defaultValueDictionary[propertyInfo.Name] = defaultValue;
                if (navigations.Any(p => p.Name == propertyInfo.Name))
                {
                    INavigation navigation = navigations.First(p => p.Name == propertyInfo.Name);
                    if (navigation.IsCollection())
                    {
                        IClrCollectionAccessor collectionAccessor              = navigation.GetCollectionAccessor();
                        IEnumerable            realObjectListPropertyValue     = (IEnumerable)propertyInfo.GetValue(RealObject);
                        IEnumerable            securityObjectListPropertyValue = (IEnumerable)propertyInfo.GetValue(SecurityObject);
                        if (securityObjectListPropertyValue != null && realObjectListPropertyValue != null)
                        {
                            foreach (object objectInListProperty in securityObjectListPropertyValue)
                            {
                                SecurityObjectBuilder metadata = securityObjectRepository.GetObjectMetaData(objectInListProperty);
                                if (metadata == null)
                                {
                                    metadata = new SecurityObjectBuilder();
                                    securityObjectRepository.RegisterBuilder(metadata);
                                    metadata.SecurityObject = objectInListProperty;
                                    metadata.CreateRealObject(model, securityObjectRepository);
                                }
                                collectionAccessor.Add(RealObject, metadata.RealObject);
                            }
                        }
                    }
                    else
                    {
                        object realValue = propertyInfo.GetValue(SecurityObject);
                        if (!Equals(realValue, null))
                        {
                            SecurityObjectBuilder metadata = securityObjectRepository.GetObjectMetaData(realValue);
                            if (metadata == null)
                            {
                                metadata = new SecurityObjectBuilder();
                                securityObjectRepository.RegisterBuilder(metadata);
                                metadata.SecurityObject = realValue;

                                metadata.CreateRealObject(model, securityObjectRepository);
                            }
                            if (propertyInfo.SetMethod != null)
                            {
                                propertyInfo.SetValue(RealObject, metadata.RealObject);
                            }
                        }
                    }
                }
                else
                {
                    bool isGeneratedPrimaryKey = false;
                    foreach (IProperty primaryKeyProperty in primaryKeyProperties)
                    {
                        if ((propertyInfo.Name == primaryKeyProperty.Name) && primaryKeyProperty.RequiresValueGenerator)
                        {
                            isGeneratedPrimaryKey = true;
                        }
                    }
                    if (propertyInfo.SetMethod != null && !isGeneratedPrimaryKey)
                    {
                        object securityValue = propertyInfo.GetValue(SecurityObject);
                        propertyInfo.SetValue(RealObject, securityValue);
                    }
                }
            }
            return(RealObject);
        }