public void AddNewCustomerReturnAdaptedDTO()
        {
            //Arrange
            var adapter = PrepareTypeAdapter();
            var countryRepository = new SICountryRepository();
            var customerRepository = new SICustomerRepository();
            customerRepository.AddCustomer = (customer) => { };
            customerRepository.UnitOfWorkGet = () =>
            {
                var uow = new SIUnitOfWork();
                uow.Commit = () => { };

                return uow;
            };

            var customerManagementService = new CustomerAppService(adapter, 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);
        }
        /// <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)
            {
                LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotAddCustomerWithEmptyInformation);

                return 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.CountryId,
                                                          address);

            customer.Telephone = customerDTO.Telephone;
            customer.Company = customerDTO.Company;

            //assign the identity
            customer.Id = IdentityGenerator.NewSequentialGuid();

            // set the picture relation
            customer.Picture = new Picture()
            {
                 Id = customer.Id
            };

            //save entity
            SaveCustomer(customer);

            //return the data with id and assigned default values
            return _typesAdapter.Adapt<Customer, CustomerDTO>(customer);
        }
        /// <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)
            {
                LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotUpdateCustomerWithEmptyInformation);
                return;
            }

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

            if (persisted != null) //if customer exist
            {
                //create the current instance with changes from customerDTO
                var address = new Address(customerDTO.AddressCity,customerDTO.AddressZipCode,customerDTO.AddressAddressLine1,customerDTO.AddressAddressLine2);

                var current = CustomerFactory.CreateCustomer(customerDTO.FirstName,
                                                             customerDTO.LastName,
                                                             customerDTO.CountryId,
                                                             address);
                current.Id = customerDTO.Id;
                current.CreditLimit = customerDTO.CreditLimit;
                current.Company = customerDTO.Company;
                current.Telephone = customerDTO.Telephone;

                current.Picture = new Picture()
                {
                    Id = current.Id,
                    RawPhoto = customerDTO.PictureRawPhoto
                };

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

                //commit unit of work
                _customerRepository.UnitOfWork.Commit();
            }
            else
                LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotUpdateNonExistingCustomer);
        }
        public void AddNewCustomerReturnNullIfCustomerCountryInformationIsEmpty()
        {
            //Arrange
            var adapter = PrepareTypeAdapter();
            var countryRepository = new SICountryRepository();
            var customerRepository = new SICustomerRepository();

            var customerManagementService = new CustomerAppService(adapter, countryRepository, customerRepository);

            var customerDTO = new CustomerDTO()
            {
                CountryId = Guid.Empty
            };

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

            //Assert
            Assert.IsNull(result);
        }
        public void UpdateCustomerMergePersistentAndCurrent()
        {
            //Arrange
            Guid countryGuid = Guid.NewGuid();
            Guid customerId = Guid.NewGuid();
            var adapter = PrepareTypeAdapter();
            var countryRepository = new SICountryRepository();
            var customerRepository = new SICustomerRepository();

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

                return uow;
            };

            customerRepository.GetGuid = (guid) =>
            {
                var customer = CustomerFactory.CreateCustomer("Jhon",
                                                               "El rojo",
                                                               countryGuid,
                                                               new Address("city", "zipCode", "address line", "address line"));
                customer.Id = customerId;

                return customer;
            };

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

            var customerManagementService = new CustomerAppService(adapter, countryRepository, customerRepository);

            var customerDTO = new CustomerDTO() //missing lastname
            {
                Id = customerId,
                CountryId = countryGuid,
                FirstName = "Jhon",
                LastName = "El rojo",
            };

            //act
            customerManagementService.UpdateCustomer(customerDTO);
        }
        public void AddNewCustomerThrowApplicationErrorsWhenEntityIsNotValid()
        {
            //Arrange
            var adapter = PrepareTypeAdapter();
            var countryRepository = new SICountryRepository();
            var customerRepository = new SICustomerRepository();
            customerRepository.AddCustomer = (customer) => { };
            customerRepository.UnitOfWorkGet = () =>
            {
                var uow = new SIUnitOfWork();
                uow.Commit = () => { };

                return uow;
            };

            var customerManagementService = new CustomerAppService(adapter, countryRepository, customerRepository);

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

            //act
            var result = customerManagementService.AddNewCustomer(customerDTO);
        }
 /// <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);
 }
 /// <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);
 }