public IEnumerable <MappedEntity> GetEntities(XPathNavigator parentNode, EntityMapping entityMapping) { var entityNodes = parentNode.Select(entityMapping.Root, _namespaceResolver); foreach (XPathNavigator entityNode in entityNodes) { MappedEntity entity = GetEntity(entityNode, entityMapping); yield return(entity); EntityMapping[] entityMappings = entityMapping.Entity; if (entityMappings == null || entityMappings.Length == 0) { continue; } // Recursive call of this method to get child entities. IEnumerable <MappedEntity> subEntities = entityMappings.SelectMany(em => GetEntities(entityNode, em)); foreach (var subEntity in subEntities) { yield return(subEntity); } } }
public static void GetTableAndSchemaNameByReflection(MappedEntity entity) { var configMember = entity.MapInstance.GetType().GetRuntimeProperties().FirstOrDefault(c => c.Name == "Configuration"); var tableNameProp = entity.MapInstance.GetType().GetProperty("TableName").GetValue(entity.MapInstance); if (tableNameProp != null) { entity.TableName = tableNameProp.ToString(); } else if (configMember != null) { var config = configMember.GetValue(entity.MapInstance); var tableProp = config.GetType().GetProperty("TableName").GetValue(config); if (tableProp != null) { entity.TableName = tableProp.ToString(); } } var schemaProp = entity.MapInstance.GetType().GetProperty("SchemaName").GetValue(entity.MapInstance); if (schemaProp == null && configMember != null) { var config = configMember.GetValue(entity.MapInstance); schemaProp = config.GetType().GetProperty("SchemaName").GetValue(config); } entity.Schema = schemaProp != null?schemaProp.ToString() : "dbo"; }
private IEnumerable <object> GetConstructorDependancy(MappedEntity mappedEntity) { var entityConstructor = mappedEntity.ConcreteType.GetConstructors().First(); foreach (var parameter in entityConstructor.GetParameters()) { yield return(ResolveEntity(parameter.ParameterType)); } }
private object GetInstance(MappedEntity mappedEntity) { if (mappedEntity.Instance == null || mappedEntity.LifeCycle == LifeCycleType.Transiet) { var dependancies = GetConstructorDependancy(mappedEntity); mappedEntity.CreateInstance(dependancies.ToArray()); } return(mappedEntity.Instance); }
public MappedEntity GetEntity(XPathNavigator parentNode, EntityMapping entityMapping) { var mappedEntity = new MappedEntity(); mappedEntity.EntityType = entityMapping.EntityType; mappedEntity.Fields = GetFields(parentNode, entityMapping.Fields); mappedEntity.FieldSet = GetFieldSet(parentNode, entityMapping); mappedEntity.Links = GetLinks(parentNode, entityMapping); mappedEntity.UniqueFieldType = entityMapping.UniqueFieldType; return(mappedEntity); }
public static List <MappedEntity> MappedTypesWithDatabaseInfo(this Assembly assembly, string namespaceFilter, string objectNameFilter, string database, string inheritsFromType, string implementsInterface) { var types = assembly.GetTypes(); var mappedTypes = new List <MappedEntity>(); foreach (var type in types) { var type0 = type; while (type0.BaseType != null) { if (type0.BaseType.Name == "DbContext" && !type.IsAbstract && type.GetConstructors().Any(cc => !cc.GetParameters().Any())) { try { var dbContext = Activator.CreateInstance(type) as DbContext; var contextName = type.FullName; var connectionString = dbContext.GetType().GetRuntimeProperty("ConnectionString").GetValue(dbContext); var modelBuilder = new DbModelBuilder(); var modelCreateAction = dbContext.GetType().GetRuntimeMethods().First(c => c.Name == "OnModelCreating"); modelCreateAction.Invoke(dbContext, new[] { modelBuilder }); var modelConfiguration = modelBuilder.Configurations.GetType().GetRuntimeFields().First() .GetValue(modelBuilder.Configurations); var mappingInstances = modelConfiguration.GetType().GetRuntimeProperties().First(c => c.Name == "ActiveEntityConfigurations") .GetValue(modelConfiguration) as ICollection; foreach (var mappingInstance in mappingInstances) { var mappedType = new MappedEntity(mappingInstance); GetMappingInfoForType(mappedType, connectionString.ToString()); if (applyFilter(mappedType, namespaceFilter, objectNameFilter, database, inheritsFromType, implementsInterface)) { mappedTypes.Add(mappedType); } } } catch { } break; } type0 = type0.BaseType; } } return(mappedTypes); }
private static bool applyFilter(MappedEntity mappedType, string namespaceFilter, string objectNameFilter, string database, string inheritsFromType, string implementsInterface) { if (namespaceFilter != null && !mappedType.Namespace.Contains(namespaceFilter)) { return(false); } if (objectNameFilter != null && !mappedType.ObjectName.Contains(objectNameFilter)) { return(false); } if (database != null && !mappedType.DatabaseName.Contains(database)) { return(false); } if (!string.IsNullOrEmpty(implementsInterface) && !mappedType.EntityType.ImplementsInterface(implementsInterface)) { return(false); } if (!string.IsNullOrEmpty(inheritsFromType) && !mappedType.EntityType.InheritsFrom(inheritsFromType)) { return(false); } return(true); }
public static void GetMappingInfoForType(MappedEntity entityType, string databaseName) { GetTableAndSchemaNameByReflection(entityType); entityType.DatabaseName = databaseName; }