Пример #1
0
        protected void LoadFile()
        {
            if (File.Exists($@"{Environment.CurrentDirectory}\{fileName}"))
            {
                XmlSerializer serializer = null;
                TextReader    reader     = null;

                try
                {
                    serializer = new XmlSerializer(this.GetType());
                    reader     = new StreamReader(fileName);

                    DBDataContext db =
                        (DBDataContext)serializer.Deserialize(reader);

                    this.Species          = db.Species;
                    this.Addresses        = db.Addresses;
                    this.UserTypes        = db.UserTypes;
                    this.Users            = db.Users;
                    this.Pets             = db.Pets;
                    this.Clients          = db.Clients;
                    this.Appointments     = db.Appointments;
                    this.Doctors          = db.Doctors;
                    this.AppointmentTypes = db.AppointmentTypes;

                    Addresses.ForEach(item =>
                                      item.Client = Clients.FirstOrDefault(r => r.Id == item.IdClient));

                    Users.ForEach(item =>
                                  item.UserType = UserTypes.FirstOrDefault(r => r.Id == item.IdUserType));

                    Pets.ForEach(item =>
                    {
                        item.Specie = Species.FirstOrDefault(r => r.Id == item.IdSpecie);
                        item.Owner  = Clients.FirstOrDefault(r => r.Id == item.IdOwner);
                    });

                    Clients.ForEach(item =>
                    {
                        item.Pets      = Pets.Where(r => r.IdOwner == item.Id).ToList();
                        item.Addresses = Addresses.Where(r => r.IdClient == item.Id).ToList();
                    });

                    Appointments.ForEach(item =>
                    {
                        item.Pet             = Pets.FirstOrDefault(r => r.Id == item.IdPet);
                        item.Address         = Addresses.FirstOrDefault(r => r.Id == item.IdAddress);
                        item.Doctor          = Doctors.FirstOrDefault(r => r.Id == item.IdDoctor);
                        item.Client          = Clients.FirstOrDefault(r => r.Id == item.Pet.IdOwner);
                        item.AppointmentType = AppointmentTypes.FirstOrDefault(r => r.Id == item.IdAppointmentType);
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (serializer != null)
                    {
                        GC.SuppressFinalize(serializer);
                    }
                }
            }
        }
Пример #2
0
        public static DBDataContext NewInstance(string fileName)
        {
            DBDataContext db = new DBDataContext(fileName);

            if (!File.Exists($@"{Environment.CurrentDirectory}\{fileName}"))
            {
                //Seeding the database with the basic user types
                db.UserTypes.Add(new UserType()
                {
                    Id = 1, Name = "Admin"
                });
                db.UserTypes.Add(new UserType()
                {
                    Id = 2, Name = "Staff"
                });

                //Seeding the table specie
                db.Species.Add(new Specie()
                {
                    Id = 1, Name = "Dog"
                });
                db.Species.Add(new Specie()
                {
                    Id = 2, Name = "Cat"
                });
                db.Species.Add(new Specie()
                {
                    Id = 3, Name = "Fish"
                });
                db.Species.Add(new Specie()
                {
                    Id = 4, Name = "Horse"
                });

                db.Users.Add(new User()
                {
                    Id = 1, Name = "Admin", Password = "******", Username = "******", IdUserType = 1
                });

                db.Addresses.Add(new Address()
                {
                    Id = -1, Line1 = "Clinic - 100 Fairway", Apartment = "23", City = "Kitchener", Province = Province.ON, PostalCode = "N2C 1X4"
                });
                db.AppointmentTypes.Add(new AppointmentType()
                {
                    Id = 1, Name = "Vaccine"
                });
                db.AppointmentTypes.Add(new AppointmentType()
                {
                    Id = 2, Name = "Surgery"
                });
                db.AppointmentTypes.Add(new AppointmentType()
                {
                    Id = 1, Name = "Therapeutic"
                });
                db.SubmitChanges();
            }
            else
            {
                db.LoadFile();
            }

            return(db);
        }
 internal RepositoryImplementation(string fileName)
 {
     DB = DBDataContext.NewInstance(fileName);
 }