public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Child_Collection() { TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true; var source = new ParentPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List <ChildPoco> { new ChildPoco { Id = Guid.NewGuid(), Name = "TestName" } } }; var exception = Assert.Throws <ArgumentOutOfRangeException>(() => TypeAdapter.Adapt <ParentPoco, ParentDto>(source)); exception.Message.ShouldContain("UnmappedChildren"); }
public void TypeAdapter_AdaptMappedTypes() { //Arrange TypeAdapter adapter = null; RegisterTypesMap[] mapModules = new RegisterTypesMap[] { new CRMRegisterTypesMap(), new SalesRegisterTypesMap() }; adapter = new TypeAdapter(mapModules); Customer customer = new Customer() { Id = 1, FirstName = "Unai", LastName = "Zorrilla" }; Order order = new Order() { Id = 1, OrderDate = DateTime.UtcNow, Total = 1000 }; //Act var dtoCustomer = adapter.Adapt<Customer, CustomerDTO>(customer); var dtoOrder = adapter.Adapt<Order, OrderDTO>(order); //Assert Assert.IsNotNull(dtoCustomer); Assert.IsNotNull(dtoOrder); Assert.IsTrue(dtoCustomer.CustomerId == 1); Assert.IsTrue(dtoCustomer.FullName == string.Format("{0},{1}",customer.LastName,customer.FirstName)); Assert.IsTrue(dtoOrder.OrderId == 1); Assert.IsTrue(dtoOrder.Description == string.Format("{0} - {1}",order.OrderDate,order.Total)); }
public void Enum_Is_Mapped_To_Enum() { var employee = new EmployeeWithEnum { Id = Guid.NewGuid(), Name = "Timuçin", Department = EmployeeDepartments.IT }; TypeAdapterConfig.GlobalSettings.Default.EnumMappingStrategy(EnumMappingStrategy.ByName); var dto = TypeAdapter.Adapt <EmployeeWithEnum, EmployeeDTO>(employee); dto.ShouldNotBeNull(); dto.Id.ShouldBe(employee.Id); dto.Name.ShouldBe(employee.Name); dto.Department.ShouldBe(Departments.IT); }
public SuccessResponse AddSaucer(RelationRequest request) { try { var dealerSaucer = TypeAdapter.Adapt <DealerSaucer>(request); _dealerSaucerValidator.ValidateAndThrowException(dealerSaucer, "Base"); _dealerSaucerRepository.Add(dealerSaucer); return(new SuccessResponse { IsSuccess = true }); } catch (DataAccessException) { throw new ApplicationException(); } }
public CreateResponse Create(UserRequest request) { try { var user = TypeAdapter.Adapt <User>(request); user.Badge = Guid.NewGuid().ToString(); _userValidator.ValidateAndThrowException(user, "Base,Create"); user.EncryptPassword(); _userRepository.Add(user); return(new CreateResponse(user.Id)); } catch (DataAccessException) { throw new ApplicationException(); } }
public void Mapped_Classes_Succeed_With_Child_Mapping() { TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true; TypeAdapterConfig <CollectionPoco, CollectionDto> .NewConfig(); TypeAdapterConfig <ChildPoco, ChildDto> .NewConfig(); var collectionPoco = new CollectionPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List <ChildPoco>() }; var collectionDto = TypeAdapter.Adapt <CollectionPoco, CollectionDto>(collectionPoco); collectionDto.Name.ShouldBe(collectionPoco.Name); }
/// <summary> /// Carrega a tela Editar Cliente /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Editar(int id) { //Traz dados do cliente para editar var clienteEditar = _negocioCliente.Consultar(id); //Traz dados da tabela sexo para carregar combo var sexosTabela = _negocioSexo.Listar(); var sexosTela = TypeAdapter.Adapt <IEnumerable <Sexo>, IEnumerable <SexoViewModel> >(sexosTabela); //Exibe na tela var clienteTela = TypeAdapter.Adapt <Cliente, ClienteViewModel>(clienteEditar); clienteTela.ListaSexos = new SelectList(sexosTela, "Codigo", "Descricao"); TempData["CodigoCliente"] = clienteTela.Codigo; return(View(clienteTela)); }
public async Task <IActionResult> GetAllLocations() { List <Location> locations = await _context.Locations.Include(x => x.Photo).ToListAsync(); // List<LocationForDisplayDto> locationsForDisplayDto = new List<LocationForDisplayDto>(); // for (var i = 0; i < locations.Count; i++) // { // Location location = locations[i]; // LocationForDisplayDto locationForDisplayDto = location.Adapt<LocationForDisplayDto>(); // locationsForDisplayDto.Add(locationForDisplayDto); // } var locationsForDisplayDto = TypeAdapter.Adapt <List <Location>, List <LocationForDisplayDto> >(locations); return(Ok(locationsForDisplayDto)); }
public UserDto Register(UserDto newUser) { var user = _uow.Users.FindByEmail(newUser.EmailAddress); if (user != null) { return(null); } _uow.Users.Create(TypeAdapter.Adapt <User>(newUser)); _uow.Save(); var createdUser = _uow.Users.FindByEmail(newUser.EmailAddress); return(TypeAdapter.Adapt <UserDto>(createdUser)); }
public void Dictionary_To_Object() { var dict = new Dictionary <string, object> { ["Id"] = Guid.NewGuid(), ["Foo"] = "test", }; TypeAdapterConfig <IReadOnlyDictionary <string, object>, SimplePoco> .NewConfig() .Compile(); var poco = TypeAdapter.Adapt <IReadOnlyDictionary <string, object>, SimplePoco>(dict); poco.Id.ShouldBe(dict["Id"]); poco.Name.ShouldBeNull(); }
public void Rule_Base_Testing() { var config = new TypeAdapterConfig(); config.When((srcType, destType, mapType) => srcType == destType) .Ignore("Id"); var simplePoco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" }; var dto = TypeAdapter.Adapt <SimplePoco>(simplePoco, config); dto.Id.ShouldBe(Guid.Empty); dto.Name.ShouldBe(simplePoco.Name); }
public void Ignores_Are_Derived_From_Base_Configurations() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Ignore(dest => dest.Name); var source = new DerivedPoco { Id = new Guid(), Name = "SourceName" }; var dto = TypeAdapter.Adapt <SimpleDto>(source); dto.Id.ShouldEqual(source.Id); dto.Name.ShouldBeNull(); }
public JsonResult Add(UserModel model) { UserRegistrationRequest request = new UserRegistrationRequest(); TypeAdapter.Adapt(model, request); foreach (var roleId in model.SelectedRoles) { request.Roles.Add(new UserRole() { Id = roleId }); } var result = _userRegistrationService.RegisterUser(request); return(Json(result)); }
public void Can_Map_From_Nullable_Source_Without_Values_To_Non_Nullable_Target() { TypeAdapterConfig <NullablePrimitivesPoco, NonNullablePrimitivesDto> .NewConfig() .Compile(); var poco = new NullablePrimitivesPoco { Id = Guid.NewGuid(), Name = "TestName" }; NonNullablePrimitivesDto dto = TypeAdapter.Adapt <NullablePrimitivesPoco, NonNullablePrimitivesDto>(poco); dto.Id.ShouldBe(poco.Id); dto.Name.ShouldBe(poco.Name); dto.IsImport.ShouldBeFalse(); dto.Amount.ShouldBe(0); }
public void Dictionary_To_Object_Map() { var dict = new Dictionary <string, object> { ["Code"] = Guid.NewGuid(), ["Foo"] = "test", }; TypeAdapterConfig <Dictionary <string, object>, SimplePoco> .NewConfig() .Map(c => c.Id, "Code"); var poco = TypeAdapter.Adapt <SimplePoco>(dict); poco.Id.ShouldBe(dict["Code"]); poco.Name.ShouldBeNull(); }
public void Base_Configuration_Applies_To_Derived_Class_If_No_Explicit_Configuration() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Map(dest => dest.Name, src => src.Name + "_Suffix"); var source = new DerivedPoco { Id = new Guid(), Name = "SourceName" }; var dto = TypeAdapter.Adapt <SimpleDto>(source); dto.Id.ShouldEqual(source.Id); dto.Name.ShouldEqual(source.Name + "_Suffix"); }
public void Property_Is_Mapped_From_Null_Value_Successfully() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Map(dest => dest.AnotherName, src => (string)null) .Compile(); var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" }; var dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco); dto.Id.ShouldBe(poco.Id); dto.Name.ShouldBe(poco.Name); dto.AnotherName.ShouldBeNull(); }
public void Object_To_Dictionary_Ignore_Null_Values() { TypeAdapterConfig <SimplePoco, Dictionary <string, object> > .NewConfig() .IgnoreNullValues(true); var poco = new SimplePoco { Id = Guid.NewGuid(), Name = null, }; var dict = TypeAdapter.Adapt <Dictionary <string, object> >(poco); dict.Count.ShouldBe(1); dict["Id"].ShouldBe(poco.Id); }
public void Can_Map_From_Null_Source_To_Non_Nullable_Existing_Target() { var poco = new NullablePrimitivesPoco { Id = Guid.NewGuid(), Name = "TestName" }; var dto = new NonNullablePrimitivesDto(); TypeAdapter.Adapt(poco, dto); dto = TypeAdapter.Adapt <NullablePrimitivesPoco, NonNullablePrimitivesDto>(poco); dto.Id.ShouldEqual(poco.Id); dto.Name.ShouldEqual(poco.Name); dto.IsImport.ShouldBeFalse(); }
public void Property_Is_Mapped_To_Different_Property_Successfully() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Map(dest => dest.AnotherName, src => src.Name) .Compile(); var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" }; var dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco); dto.Id.ShouldBe(poco.Id); dto.Name.ShouldBe(poco.Name); dto.AnotherName.ShouldBe(poco.Name); }
public void Dest_Calls_Calls_Non_Default_Constructor_With_ConstructUsing() { TypeAdapterConfig <SimplePoco, SimpleDtoWithDefaultConstructor> .NewConfig() .IgnoreNullValues(true) .ConstructUsing(() => new SimpleDtoWithDefaultConstructor("unmapped")); var simplePoco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" }; var dto = TypeAdapter.Adapt <SimpleDtoWithDefaultConstructor>(simplePoco); dto.Id.ShouldEqual(simplePoco.Id); dto.Name.ShouldEqual(simplePoco.Name); dto.Unmapped.ShouldEqual("unmapped"); }
public void Base_Configuration_DestinationTransforms_Apply_To_Derived_Class() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .DestinationTransforms.Upsert <string>(x => x.Trim()); var source = new DerivedPoco { Id = new Guid(), Name = "SourceName " }; var dto = TypeAdapter.Adapt <SimpleDto>(source); dto.Id.ShouldEqual(source.Id); dto.Name.ShouldEqual(source.Name.Trim()); }
public void Int_Is_Mapped_To_Enum() { TypeAdapterConfig <Employee, EmployeeDTO> .NewConfig(); var employee = new Employee { Id = Guid.NewGuid(), Name = "Timuçin", Surname = "KIVANÇ", Department = (int)Departments.IT }; var dto = TypeAdapter.Adapt <Employee, EmployeeDTO>(employee); Assert.IsNotNull(dto); Assert.IsTrue(dto.Id == employee.Id && dto.Name == employee.Name && dto.Department == Departments.IT); }
public void Empty_String_Is_Mapped_To_Enum_Default() { TypeAdapterConfig <EmployeeWithStringEnum, EmployeeDTO> .NewConfig(); var employee = new EmployeeWithStringEnum { Id = Guid.NewGuid(), Name = "Timuçin", Department = "" }; var dto = TypeAdapter.Adapt <EmployeeWithStringEnum, EmployeeDTO>(employee); dto.ShouldNotBeNull(); dto.Id.ShouldBe(employee.Id); dto.Name.ShouldBe(employee.Name); dto.Department.ShouldBe(Departments.Finance); }
public void Simple_Type_Is_Converted_With_Adapter_Function() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .MapWith(() => new SimplePocoResolver()); var source = new SimplePoco { Id = Guid.NewGuid(), Name = "SimplePoco" }; var dest = TypeAdapter.Adapt <SimpleDto>(source); dest.Id.ShouldEqual(source.Id); dest.Name.ShouldEqual("I got converted!"); }
public void When_Condition_Throws_Exception_Bubbles_Up() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Map(dest => dest.Amount, src => src.Amount, cond => cond.Amount / cond.Count > 0); var poco = new SimplePoco { Id = new Guid(), Name = "TestName", Amount = 100, Count = 0 }; var exception = Assert.Throws <InvalidOperationException>(() => TypeAdapter.Adapt <SimpleDto>(poco)); exception.InnerException.ShouldBeType <DivideByZeroException>(); }
public void Dictionary_To_Object_Flexible() { var config = new TypeAdapterConfig(); config.Default.NameMatchingStrategy(NameMatchingStrategy.Flexible); var dict = new Dictionary <string, object> { ["id"] = Guid.NewGuid(), ["Name"] = "bar", ["foo"] = "test", }; var poco = TypeAdapter.Adapt <SimplePoco>(dict, config); poco.Id.ShouldBe(dict["id"]); poco.Name.ShouldBe(dict["Name"]); }
public DTO.Worker Execute(Worker worker) { var workerDto = TypeAdapter.Adapt <DTO.Worker>(worker); var department = _departmentRepository.FindBy(worker.DepartmentId); workerDto.Department = TypeAdapter.Adapt <DTO.Department>(department); var job = _jobRepository.FindBy(worker.JobId); workerDto.Job = TypeAdapter.Adapt <DTO.Job>(job); var role = _roleRepository.FindBy(worker.RoleId); workerDto.Role = TypeAdapter.Adapt <DTO.Role>(role); var branch = _branchRepository.FindBy(worker.BranchId); workerDto.Branch = TypeAdapter.Adapt <DTO.Branch>(branch); return(workerDto); }
public void Object_To_Dictionary_Map() { var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "test", }; TypeAdapterConfig <SimplePoco, Dictionary <string, object> > .NewConfig() .Map("Code", c => c.Id); var dict = TypeAdapter.Adapt <Dictionary <string, object> >(poco); dict.Count.ShouldBe(2); dict["Code"].ShouldBe(poco.Id); dict["Name"].ShouldBe(poco.Name); }
public void Simple_String_Value_Is_Converted_With_Resolver_Instance() { TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Resolve(dest => dest.Name, new SimplePocoNameResolver()); var source = new SimplePoco { Id = Guid.NewGuid(), Name = "SimplePoco" }; var destination = TypeAdapter.Adapt <SimpleDto>(source); destination.Id.ShouldEqual(source.Id); destination.Name.ShouldEqual("Resolved:SimplePoco"); }
public void Ignores_Are_Derived_From_Base_Dest_Configurations() { TypeAdapterConfig.GlobalSettings.AllowImplicitDestinationInheritance = true; TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig() .Map(dest => dest.Name, src => src.Name + "_Suffix"); var source = new DerivedPoco { Id = new Guid(), Name = "SourceName" }; var dto = TypeAdapter.Adapt <DerivedDto>(source); dto.Id.ShouldEqual(source.Id); dto.Name.ShouldEqual(source.Name + "_Suffix"); }
public void TypeAdapter_AdapUnmappedTypesThrowInvalidOperationException() { //Arrange TypeAdapter adapter = null; RegisterTypesMap[] mapModules = new RegisterTypesMap[] { new CRMRegisterTypesMap(), new SalesRegisterTypesMap() }; adapter = new TypeAdapter(mapModules); Product product = new Product() { ProductId = 1, LaunchDate = DateTime.UtcNow, ProductName ="Product Name" }; //Act adapter.Adapt<Product, ProductDTO>(product); }
public void TypeAdapter_AdaptNullItemThrowArgumentException() { //Arrange TypeAdapter adapter = null; RegisterTypesMap[] mapModules = new RegisterTypesMap[] { new CRMRegisterTypesMap(), new SalesRegisterTypesMap() }; adapter = new TypeAdapter(mapModules); //Act adapter.Adapt<Customer, CustomerDTO>(null); }