예제 #1
0
        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;
                }
            }
        }
예제 #2
0
        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;
                }
            }
        }
예제 #3
0
        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;
        }