コード例 #1
0
      public void AddNewCustomerThrowArgumentExceptionIfCustomerCountryInformationIsEmpty()
      {
         //Arrange
         var countryRepository = new StubICountryRepository();
         var customerRepository = new StubICustomerRepository();

         var customerManagementService = new CustomerAppService(countryRepository, customerRepository);

         var customerDto = new CustomerDto()
         {
            CountryId = Guid.Empty
         };

         //act
         var result = customerManagementService.AddNewCustomer(customerDto);
      }
コード例 #2
0
      /// <summary>
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.AddNewCustomer" />
      /// </summary>
      /// <param name="customerDto">
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.AddNewCustomer" />
      /// </param>
      /// <returns>
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.AddNewCustomer" />
      /// </returns>
      public CustomerDto AddNewCustomer(CustomerDto customerDto)
      {
         //check preconditions
         if (customerDto == null || customerDto.CountryId == Guid.Empty) {
            throw new ArgumentException(Messages.warning_CannotAddCustomerWithEmptyInformation);
         }

         var country = _countryRepository.Get(customerDto.CountryId);

         if (country != null)
         {
            //Create the entity and the required associated data
            var address = new Address(
               customerDto.AddressCity,
               customerDto.AddressZipCode,
               customerDto.AddressAddressLine1,
               customerDto.AddressAddressLine2);

            var customer = CustomerFactory.CreateCustomer(
               customerDto.FirstName,
               customerDto.LastName,
               customerDto.Telephone,
               customerDto.Company,
               country,
               address);

            //save entity
            SaveCustomer(customer);

            //return the data with id and assigned default values
            return customer.ProjectedAs<CustomerDto>();
         }
         else
         {
            return null;
         }
      }
コード例 #3
0
      public void AddNewCustomerReturnAdaptedDto()
      {
         //Arrange

         var countryRepository = new StubICountryRepository();
         countryRepository.GetGuid = (guid) =>
         {
            var country = new Country("Spain", "es-ES");
            ;
            country.ChangeCurrentIdentity(guid);

            return country;
         };
         var customerRepository = new StubICustomerRepository();
         customerRepository.AddCustomer = (customer) => { };
         customerRepository.UnitOfWorkGet = () =>
         {
            var uow = new StubIUnitOfWork();
            uow.Commit = () => { };

            return uow;
         };

         var customerManagementService = new CustomerAppService(countryRepository, customerRepository);

         var customerDto = new CustomerDto()
         {
            CountryId = Guid.NewGuid(),
            FirstName = "Jhon",
            LastName = "El rojo"
         };

         //act
         var result = customerManagementService.AddNewCustomer(customerDto);

         //Assert
         Assert.IsNotNull(result);
         Assert.IsTrue(result.Id != Guid.Empty);
         Assert.AreEqual(result.FirstName, customerDto.FirstName);
         Assert.AreEqual(result.LastName, customerDto.LastName);
      }
コード例 #4
0
      public void UpdateCustomerMergePersistentAndCurrent()
      {
         //Arrange
         var country = new Country("spain", "es-ES");
         country.GenerateNewIdentity();

         var customerId = Guid.NewGuid();

         var countryRepository = new StubICountryRepository();
         var customerRepository = new StubICustomerRepository();

         customerRepository.UnitOfWorkGet = () =>
         {
            var uow = new StubIUnitOfWork();
            uow.Commit = () => { };

            return uow;
         };

         customerRepository.GetGuid = (guid) =>
         {
            var customer = CustomerFactory.CreateCustomer(
               "Jhon",
               "El rojo",
               "+3434",
               "company",
               country,
               new Address("city", "zipCode", "address line", "address line"));
            customer.ChangeCurrentIdentity(customerId);

            return customer;
         };

         customerRepository.MergeCustomerCustomer = (persistent, current) =>
         {
            Assert.AreEqual(persistent, current);
            Assert.IsTrue(persistent != null);
            Assert.IsTrue(current != null);
         };

         var customerManagementService = new CustomerAppService(countryRepository, customerRepository);

         var customerDto = new CustomerDto() //missing lastname
         {
            Id = customerId,
            CountryId = country.Id,
            FirstName = "Jhon",
            LastName = "El rojo",
         };

         //act
         customerManagementService.UpdateCustomer(customerDto);
      }
コード例 #5
0
      public void AddNewCustomerThrowApplicationErrorsWhenEntityIsNotValid()
      {
         //Arrange
         var countryId = Guid.NewGuid();

         var countryRepository = new StubICountryRepository();
         countryRepository.GetGuid = (guid) =>
         {
            var country = new Country("spain", "es-ES");
            country.GenerateNewIdentity();

            return country;
         };
         var customerRepository = new StubICustomerRepository();
         customerRepository.AddCustomer = (customer) => { };
         customerRepository.UnitOfWorkGet = () =>
         {
            var uow = new StubIUnitOfWork();
            uow.Commit = () => { };

            return uow;
         };

         var customerManagementService = new CustomerAppService(countryRepository, customerRepository);

         var customerDto = new CustomerDto() //missing lastname
         {
            CountryId = Guid.NewGuid(),
            FirstName = "Jhon"
         };

         //act
         var result = customerManagementService.AddNewCustomer(customerDto);
      }
コード例 #6
0
 /// <summary>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </summary>
 /// <param name="customer">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 public void UpdateCustomer(CustomerDto customer)
 {
    _customerAppService.UpdateCustomer(customer);
 }
コード例 #7
0
 /// <summary>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </summary>
 /// <param name="customer">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 /// <returns>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </returns>
 public CustomerDto AddNewCustomer(CustomerDto customer)
 {
    return _customerAppService.AddNewCustomer(customer);
 }
コード例 #8
0
      private Customer MaterializeCustomerFromDto(CustomerDto customerDto)
      {
         //create the current instance with changes from customerDTO
         var address = new Address(
            customerDto.AddressCity,
            customerDto.AddressZipCode,
            customerDto.AddressAddressLine1,
            customerDto.AddressAddressLine2);

         var country = new Country("Spain", "es-ES");
         country.ChangeCurrentIdentity(customerDto.CountryId);

         var current = CustomerFactory.CreateCustomer(
            customerDto.FirstName,
            customerDto.LastName,
            customerDto.Telephone,
            customerDto.Company,
            country,
            address);

         current.SetTheCountryReference(customerDto.Id);

         //set credit
         current.ChangeTheCurrentCredit(customerDto.CreditLimit);

         //set picture
         var picture = new Picture
         {
            RawPhoto = customerDto.PictureRawPhoto
         };
         picture.ChangeCurrentIdentity(current.Id);

         current.ChangePicture(picture);

         //set identity
         current.ChangeCurrentIdentity(customerDto.Id);

         return current;
      }
コード例 #9
0
      /// <summary>
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.UpdateCustomer" />
      /// </summary>
      /// <param name="customerDto">
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.UpdateCustomer" />
      /// </param>
      /// <returns>
      ///    <see
      ///       cref="M:Microsoft.Samples.NLayerApp.Application.MainBoundedContext.ERPModule.Services.ICustomerManagement.UpdateCustomer" />
      /// </returns>
      public void UpdateCustomer(CustomerDto customerDto)
      {
         if (customerDto == null || customerDto.Id == Guid.Empty) {
            throw new ArgumentException(Messages.warning_CannotUpdateCustomerWithEmptyInformation);
         }

         //get persisted item
         var persisted = _customerRepository.Get(customerDto.Id);

         if (persisted != null) //if customer exist
         {
            //materialize from customer dto
            var current = MaterializeCustomerFromDto(customerDto);

            //Merge changes
            _customerRepository.Merge(persisted, current);

            //commit unit of work
            _customerRepository.UnitOfWork.Commit();
         }
         else
         {
            LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotUpdateNonExistingCustomer);
         }
      }