/// <summary>
        /// IN REAL LIFE THIS METHOD SHOULD USE HIBERNATE TO SAVE PERSON ENTITY
        /// </summary>
        /// <returns></returns>
        public void SavePerson(Person p)
        {
            Person person = new Person();

            using (RepositoryBase repository = new RepositoryBase())
            {
                try 
                { 
                    repository.BeginTransaction();
                    person.Age = p.Age;
                    person.FirstName = p.FirstName;
                    person.LastName = p.LastName;

                    repository.Save(person);

                    person.City = new City { Name = p.City.Name, Id = p.City.Id };
                    person.Extra = new AdditionalInformation { DateOfBirth = p.Extra.DateOfBirth, PlaceOfBirth = p.Extra.PlaceOfBirth, Person_id = person.Id };
                
                    person.Certificates = new List<Certificate>();

                    foreach (Certificate newCertif in p.Certificates)
                    {
                        person.Certificates.Add(new Certificate { Name = newCertif.Name, Id = newCertif.Id });
                    }

                    person.Automobiles = new List<Automobile>();

                    foreach (Automobile newAuto in p.Automobiles)
                    {
                        person.Automobiles.Add(new Automobile { Description = newAuto.Description, Person_id = person.Id, Registration_number = newAuto.Registration_number });
                    }
                    repository.Save(person);
                
                    repository.CommitTransaction();

                    repository.CloseSession();

                }
                catch (Exception) 
                {
                    repository.RollbackTransaction();
                }
               
            }
        }
        /// <summary>
        /// IN REAL LIFE THIS METHOD SHOULD USE HIBERNATE TO RETRIEVE PERSON ENTITY
        /// </summary>
        /// <returns></returns>
        public Person GetPerson()
        {
            using (RepositoryBase repository = new RepositoryBase())
            {

                Person prof = repository.GetPerson();
                p = prof;
                
                p.PersonAutomobiles = new List<Automobile>();
                    foreach (Automobile auto in prof.Automobiles)
                    {
                        p.PersonAutomobiles.Add(auto);
                    }

                repository.CloseSession();

                return (Person)p.Clone();
            }
        }
Esempio n. 3
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            personCity = new Dictionary<int, City>();
            IList<City> allcity = new List<City>();
          
            cache = new ItemCache<Certificate>();

            using (RepositoryBase repository = new RepositoryBase())
            {
                try
                {
                    var city = repository.GetCity();
                    int i = 0;
                    foreach (City c in city)
                    {

                        comboBoxEdit1.Properties.Items.Add(c.Name);
                        personCity.Add(i, c);
                        i++;
                    }

                    var certificate = repository.GetCertificates();

                    foreach (Certificate c in certificate)
                    {
                        cache.Add(listBoxControl1.Items.Add(c.Name), c.Id, c);
                        
                    }

                }
                catch (Exception)
                {
                    repository.RollbackTransaction();
                }
            }

            listBoxControl1.SelectedIndex = -1;
        }
        /// <summary>
        /// IN REAL LIFE THIS METHOD SHOULD USE HIBERNATE TO UPDATE PERSON ENTITY
        /// </summary>
        /// <returns></returns>
        public void UpdatePerson(Person p)
        {
            using (RepositoryBase repository = new RepositoryBase())
            {
                try
                {
                    repository.BeginTransaction();
                    p.Id = this.p.Id;

                    foreach (Automobile a in p.Automobiles)
                    {
                        a.Person_id = this.p.Id;
                    }

                    p.Extra.Person_id = this.p.Id;
                    repository.Update(p);

                    repository.CommitTransaction();
                    repository.CloseSession();
                    
                }
                catch(Exception)
                {
                    repository.RollbackTransaction();
                }
            }
        }
 /// <summary>
 /// IN REAL LIFE THIS METHOD SHOULD USE HIBERNATE TO DELETE PERSON ENTITY
 /// </summary>
 /// <returns></returns>
 public void DeletePerson(Person p)
 {
     using (RepositoryBase repository = new RepositoryBase())
     {
         //try
         //{
             repository.BeginTransaction();
             p.Id = this.p.Id;
             repository.Delete(p);
             repository.CommitTransaction();
             repository.CloseSession();
         //}
         //catch (Exception)
         //{
         //    repository.RollbackTransaction();
         //}
     }
 }