/// <summary>	Creates a type builder. </summary>
 ///
 /// <param name="dynamicClassFactory">	The dynamic class factory. </param>
 /// <param name="name">				  	The name. </param>
 /// <param name="property">			  	The property. </param>
 ///
 /// <returns>	The new type builder. </returns>
 private static TypeBuilder CreateTypeBuilder(DynamicClassFactory dynamicClassFactory, string name, Dictionary<string, DynamicPropertyData> property)
 {
     var dynamicTypeBuilder = dynamicClassFactory.CreateDynamicTypeBuilder<DynamicEntity>(name, property);
     return dynamicTypeBuilder;
 }
Example #2
0
        /// <summary>	Creates a type builder. </summary>
        ///
        /// <param name="dynamicClassFactory">	The dynamic class factory. </param>
        /// <param name="name">				    The name. </param>
        /// <param name="property">			    The property. </param>
        ///
        /// <returns>	The new type builder. </returns>
        private static TypeBuilder CreateTypeBuilder(DynamicClassFactory dynamicClassFactory, string schema, string name, Dictionary <string, DynamicPropertyData> property)
        {
            var dynamicTypeBuilder = dynamicClassFactory.CreateDynamicTypeBuilder <DynamicEntity>(schema, name, property);

            return(dynamicTypeBuilder);
        }
Example #3
0
        public async Task Invoke(
            HttpContext context,
            MetadataHolder metadataHolder,
            TContext dbContext)
        {
            var metadataHolderFromJson = new { Version = "", Entities = new List <MetadataEntity>() }; ///LOAD your metadata from cache   await _distributedCacheService.GetAsync<MetadataHolder>("MetadataJson");

            if (metadataHolderFromJson == null)
            {
                throw new NullReferenceException("MetadataJson load failed.");
            }

            var isNewMetadataJson = false;

            if (string.IsNullOrEmpty(metadataHolder.Version))
            {
                isNewMetadataJson = true;
            }
            else if (metadataHolder.Version != metadataHolderFromJson.Version)
            {
                isNewMetadataJson = true;
            }

            if (isNewMetadataJson)
            {
                Dictionary <string, TypeBuilder> entityTypeBuilderList = new Dictionary <string, TypeBuilder>();

                metadataHolder.Entities = new List <MetadataEntity>();

                var dynamicClassFactory = new DynamicClassFactory();
                foreach (var metadataEntity in metadataHolderFromJson.Entities)
                {
                    var metadataProps = new Dictionary <string, Type>();
                    foreach (var metaDataEntityProp in metadataEntity.Properties.Where(p => !p.IsNavigation))
                    {
                        // TODO YASIN datetime vb eklenebilir.
                        switch (metaDataEntityProp.Type)
                        {
                        case "String":
                            metadataProps.Add(metaDataEntityProp.Name, typeof(string));
                            break;

                        case "Int":
                            metadataProps.Add(metaDataEntityProp.Name, typeof(int));
                            break;

                        case "Guid":
                            metadataProps.Add(metaDataEntityProp.Name, typeof(Guid));
                            break;

                        default:
                            //Implement for Other types
                            break;
                        }
                    }
                    if (string.IsNullOrEmpty(metadataEntity.CustomAssemblyType))
                    {
                        var entityTypeBuilder = dynamicClassFactory.CreateDynamicTypeBuilder <DynamicEntity>(metadataEntity.Name, metadataProps);

                        entityTypeBuilderList.Add(metadataEntity.Name, entityTypeBuilder);
                    }

                    else
                    {
                        metadataEntity.EntityType = Type.GetType(metadataEntity.CustomAssemblyType);
                    }


                    metadataHolder.Entities.Add(metadataEntity);
                }

                metadataHolder.Version = metadataHolderFromJson.Version;

                foreach (var metadataEntity in metadataHolder.Entities)
                {
                    var existEntityTypeBuilder = entityTypeBuilderList.FirstOrDefault(p => p.Key == metadataEntity.Name).Value;

                    foreach (var metaDataEntityProp in metadataEntity.Properties.Where(p => p.IsNavigation))
                    {
                        var relatedEntityTypeBuilder = entityTypeBuilderList.FirstOrDefault(p => p.Key == metaDataEntityProp.Type).Value;

                        var relatedProp = new Dictionary <string, Type>();

                        if (metaDataEntityProp.NavigationType == "Single")
                        {
                            relatedProp.Add(metaDataEntityProp.Name, relatedEntityTypeBuilder);
                        }
                        else
                        {
                            Type listGenericType = typeof(List <>);

                            Type listRelateEntityType = listGenericType.MakeGenericType(relatedEntityTypeBuilder);

                            relatedProp.Add(metaDataEntityProp.Name, listRelateEntityType);
                        }
                        new DynamicClassFactory().CreatePropertiesForTypeBuilder(existEntityTypeBuilder, relatedProp, null);
                    }
                    metadataEntity.EntityType = existEntityTypeBuilder.CreateType();
                }
            }

            foreach (var metaDataEntity in metadataHolder.Entities)
            {
                dbContext.AddMetadata(metaDataEntity);
            }

            dbContext.SetContextVersion(metadataHolder.Version);

            await _next.Invoke(context);
        }