Exemplo n.º 1
0
        private void DeserializeStars()
        {
            var xmlDocument = XDocument.Load(Constants.StarXmlFile).Root.Elements();

            using (PlanetHuntersContext ctx = new PlanetHuntersContext())
            {
                StarSystemRepo syRepo = new StarSystemRepo(ctx);
                StarRepo       stRepo = new StarRepo(ctx);

                foreach (var starElem in xmlDocument)
                {
                    StringReader  reader     = new StringReader(starElem.ToString());
                    XmlSerializer serializer = new XmlSerializer(typeof(StarDto));
                    var           starDto    = (StarDto)serializer.Deserialize(reader);

                    if (starDto.Temperature > 2400 && starDto.Name != null && starDto.StarSystem != null)
                    {
                        var starSystem = syRepo.SearchFor(s => s.Name == starDto.StarSystem).FirstOrDefault();
                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = starDto.StarSystem
                            }
                        }
                        ;

                        var star = new Star();
                        Mapper.Map(starDto, star);
                        star.HostStarSystem = starSystem;

                        stRepo.Insert(star);
                        ctx.SaveChanges();

                        this.DebugLog.AppendLine($"Record {star.Name} successfully imported.");
                    }
                    else
                    {
                        this.DebugLog.AppendLine(Constants.InvalidDataFormat);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void DeserializePlanets()
        {
            var planetDtos = JsonFileOperations.DeserializeJsonCollection <PlanetDto>(Constants.PlanetJsonFile);

            using (PlanetHuntersContext ctx = new PlanetHuntersContext())
            {
                StarSystemRepo stRepo = new StarSystemRepo(ctx);
                PlanetRepo     plRepo = new PlanetRepo(ctx);

                foreach (var dto in planetDtos)
                {
                    if (dto.Mass > 0 && dto.Name != null && dto.StarSystem != null)
                    {
                        var planet = new Planet();
                        Mapper.Map(dto, planet);

                        var starSystem = stRepo.SearchFor(s => s.Name == dto.StarSystem).FirstOrDefault();

                        if (starSystem == null)
                        {
                            starSystem = new StarSystem()
                            {
                                Name = dto.StarSystem
                            }
                        }
                        ;

                        starSystem.Planets.Add(planet);
                        planet.HostStarSystem = starSystem;

                        plRepo.Insert(planet);
                        ctx.SaveChanges();

                        this.DebugLog.AppendLine($"Record {dto.Name} successfully imported.");
                    }
                    else
                    {
                        this.DebugLog.AppendLine(Constants.InvalidDataFormat);
                    }
                }
            }
        }