public void Init()
 {
     patient = new Patient
     {
         Firstname = "AA",
         Surname = "BB",
         DateOfBirth = DateTime.Today,
         Gender = "M",
         Index = 1
     };
     residentialAddress = new Address
     {
         StreetNumber = "10",
         Line1 = "Hope st",
         Suburb = "ASD Park",
         PostCode = "4000",
         Country = "Australia",
         DefaultAddress = true,
         AddressType = AddressType.Residential
     };
     postalAddress = new Address
     {
         StreetNumber = "20",
         Line1 = "Tom st",
         Suburb = "Norman Park",
         PostCode = "4002",
         Country = "Australia",
         DefaultAddress = false,
         AddressType = AddressType.Postal
     };
     patient.Addresses.Add(residentialAddress);
     patient.Addresses.Add(postalAddress);
 }
 public void UpdatePatient(Patient patient)
 {
     ValidatePatientAggregate(patient);
     var aggregate = _patientRepository.GetPatientWithAddress(patient.Id);
     if (aggregate == null) throw new ValidationException("Patient not found");
     aggregate.Firstname = patient.Firstname;
     aggregate.Surname = patient.Surname;
     aggregate.DateOfBirth = patient.DateOfBirth;
     aggregate.Email = patient.Email;
     aggregate.Gender = patient.Gender;
     foreach (var address in patient.Addresses)
     {
         var oldAddress = aggregate.Addresses.FirstOrDefault(f => f.AddressType == address.AddressType);
         if(oldAddress==null)
             aggregate.Addresses.Add(address);
         else
         {
             oldAddress.StreetNumber = address.StreetNumber;
             oldAddress.Country = address.Country;
             oldAddress.Line1 = address.Line1;
             oldAddress.Line2 = address.Line2;
             oldAddress.PostCode = address.PostCode;
             oldAddress.Suburb = address.Suburb;
             oldAddress.DefaultAddress = address.DefaultAddress;
         }
     }
         _patientRepository.AddOrUpdate(aggregate);
         _unitOfWork.Save();
 }
 public void AddPatient(Patient patient)
 {
     ValidatePatientAggregate(patient);
     patient.Created=DateTime.Now;
     _patientRepository.AddOrUpdate(patient);
     _unitOfWork.Save();
 }
 public Patient AddOrUpdate(Patient patient)
 {
     if (patient.Id == default(int)) // New entity
     {
         _context.SetAdd(patient);
     }
     else        // Existing entity
     {
         _context.SetModified(patient);
     };
     foreach (var address in patient.Addresses)
     {
         if (address.Id == default(int))
         {
             _context.SetAdd(address);
         }
         else
         {
             _context.SetModified(address);
         }
     }
     return patient;
 }
 public void ItWillAddAnyAddressWhichMissedThatTypeInRepositoryWhenUpdating()
 {
     var patinetInRepository = new Patient
     {
         Firstname = "AAA",
         Surname = "BBB",
         DateOfBirth = DateTime.Today,
         Gender = "M",
         Index = 1
     };
     var residentialAddressInRepository = new Address
     {
         StreetNumber = "10",
         Suburb = "dfgdf",
         PostCode = "4544",
         Line1 = "Irwe rwer",
         Country = "ewrwe",
         DefaultAddress = true,
         AddressType = AddressType.Residential
     };
     validatorService.Validate(Arg.Is(patient), Arg.Any<Func<Patient, bool>>()).Returns(true);
     patinetInRepository.Addresses.Add(residentialAddressInRepository);
     patientRepository.GetPatientWithAddress(Arg.Any<int>()).Returns(patinetInRepository);
     patientService.UpdatePatient(patient);
     patientRepository.Received().AddOrUpdate(Arg.Any<Patient>());
     unitOfWork.Received().Save();
     patient.Addresses.Any(a => a.AddressType == AddressType.Postal).Should().BeTrue();
 }
 private void ValidatePatientAggregate(Patient patient)
 {
     if (!_validatorService.Validate(patient, _patientValidatorFunc))
         throw new ValidationException("Null or empity information for patient");
     if (patient.Addresses.Count(w => w.DefaultAddress) > 1)
         throw new ValidationException("More than one default address is invalid");
     if (patient.Addresses.Count(w => w.AddressType == AddressType.Postal) > 1 ||
         patient.Addresses.Count(w => w.AddressType == AddressType.Residential) > 1)
         throw new ValidationException("There can be only one Postal or Residential address for any patient");
 }