Skip to content

Commit

Permalink
Support for VB.NET named, indexed properties where all indexes have d…
Browse files Browse the repository at this point in the history
…efault values, re: #221 / Performance improvements
  • Loading branch information
SteveWilkes committed Mar 26, 2022
1 parent 93f2b4f commit c0f6beb
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 117 deletions.
24 changes: 17 additions & 7 deletions AgileMapper.UnitTests.MoreTestClasses.Vb/PublicNamedIndex.vb
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
Public Class PublicNamedIndex(Of T)
Public Class PublicNamedIndex(Of T1, T2)

Private _valueToReturn As T
Private _value1ToReturn As T1

Public WriteOnly Property ValueToReturn As T
Public WriteOnly Property Value1ToReturn As T1
Set
_valueToReturn = Value
_value1ToReturn = Value
End Set
End Property

Public ReadOnly Property Value(
Public ReadOnly Property Value1(
Optional indexOne As Integer = 1,
Optional indexTwo As Integer = 2) As T
Optional indexTwo As Integer = 2) As T1
Get
Return _valueToReturn
Return _value1ToReturn
End Get
End Property

Public Property Value2SetValue As T2

Public WriteOnly Property Value2(
indexOne As Integer,
indexTwo As Integer) As T2
Set
Value2SetValue = Value
End Set
End Property

End Class
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public void ShouldIncludeUnmappableReadOnlyArrayMemberDetails()
public void ShouldIncludeUnmappableIndexedPropertyDetails()
{
string plan = Mapper
.GetPlanFor<PublicField<PublicField<string>>>()
.ToANew<PublicNamedIndex<PublicField<string>>>();
.GetPlanFor<PublicTwoFields<PublicField<string>, PublicField<int>>>()
.ToANew<PublicNamedIndex<PublicField<string>, PublicField<int>>>();

plan.ShouldContain("requires index(es) - indexOne: int, indexTwo: int");
}
Expand Down
30 changes: 18 additions & 12 deletions AgileMapper.UnitTests/WhenMappingToNewComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,40 @@ public void ShouldConditionallyUseConstructorsWhereArgumentsAreNull()

// See https://github.com/agileobjects/AgileMapper/issues/221
[Fact]
public void ShouldIgnoreSourceIndexedProperties()
public void ShouldIgnoreUnmappableSourceIndexedProperties()
{
var source = new PublicNamedIndex<PublicField<string>>
var source = new PublicNamedIndex<PublicField<string>, PublicField<int>>
{
ValueToReturn = new PublicField<string> { Value = "Test" }
Value1ToReturn = new PublicField<string> { Value = "Test" }
};

var result = Mapper
.Map(source)
.ToANew<PublicField<PublicField<string>>>();
.ToANew<PublicTwoFields<PublicField<string>, PublicField<int>>>();

result.ShouldNotBeNull().Value.ShouldBeNull();
result.ShouldNotBeNull().Value1.ShouldNotBeNull().Value.ShouldBe("Test");
result.ShouldNotBeNull().Value2.ShouldBeNull();
}

// See https://github.com/agileobjects/AgileMapper/issues/221
[Fact]
public void ShouldIgnoreTargetIndexedProperties()
public void ShouldIgnoreUnmappableTargetIndexedProperties()
{
var source = new PublicField<PublicField<string>>
var source = new PublicTwoFields<PublicField<int>, PublicField<string>>
{
Value = new PublicField<string> { Value = "Hello!" }
Value1 = new PublicField<int> { Value = 999 },
Value2 = new PublicField<string> { Value = "C ya!" }
};

var result = Mapper
.Map(source)
.ToANew<PublicNamedIndex<PublicField<string>>>();
var target = new PublicNamedIndex<PublicField<int>, PublicField<string>>
{
Value1ToReturn = new PublicField<int>()
};

var result = Mapper.Map(source).OnTo(target);

result.ShouldNotBeNull().get_Value().ShouldBeNull();
result.ShouldNotBeNull().get_Value1().ShouldNotBeNull().Value.ShouldBe(999);
result.ShouldNotBeNull().Value2SetValue.ShouldBeNull();
}

#region Helper Classes
Expand Down
11 changes: 6 additions & 5 deletions AgileMapper/AgileMapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>./Icon.png</PackageIcon>
<PackageReleaseNotes>- Fixing exact-name-matching constructor parameter mapping, re: #221
- Partial support for VB.NET named, indexed properties, re: #221
- Performance improvements
</PackageReleaseNotes>
</PropertyGroup>
Expand All @@ -42,7 +43,7 @@
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net35'">
<DefineConstants>$(DefineConstants);FEATURE_DRAWING;FEATURE_APPDOMAIN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_DRAWING;FEATURE_APPDOMAIN;FEATURE_DBNULL</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
Expand All @@ -51,7 +52,7 @@
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' ">
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET;FEATURE_APPDOMAIN;FEATURE_ASSEMBLY_TRUST</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET;FEATURE_APPDOMAIN;FEATURE_ASSEMBLY_TRUST;FEATURE_MAKEINDEX;FEATURE_DBNULL</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
Expand All @@ -61,7 +62,7 @@
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<DefineConstants>$(DefineConstants);FEATURE_DYNAMIC;FEATURE_ISET</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_DYNAMIC;FEATURE_ISET;FEATURE_MAKEINDEX</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
Expand All @@ -71,11 +72,11 @@
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET;FEATURE_MAKEINDEX</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET;FEATURE_APPDOMAIN;FEATURE_ASSEMBLY_TRUST</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZATION;FEATURE_DYNAMIC;FEATURE_DYNAMIC_ROOT_SOURCE;FEATURE_ISET;FEATURE_APPDOMAIN;FEATURE_ASSEMBLY_TRUST;FEATURE_MAKEINDEX;FEATURE_DBNULL</DefineConstants>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions AgileMapper/DataSources/EnumerableDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static Expression GetForwardLinkSelection(Expression sourceEnumerable,
IMemberMapperData mapperData)
{
var funcTypes = new[] { linkParameter.Type, forwardLink.Type };
var forwardLinkAccess = forwardLink.GetAccess(linkParameter);
var forwardLinkAccess = forwardLink.GetReadAccess(linkParameter);

var forwardLinkLambda = Expression.Lambda(
Expression.GetFuncType(funcTypes),
Expand Down Expand Up @@ -150,7 +150,7 @@ private static Expression GetLinkOrdering(

var orderMemberAccess = Expression.MakeMemberAccess(
(orderMember.DeclaringType != linkParameter.Type)
? forwardLink.GetAccess(linkParameter)
? forwardLink.GetReadAccess(linkParameter)
: linkParameter,
orderMember);

Expand Down
24 changes: 24 additions & 0 deletions AgileMapper/Extensions/Internal/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,29 @@ public static IEnumerable<Type> QueryTypes(this Assembly assembly)
return ex.Types.WhereNotNull();
}
}

public static IEnumerable<ParameterInfo> QueryRequiredParameters(
this MethodInfo method)
{
return method.GetParameters().QueryRequired();
}

public static IEnumerable<ParameterInfo> QueryRequired(
this IEnumerable<ParameterInfo> parameters)
{
return parameters.Filter(p =>
{
if (p.IsOptional)
{
return false;
}

#if FEATURE_DBNULL
return p.DefaultValue == DBNull.Value;
#else
return p.DefaultValue.GetType().Name == "DBNull";
#endif
});
}
}
}
2 changes: 1 addition & 1 deletion AgileMapper/MapperContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Expression GetIdentifierOrNull(

var identifier = context.GetIdentifierOrNull(key.Type);

return identifier?.GetAccess(subject);
return identifier?.GetReadAccess(subject);
});
}

Expand Down
Loading

0 comments on commit c0f6beb

Please sign in to comment.