public static IEnumerable<Entity> GetEntities(string fileName)
		{
			using (var reader = new BackupReader(fileName))
			{
				List<Entity> entities = new List<Entity>();
				var map = new Dictionary<Type, Dictionary<int, Entity>>();
				var foreignKeys = new List<Tuple<EntityPropertyInfo, Action<object>, int>>();

				var entityTypes = GetEntityTypes();
				Entity entity = null;
				EntityInfo entityInfo = null;

				foreach (Line line in reader.GetLines().Select(s => new Line(s)))
				{
					if (line.Key == "$ENTITY")
					{
						if (!String.IsNullOrEmpty(line.Value) && entityTypes.TryGetValue(line.Value, out entityInfo))
							entity = (Entity)Activator.CreateInstance(entityInfo.EntityType);
					}
					else if (line.Key == "$$" && entity != null)
					{
						if (!map.ContainsKey(entityInfo.EntityType))
							map[entityInfo.EntityType] = new Dictionary<int, Entity>();
						map[entityInfo.EntityType][entity.Id] = entity;

						entities.Add(entity);

						entity = null;
					}
					else if (entity != null && line.Value != null)
					{
						EntityPropertyInfo property;
						if (entityInfo.Properties.TryGetValue(line.Key, out property))
						{
							if (typeof(Entity).IsAssignableFrom(property.PropertyType))
							{
								Entity cEntity = entity;
								foreignKeys.Add(Tuple.Create(property, new Action<object>(v => property.SetValue(cEntity, v)), int.Parse(line.Value)));
							}
							else
								property.SetValue(entity, line.Value);
						}
					}
				}

				foreach (Tuple<EntityPropertyInfo, Action<object>, int> link in foreignKeys)
				{
					Dictionary<int, Entity> mapById;
					Entity linkedEntity;
					if (map.TryGetValue(link.Item1.PropertyType, out mapById) && mapById.TryGetValue(link.Item3, out linkedEntity))
						link.Item2(linkedEntity);
					else if (link.Item1.Converter.GetType() != EntityPropertyAttribute.DefaultConverter)
					{
						try
						{
							link.Item2(link.Item3); // try to pass original ID to the converter
						}
						catch (InvalidCastException)
						{
						}
					}
				}

				return entities;
			}
		}
Esempio n. 2
0
        public static IEnumerable <Entity> GetEntities(string fileName)
        {
            using (var reader = new BackupReader(fileName))
            {
                List <Entity> entities    = new List <Entity>();
                var           map         = new Dictionary <Type, Dictionary <int, Entity> >();
                var           foreignKeys = new List <Tuple <EntityPropertyInfo, Action <object>, int> >();

                var        entityTypes = GetEntityTypes();
                Entity     entity      = null;
                EntityInfo entityInfo  = null;

                foreach (Line line in reader.GetLines().Select(s => new Line(s)))
                {
                    if (line.Key == "$ENTITY")
                    {
                        if (!String.IsNullOrEmpty(line.Value) && entityTypes.TryGetValue(line.Value, out entityInfo))
                        {
                            entity = (Entity)Activator.CreateInstance(entityInfo.EntityType);
                        }
                    }
                    else if (line.Key == "$$" && entity != null)
                    {
                        if (!map.ContainsKey(entityInfo.EntityType))
                        {
                            map[entityInfo.EntityType] = new Dictionary <int, Entity>();
                        }
                        map[entityInfo.EntityType][entity.Id] = entity;

                        entities.Add(entity);

                        entity = null;
                    }
                    else if (entity != null && line.Value != null)
                    {
                        EntityPropertyInfo property;
                        if (entityInfo.Properties.TryGetValue(line.Key, out property))
                        {
                            if (typeof(Entity).IsAssignableFrom(property.PropertyType))
                            {
                                Entity cEntity = entity;
                                foreignKeys.Add(Tuple.Create(property, new Action <object>(v => property.SetValue(cEntity, v)), int.Parse(line.Value)));
                            }
                            else
                            {
                                property.SetValue(entity, line.Value);
                            }
                        }
                    }
                }

                foreach (Tuple <EntityPropertyInfo, Action <object>, int> link in foreignKeys)
                {
                    Dictionary <int, Entity> mapById;
                    Entity linkedEntity;
                    if (map.TryGetValue(link.Item1.PropertyType, out mapById) && mapById.TryGetValue(link.Item3, out linkedEntity))
                    {
                        link.Item2(linkedEntity);
                    }
                    else if (link.Item1.Converter.GetType() != EntityPropertyAttribute.DefaultConverter)
                    {
                        try
                        {
                            link.Item2(link.Item3);                             // try to pass original ID to the converter
                        }
                        catch (InvalidCastException)
                        {
                        }
                    }
                }

                return(entities);
            }
        }