// 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; } } } }
// 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; } } }
// 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; } } }