public IResult<IContactSummaryReturn> GetContact(string contactKey)
        {
            if(contactKey == null) { throw new ArgumentNullException("contactKey"); }

            var contactKeyResult = KeyParserHelper.ParseResult<IContactKey>(contactKey);
            if(!contactKeyResult.Success)
            {
                return contactKeyResult.ConvertTo<IContactSummaryReturn>();
            }

            var predicate = new ContactKey(contactKeyResult.ResultingObject).FindByPredicate;
            var selector = ContactProjectors.SelectSummary();

            var contact = _companyUnitOfWork.ContactRepository.Filter(predicate).AsExpandable().Select(selector).SingleOrDefault();
            if(contact == null)
            {
                return new InvalidResult<IContactSummaryReturn>(null, string.Format(UserMessages.ContactNotFound, contactKey));
            }

            return new SuccessResult<IContactSummaryReturn>(contact);
        }
            public void Removes_Contact_and_associated_records_from_database_on_success()
            {
                //Arrange
                var contactKey  = new ContactKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <Contact>(c => c.Addresses = null));
                var addressKey0 = new ContactAddressKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(c => c.SetContact(contactKey)));
                var addressKey1 = new ContactAddressKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(c => c.SetContact(contactKey)));
                var addressKey2 = new ContactAddressKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(c => c.SetContact(contactKey)));



                //Act
                var result = Service.DeleteContact(contactKey.KeyValue);

                //Assert
                result.AssertSuccess();

                Assert.IsNull(RVCUnitOfWork.ContactAddressRepository.FindByKey(addressKey0));
                Assert.IsNull(RVCUnitOfWork.ContactAddressRepository.FindByKey(addressKey1));
                Assert.IsNull(RVCUnitOfWork.ContactAddressRepository.FindByKey(addressKey2));
                Assert.IsNull(RVCUnitOfWork.ContactRepository.FindByKey(contactKey));
            }
            public void Removes_all_ContactAddresses_if_null_Addresses_is_passed()
            {
                //Arrange
                var contactKey = new ContactKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <Contact>(c => c.Addresses = null));

                TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(a => a.SetContact(contactKey));
                TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(a => a.SetContact(contactKey));
                TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(a => a.SetContact(contactKey));

                //Act
                var result = Service.UpdateContact(new UpdateContactParameters
                {
                    ContactKey = contactKey
                });

                //Assert
                result.AssertSuccess();
                var contact = RVCUnitOfWork.ContactRepository.FindByKey(contactKey, c => c.Addresses);

                Assert.IsEmpty(contact.Addresses);
            }
            public void Updates_Contact_as_expected_on_success()
            {
                //Arrange
                const int    expectedAddresses          = 2;
                const string expectedAddressDescription = "Desc";
                const string expectedName        = "Jimmy Corrigan";
                const string expectedPhoneNumber = "lonelyNumber";
                const string expectedEmail       = "probably none";

                var updatedAddress = new Address
                {
                    AddressLine1 = "updated"
                };

                var newAddress = new Address
                {
                    AddressLine1 = "new"
                };

                var contactKey         = new ContactKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <Contact>(c => c.Addresses = null));
                var addressToUpdateKey = new ContactAddressKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(a => a.SetContact(contactKey)));
                var addressToRemoveKey = new ContactAddressKey(TestHelper.CreateObjectGraphAndInsertIntoDatabase <ContactAddress>(a => a.SetContact(contactKey)));

                //Act
                var result = Service.UpdateContact(new UpdateContactParameters
                {
                    ContactKey   = contactKey,
                    Name         = expectedName,
                    PhoneNumber  = expectedPhoneNumber,
                    EmailAddress = expectedEmail,
                    Addresses    = new List <IContactAddressReturn>
                    {
                        new ContactAddressParameters
                        {
                            ContactAddressKey  = addressToUpdateKey.KeyValue,
                            AddressDescription = expectedAddressDescription,
                            Address            = updatedAddress
                        },
                        new ContactAddressParameters
                        {
                            Address = newAddress
                        }
                    }
                });

                //Assert
                result.AssertSuccess();

                var contact = RVCUnitOfWork.ContactRepository.FindByKey(contactKey, c => c.Addresses);

                Assert.AreEqual(expectedName, contact.Name);
                Assert.AreEqual(expectedPhoneNumber, contact.PhoneNumber);
                Assert.AreEqual(expectedEmail, contact.EMailAddress);

                var addresses = contact.Addresses.ToList();

                Assert.AreEqual(expectedAddresses, addresses.Count);

                var contactAddress = addresses.Single(a => addressToUpdateKey.Equals(a));

                Assert.AreEqual(contactAddress.AddressDescription, expectedAddressDescription);
                contactAddress.Address.AssertEqual(updatedAddress);
                Assert.IsNull(addresses.SingleOrDefault(a => addressToRemoveKey.Equals(a)));
                addresses.Single(a => a.Address.AddressLine1 == newAddress.AddressLine1).Address.AssertEqual(newAddress);
            }