Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates for PR review
Squash this commit out
  • Loading branch information
lxalln committed Jun 24, 2018
commit 19757a7121ae2899b38b3d0e3b79c944b48e43a0
9 changes: 3 additions & 6 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ Mapper.Initialize(cfg => {
});
```

## Global property/field/constructor filtering
## Global property/field filtering

By default, AutoMapper tries to map every public property/field, and all non-static declared constructors. You can filter out properties/fields/constructors with the property/field/constructor filters:
By default, AutoMapper tries to map every public property/field. You can filter out properties/fields with the property/field filters:

```c#
Mapper.Initialize(cfg =>
Expand All @@ -189,9 +189,6 @@ Mapper.Initialize(cfg =>
// map properties with a public or private getter
cfg.ShouldMapProperty = pi =>
pi.GetMethod != null && (pi.GetMethod.IsPublic || pi.GetMethod.IsPrivate);

// don't map private constructors
cfg.ShouldMapCtor = ci => !ci.IsPrivate;
});
```

Expand Down Expand Up @@ -231,4 +228,4 @@ Mapper.Reset();
Mapper.Initialize(cfg => { /* configure again */ });
```

`Reset` should not be used in production code. It is intended to support testing scenarios only.
`Reset` should not be used in production code. It is intended to support testing scenarios only.
7 changes: 7 additions & 0 deletions docs/Construction.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ You can also disable constructor mapping :
```c#
Mapper.Initialize(cfg => cfg.DisableConstructorMapping());
```

You can configure which constructors are mapped:

```c#
// don't map private constructors
Mapper.Initialize(cfg => cfg.ShouldMapConstructor = ci => !ci.IsPrivate);
```
2 changes: 1 addition & 1 deletion src/AutoMapper/Configuration/IProfileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IProfileConfiguration
/// Specify which constructors should be mapped.
/// By default all non-static constructors are mapped.
/// </summary>
Func<ConstructorInfo, bool> ShouldMapCtor { get; }
Func<ConstructorInfo, bool> ShouldMapConstructor { get; }

string ProfileName { get; }
IEnumerable<string> GlobalIgnores { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/AutoMapper/IProfileExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public interface IProfileExpression

Func<PropertyInfo, bool> ShouldMapProperty { get; set; }
Func<FieldInfo, bool> ShouldMapField { get; set; }
Func<ConstructorInfo, bool> ShouldMapCtor { get; set; }
Func<ConstructorInfo, bool> ShouldMapConstructor { get; set; }

string ProfileName { get; }
IMemberConfiguration AddMemberConfiguration();
Expand Down
2 changes: 1 addition & 1 deletion src/AutoMapper/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ IEnumerable<Action<PropertyMap, IMemberConfigurationExpression>> IProfileConfigu
public bool? EnableNullPropagationForQueryMapping { get; set; }
public Func<PropertyInfo, bool> ShouldMapProperty { get; set; }
public Func<FieldInfo, bool> ShouldMapField { get; set; }
public Func<ConstructorInfo, bool> ShouldMapCtor { get; set; }
public Func<ConstructorInfo, bool> ShouldMapConstructor { get; set; }

public INamingConvention SourceMemberNamingConvention { get; set; }
public INamingConvention DestinationMemberNamingConvention { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/AutoMapper/ProfileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ProfileMap(IProfileConfiguration profile, IConfiguration configuration)
ConstructorMappingEnabled = profile.ConstructorMappingEnabled ?? configuration?.ConstructorMappingEnabled ?? true;
ShouldMapField = profile.ShouldMapField ?? configuration?.ShouldMapField ?? (p => p.IsPublic());
ShouldMapProperty = profile.ShouldMapProperty ?? configuration?.ShouldMapProperty ?? (p => p.IsPublic());
ShouldMapCtor = profile.ShouldMapCtor ?? configuration?.ShouldMapCtor ?? (c => true);
ShouldMapConstructor = profile.ShouldMapConstructor ?? configuration?.ShouldMapConstructor ?? (c => true);
CreateMissingTypeMaps = profile.CreateMissingTypeMaps ?? configuration?.CreateMissingTypeMaps ?? true;
ValidateInlineMaps = profile.ValidateInlineMaps ?? configuration?.ValidateInlineMaps ?? true;

Expand Down Expand Up @@ -84,7 +84,7 @@ public ProfileMap(IProfileConfiguration profile, IConfiguration configuration)
public string Name { get; }
public Func<FieldInfo, bool> ShouldMapField { get; }
public Func<PropertyInfo, bool> ShouldMapProperty { get; }
public Func<ConstructorInfo, bool> ShouldMapCtor { get; }
public Func<ConstructorInfo, bool> ShouldMapConstructor { get; }

public IEnumerable<Action<PropertyMap, IMemberConfigurationExpression>> AllPropertyMapActions { get; }
public IEnumerable<Action<TypeMap, IMappingExpression>> AllTypeMapActions { get; }
Expand Down
13 changes: 5 additions & 8 deletions src/AutoMapper/TypeDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class TypeDetails
public TypeDetails(Type type, ProfileMap config)
{
Type = type;
var membersToMap = MembersToMap(config.ShouldMapProperty, config.ShouldMapField, config.ShouldMapCtor);
var membersToMap = MembersToMap(config.ShouldMapProperty, config.ShouldMapField);
var publicReadableMembers = GetAllPublicReadableMembers(membersToMap);
var publicWritableMembers = GetAllPublicWritableMembers(membersToMap);
PublicReadAccessors = BuildPublicReadAccessors(publicReadableMembers);
PublicWriteAccessors = BuildPublicAccessors(publicWritableMembers);
PublicNoArgMethods = BuildPublicNoArgMethods();
Constructors = GetAllConstructors(membersToMap);
Constructors = GetAllConstructors(config.ShouldMapConstructor);
PublicNoArgExtensionMethods = BuildPublicNoArgExtensionMethods(config.SourceExtensionMethods);
AllMembers = PublicReadAccessors.Concat(PublicNoArgMethods).Concat(PublicNoArgExtensionMethods).ToList();
DestinationMemberNames = AllMembers.Select(mi => new DestinationMemberName { Member = mi, Possibles = PossibleNames(mi.Name, config.Prefixes, config.Postfixes).ToArray() });
Expand Down Expand Up @@ -61,8 +61,7 @@ private static IEnumerable<string> PostFixes(IEnumerable<string> postfixes, stri

private static Func<MemberInfo, bool> MembersToMap(
Func<PropertyInfo, bool> shouldMapProperty,
Func<FieldInfo, bool> shouldMapField,
Func<ConstructorInfo, bool> shouldMapCtor)
Func<FieldInfo, bool> shouldMapField)
{
return m =>
{
Expand All @@ -72,8 +71,6 @@ private static Func<MemberInfo, bool> MembersToMap(
return !property.IsStatic() && shouldMapProperty(property);
case FieldInfo field:
return !field.IsStatic && shouldMapField(field);
case ConstructorInfo constructor:
return !constructor.IsStatic && shouldMapCtor(constructor);
default:
throw new ArgumentException("Should be a field, property or constructor.");
}
Expand Down Expand Up @@ -169,9 +166,9 @@ private IEnumerable<MemberInfo> GetAllPublicReadableMembers(Func<MemberInfo, boo
private IEnumerable<MemberInfo> GetAllPublicWritableMembers(Func<MemberInfo, bool> membersToMap)
=> GetAllPublicMembers(PropertyWritable, FieldWritable, membersToMap);

private IEnumerable<ConstructorInfo> GetAllConstructors(Func<MemberInfo, bool> membersToMap)
private IEnumerable<ConstructorInfo> GetAllConstructors(Func<ConstructorInfo, bool> shouldMapConstructor)
{
return Type.GetDeclaredConstructors().Where(ci => membersToMap(ci)).ToArray();
return Type.GetDeclaredConstructors().Where(shouldMapConstructor).ToArray();
}

private static bool PropertyReadable(PropertyInfo propertyInfo) => propertyInfo.CanRead;
Expand Down
6 changes: 2 additions & 4 deletions src/AutoMapper/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,8 @@ public bool PassesCtorValidation()
if (DestinationTypeToUse.IsValueType())
return true;

var constructors = DestinationTypeToUse
.GetDeclaredConstructors()
.Where(ci => !ci.IsStatic && Profile.ShouldMapCtor(ci));

var constructors = DestinationTypeDetails.Constructors;

//find a ctor with only optional args
var ctorWithOptionalArgs = constructors.FirstOrDefault(c => c.GetParameters().All(p => p.IsOptional));

Expand Down
8 changes: 0 additions & 8 deletions src/UnitTests/AutoMapper.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;netcoreapp2.0;netcoreapp1.1;</TargetFrameworks>
<AssemblyName>AutoMapper.UnitTests</AssemblyName>
<PackageId>AutoMapper.UnitTests</PackageId>
<NoWarn>$(NoWarn);0649</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' ">
<DefineConstants>NET45</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' OR '$(TargetFramework)' == 'netcoreapp2.0'">
<DefineConstants>DYNAMIC_METHODS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\AutoMapper\ReflectionExtensions.cs" Link="ReflectionExtensions.cs" />
<Compile Include="..\AutoMapper\TypeExtensions.cs" Link="TypeExtensions.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="FakeItEasy" Version="4.6.0" />
Expand All @@ -30,15 +25,12 @@
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<ProjectReference Include="..\AutoMapper\AutoMapper.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>

</Project>
179 changes: 0 additions & 179 deletions src/UnitTests/Bug/ShouldMapCtor.cs

This file was deleted.

Loading