예제 #1
0
 private void InitializeCustomFields(DynamicTypeDefinition definition)
 {
     foreach (var field in definition.DynamicProperties)
     {
         var t = Type.GetType(field.PropertyTypeName);
         CustomFieldData[field.PropertyName] = t.GetDefault()?.ToString();
     }
 }
예제 #2
0
 public DynamicTypeCodeGenerator(string @namespace, string typeName, DynamicBaseType baseType, DynamicTypeDefinition def, HashSet <string> usings)
 {
     this.Usings    = usings;
     this.Namespace = @namespace;
     this.TypeName  = typeName;
     this.BaseType  = baseType;
     this.Def       = def;
 }
        private void CreateNavigationProperties(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, String tableName)
        {
            foreach (String propertyName in MetadataProvider.GetNavigationProperties(tableName))
            {
                DynamicDependentPropertyInfo dependentInfo = MetadataProvider.GetDependentProperties(tableName, propertyName);

                EntityType dependentEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.DependentEntityName), false);
                EntityType principalEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.PrincipalEntityName), false);

                var dependentProperties = new List <Property>();
                foreach (String dependentPropertyName in dependentInfo.DependentPropertyNames)
                {
                    dependentProperties.Add((Property)dependentEntityType.GetProperty(dependentPropertyName));
                }

                ForeignKey fkey = dependentEntityType.FindForeignKey(dependentProperties, principalEntityType.FindPrimaryKey(), principalEntityType);
                if (fkey == null)
                {
                    var principalProperties = new List <Property>();
                    foreach (String principalPropertyName in dependentInfo.PrincipalPropertyNames)
                    {
                        principalProperties.Add((Property)principalEntityType.GetProperty(principalPropertyName));
                    }

                    Key pkey = principalEntityType.FindKey(principalProperties);
                    if (pkey == null)
                    {
                        pkey = principalEntityType.AddKey(principalProperties);
                    }

                    fkey = dependentEntityType.AddForeignKey(dependentProperties, pkey, principalEntityType);
                }

                DynamicTypeDefinition dynamicTypeDefinition = TypeDefinitionManager.GetDynamicTypeDefinition(tableName);
                if (dependentInfo.IsCollection)
                {
                    Navigation navigation = fkey.HasPrincipalToDependent(propertyName);
                    navigation.SetField(dynamicTypeDefinition.GetCollectionFiledName(propertyName));
                }
                else
                {
                    Navigation navigation = fkey.HasDependentToPrincipal(propertyName);
                    navigation.SetField(dynamicTypeDefinition.GetSingleFiledName(propertyName));
                }
            }
        }
        public DynamicTypeDefinition Resolve(Guid uid)
        {
            if (_definitions.ContainsKey(uid))
            {
                return(_definitions[uid]);
            }

            var typeExtensions = new ContentInterfaceRepository().Find().Where(x => x.ContentTypeId == uid);
            var metadata       = new ContentTypeMetadataRepository().Find().Where(x => x.TypeResolverId == uid).SingleOrDefault();
            var dtd            = new DynamicTypeDefinition();

            dtd.TitleProperty = metadata?.TitleProperty;
            foreach (var extension in typeExtensions)
            {
                dtd.DynamicProperties.AddRange(extension.InterfaceFields);
            }

            _definitions.Add(uid, dtd);
            return(_definitions[uid]);
        }
        protected override OeShadowPropertyInfo CreateShadowProperty(IPropertyBase efProperty)
        {
            if (efProperty is INavigation efNavigation)
            {
                Type propertyType = efNavigation.IsDependentToPrincipal() ?
                                    efNavigation.ForeignKey.PrincipalEntityType.ClrType : efNavigation.ForeignKey.DeclaringEntityType.ClrType;

                if (efNavigation.IsCollection())
                {
                    propertyType = typeof(ICollection <>).MakeGenericType(new Type[] { propertyType });
                }

                return(new OeShadowPropertyInfo(efNavigation.DeclaringType.ClrType, propertyType, efNavigation.Name));
            }

            DynamicTypeDefinition typeDefinition = _typeDefinitionManager.GetDynamicTypeDefinition(efProperty.DeclaringType.ClrType);
            MethodInfo            getMethodInfo  = typeDefinition.AddShadowPropertyGetMethodInfo(efProperty.Name, efProperty.ClrType);

            return(new OeShadowPropertyInfo(efProperty.DeclaringType.ClrType, efProperty.ClrType, efProperty.Name, getMethodInfo));
        }
예제 #6
0
 protected DynamicType(DynamicTypeDefinition dynamicTypeDefinition)
 {
     _dynamicTypeDefinition = dynamicTypeDefinition;
 }
예제 #7
0
 protected DynamicType(DynamicDbContext dynamicDbContext)
 {
     _dynamicTypeDefinition = dynamicDbContext.TypeDefinitionManager.GetDynamicTypeDefinition(base.GetType());
 }
예제 #8
0
 public void SetDefinition(DynamicTypeDefinition definition)
 {
     this.TypeDefinition = JsonSerializer.Serialize(definition, settings);
     this.definition     = definition;
 }