Esempio n. 1
0
        public static IZooDatabase FromFile(IRandomizer randomizer, IAnimalNamesDatabase animalNamesDatabase, string filename, bool createIfNotExist = true)
        {
            var database = new ZooDatabase(randomizer, animalNamesDatabase, filename);

            if (createIfNotExist &&
                !File.Exists(filename))
            {
                database.Save(); // force generate an empty database file
                return(database);
            }

            var doc = new XmlDocument();

            doc.Load(filename);

            foreach (XmlNode node in doc.SelectNodes("Zoo/Animals/Animal"))
            {
                // attempt to find the animal type by full name
                var type = Assembly.GetExecutingAssembly().GetType(node.Attributes["Type"].InnerText);
                if (type != null)
                {
                    var id   = Guid.Parse(node.Attributes["Id"].InnerText);
                    var name = node.Attributes["Name"].InnerText;
                    var age  = new TimeSpan(long.Parse(node.Attributes["Age"].InnerText));

                    // construct an instance of the animal
                    var animal = ConstructAnimal(type, id, name, age);

                    // add animal to database (not saved yet)
                    database._animals.Add(animal.Id, animal);
                }
            }

            return(database);
        }
Esempio n. 2
0
 private ZooDatabase(IRandomizer randomizer, IAnimalNamesDatabase animalNamesDatabase, string filename)
 {
     _randomizer          = randomizer;
     _animalNamesDatabase = animalNamesDatabase;
     _filename            = filename;
 }