public EditWindow(Window owner, PersonDTO personDTO) { InitializeComponent(); Owner = owner; personDTOEdited = new PersonDTO(); if (personDTO != null) { personDTOEdited.Id = personDTO.Id; personDTOEdited.FirstName = personDTO.FirstName; personDTOEdited.MiddleName = personDTO.MiddleName; personDTOEdited.SecondName = personDTO.SecondName; personDTOEdited.Position = personDTO.Position; personDTOEdited.Department = personDTO.Department; personDTOEdited.DateOfBirth = personDTO.DateOfBirth; } else { personDTOEdited.FirstName = ""; personDTOEdited.MiddleName = ""; personDTOEdited.SecondName = ""; personDTOEdited.Position = ""; personDTOEdited.Department = ""; personDTOEdited.DateOfBirth = new DateTime(1990, 1, 1); } loadData(); }
// delete existed person internal void DeletePerson(PersonDTO personDTODelete) { using (var entities = new ORGANIZATIONEntities()) { try { // get personDTO id var id = personDTODelete.Id; // select deleted person and delete it if (entities.People.Any(pers => pers.Id == id)) { var personToDelete = entities.People.Where(pers => pers.Id == id).First(); entities.People.DeleteObject(personToDelete); entities.SaveChanges(SaveOptions.DetectChangesBeforeSave); personDTO.Remove(personDTODelete); } else { throw new ArgumentException("Не найден сотрудник с указанным идентификатором (Id)"); } } catch (Exception) { // error while delete person // throw exception again throw; } } }
// add new person to organization internal void AddPerson(PersonDTO personDTOAdd) { // if new person is null if (personDTOAdd == null) throw new ArgumentNullException("personDTOAdd", "Переданный объект не указывает на конкретный экзампляр"); using (var entities = new ORGANIZATIONEntities()) { // create transaction scope to make full transaction using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew)) { try { // add new person from parameters var person = personDTOAdd.GetPerson(); entities.AddToPeople(person); entities.SaveChanges(SaveOptions.DetectChangesBeforeSave); personDTOAdd.Id = person.Id; personDTO.Add(personDTOAdd); transaction.Complete(); } catch (Exception) { // error while add person // throw exception again throw; } } } }
public static void UpdatePerson(this Person person, PersonDTO newPersonDTO) { person.FirstName = newPersonDTO.FirstName; person.MiddleName = newPersonDTO.MiddleName; person.SecondName = newPersonDTO.SecondName; person.Position = newPersonDTO.Position; person.Department = newPersonDTO.Department; person.DateOfBirth = newPersonDTO.DateOfBirth; }
public static PersonDTO GetPersonDTO(this Person person) { var personDTO = new PersonDTO(); personDTO.Id = person.Id; personDTO.FirstName = person.FirstName; personDTO.MiddleName = person.MiddleName; personDTO.SecondName = person.SecondName; personDTO.Position = person.Position; personDTO.Department = person.Department; personDTO.DateOfBirth = person.DateOfBirth; return personDTO; }
// edit existed person internal void EditPerson(PersonDTO personDTOEdit) { using (var entities = new ORGANIZATIONEntities()) { try { // get personDTO id var id = personDTOEdit.Id; // select person with specified id if (entities.People.Any(pers => pers.Id == id)) { var personToEdit = entities.People.Where(pers => pers.Id == id).First(); // edit existed person in DB personToEdit.UpdatePerson(personDTOEdit); entities.SaveChanges(SaveOptions.DetectChangesBeforeSave); // update existed personDTO var personDTOEdited = personDTO.Where(pers => pers.Id == id).First(); personDTOEdited.UpdatePersonDTO(personDTOEdit); } else { throw new ArgumentException("Не найден сотрудник с указанным идентификатором (Id)"); } } catch (Exception) { // throw exception again throw; } } }