public ContactModel Create(ContactModel model) { using (var session = _database.OpenSession()) { using (var transaction = session.BeginTransaction()) { var contact = ConvertToDbo(model); session.Save(contact); transaction.Commit(); model.Id = contact.Id; return model; } } }
public ContactModel Update(ContactModel model) { using (var session = _database.OpenSession()) { using(var transaction = session.BeginTransaction()) { var contact = ConvertToDbo(model); session.Update(contact); var orphans = session.Query<PhoneNumber>().Where(n => n.Contact == null).ToList(); orphans.ForEach(session.Delete); transaction.Commit(); return model; } } }
private Contact ConvertToDbo(ContactModel model) { var contact = new Contact { Id = model.Id ?? 0, FirstName = model.FirstName, SecondName = model.SecondName, Email = model.Email, PhoneNumbers = model.PhoneNumbers.Select(ConvertToDbo).ToList() }; foreach(var phoneNumber in contact.PhoneNumbers) { phoneNumber.Contact = contact; } return contact; }