public void CopyPropertiesIfNameTypeAndValueTypeMatches() { //Arrange var source = new SomeSource { SamePropNameNotPrimitiveValueType = (decimal)1.2 }; var target = new SomeTarget(); //Act SimplePropertyMapper.Map(source, target); //Assert Assert.AreEqual(source.SamePropNameNotPrimitiveValueType, target.SamePropNameNotPrimitiveValueType); }
public void DoNotCopyPropertiesIfNamesMismatch() { //Arrange var source = new SomeSource { SomeProp1 = "Test String" }; var target = new SomeTarget(); //Act SimplePropertyMapper.Map(source, target); //Assert Assert.AreNotEqual(source.SomeProp1, target.DbProp1); }
public void DoNotCopyPropertiesIfTypesAreNotPrimitiveOrInTheAllowedTypesList() { //Arrange var source = new SomeSource { SomeComplexType = (object)("TEST complex type") }; var target = new SomeTarget(); //Act SimplePropertyMapper.Map(source, target); //Assert Assert.AreNotEqual(source.SomeComplexType, target.SomeComplexType); }
public void DoNotCopyPropertiesIfTypesMismatch() { //Arrange var source = new SomeSource { SameNameDifferentType = "Test String" }; var target = new SomeTarget(); //Act SimplePropertyMapper.Map(source, target); //Assert Assert.AreNotEqual(source.SameNameDifferentType, target.SameNameDifferentType); }
public OrganizationViewModel(CompanyBo organization) : this() { SimplePropertyMapper.Map(organization, this); }
private PropertyInfo getPropertyInfo(PropertyDeclarationSyntax node, SemanticModel semanticModel) { var isEnumerable = false; var isGeneric = false; var typeInfo = semanticModel.GetTypeInfo(node.Type); var propertyName = node.Identifier.ValueText; var hasSetter = node.AccessorList?.Accessors.FirstOrDefault( a => a.IsKind(SyntaxKind.SetAccessorDeclaration) && !a.Modifiers.Any(m => m.IsKind(SyntaxKind.PrivateKeyword))) != null; var typeName = ""; PropertyMapper mapper = new UnknownTypePropertyMapper(propertyName, hasSetter); if (typeInfo.ConvertedType is INamedTypeSymbol namedType) { var namedTypeFullName = namedType.ToString().Replace(" ", string.Empty); isEnumerable = namedType.ConstructedFrom.MemberNames.Contains("GetEnumerator"); var simpleTypeKinds = new[] { TypeKind.Enum, TypeKind.Struct, TypeKind.Interface }; var isSimple = namedType.IsValueType || simpleTypeKinds.Any(t => namedType.TypeKind == t) || namedType.Name.Equals("object", StringComparison.CurrentCultureIgnoreCase) || namedType.Name.Equals("string", StringComparison.CurrentCultureIgnoreCase); if (isSimple) { mapper = new SimplePropertyMapper(propertyName, hasSetter); } else { isGeneric = namedType.ConstructedFrom.IsGenericType; if (isEnumerable) { if (namedType.Name.Equals("List", StringComparison.InvariantCultureIgnoreCase)) { typeName = namedTypeFullName.Substring(namedTypeFullName.LastIndexOf(".List", StringComparison.InvariantCultureIgnoreCase) + 1); var fullGenericPart = $"{typeName.Replace("List<", string.Empty).Replace(">", string.Empty)}"; var genericPart = fullGenericPart.Substring(fullGenericPart.LastIndexOf('.') + 1); if (Constants.SimpleTypeNames.Any(t => genericPart.Equals(t))) { mapper = new SimpleGenericListPropertyMapper(propertyName, hasSetter); } else { genericPart = $"{genericPart}{Constants.DtoSuffix}"; mapper = new ListPropertyMapper(genericPart, propertyName, hasSetter); } typeName = $"List<{genericPart}>"; } } else { typeName = $"{namedType.Name}{Constants.DtoSuffix}"; mapper = new ClassPropertyMapper(typeName, propertyName, hasSetter); } } } else { if (typeInfo.ConvertedType.Kind == SymbolKind.ArrayType && typeInfo.ConvertedType is IArrayTypeSymbol arrayType) { if (arrayType.ElementType.IsValueType || arrayType.ElementType.Name.ToLowerInvariant().Contains("string")) { mapper = new SimpleArrayPropertyMapper(propertyName, hasSetter); } else { typeName = $"{arrayType.ElementType.Name}{Constants.DtoSuffix}[]"; mapper = new ArrayPropertyMapper($"{arrayType.ElementType.Name}{Constants.DtoSuffix}", propertyName, hasSetter); } isEnumerable = true; } } return(new PropertyInfo { Name = propertyName, Type = node.Type, TypeName = typeName, IsEnumerableType = isEnumerable, IsGenericType = isGeneric, Mapper = mapper, HasSetter = hasSetter }); }