Skip to content

Conversation

lxalln
Copy link
Contributor

@lxalln lxalln commented Jun 23, 2018

Allows for constructors to be excluded in the same way as properties
and fields (ShouldMapField & ShouldMapProperty).
Fixes #2691.

lxalln added 2 commits June 23, 2018 17:19
Allows for constructors to be excluded in the same way as properties
and fields (ShouldMapField & ShouldMapProperty).
@dnfclas
Copy link

dnfclas commented Jun 23, 2018

CLA assistant check
All CLA requirements met.

@lxalln
Copy link
Contributor Author

lxalln commented Jun 23, 2018

I think I got all the places where ShouldMapField & ShouldMapProperty are used.

Couldn't find a sensible place for the tests to live, so I just put them with the big random list of tests in the bugs folder.

I'm not sure why ProfileMap is being shown as such a massive diff on GitHub, it really isn't a big diff.

Happy to make changes if required.

/// Specify which constructors should be mapped.
/// By default all non-static constructors are mapped.
/// </summary>
Func<ConstructorInfo, bool> ShouldMapCtor { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ShouldUseConstructor is a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I used Ctor because other places in AM use Ctor in place of Constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, an oversight :)

## Global property/field/constructor filtering

By default, AutoMapper tries to map every public property/field. You can filter out properties/fields with the property/field filters:
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the wording is strange, AM doesn't map constructors, it uses them to construct destination objects. Also there is a special page for constructors, so the content should be there, not here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move it, I agree.

}

private void BuildTypeMap(IConfigurationProvider configurationProvider, ITypeMapConfiguration config)
using System;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know GitHub sometimes messes the diff, but if you could fix it somehow, that would be nice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see what I can do. I am assuming it's line endings, but no idea why it would affect only this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have force saved the file with CRLF line endings, which has produced a much smaller diff in GitHub.

I am guessing that this file has messed up line endings. Not sure what I can do beyond this. It's at least reviewable now, even if 90% of the changes are not really changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return !property.IsStatic() && shouldMapProperty(property);
case FieldInfo field:
return !field.IsStatic && shouldMapField(field);
case ConstructorInfo constructor:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't belong here, constructors are not members to map.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just check Constructors in TypeDetails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should have done that in the first place too 😄 I'll change it.


namespace AutoMapper.UnitTests.Bug
{
public abstract class ShouldMapCtor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can simply rename the namespace from Bug to ShouldMapCtor and then this class is not useful anymore. The file should be one level up. The tests should work against the API. Either assert that some constructor is used when calling Map or assert that configuration validation fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move it.

I wasn't comfortable with these tests, but couldn't get past the way I'd tried to work around outside of AM. Asserting the configuration validation fails, should be fine. I'll do that.

@lxalln
Copy link
Contributor Author

lxalln commented Jun 24, 2018

Right, we're stepping on each other's toes now. I'll leave this alone. I noticed the cosmetic changes too :)

Happy to squash it down if needs be.

@lbargaoanu
Copy link
Contributor

@jbogard I think ShouldUseConstructor makes more sense than ShouldMapConstructor.

@lbargaoanu
Copy link
Contributor

Sorry about the commits :) I thought it would easier to do it myself.

@lxalln
Copy link
Contributor Author

lxalln commented Jun 24, 2018

No problem.

I agree with ShouldUseConstructor, unfortunately there is already DisableConstrutorMapping, so does that mean we should stick with Map instead of Use?

@lbargaoanu
Copy link
Contributor

No, constructor mapping is only about mapping the constructor parameters from the source object. You're filtering all the constructors, regardless of constructor mapping.

@lbargaoanu
Copy link
Contributor

@lxalln Can you please do the rename, as I don't have VS handy?

@lxalln
Copy link
Contributor Author

lxalln commented Jun 24, 2018

Will do shortly

@lbargaoanu
Copy link
Contributor

I was wrong, this will still allow a constructor without parameters to be used, regardless of the condition. I'd say we want it to apply to those too, but I guess it's debatable.

Copy link
Contributor

@lbargaoanu lbargaoanu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@lbargaoanu lbargaoanu changed the title Implement ShouldMapCtor Implement ShouldUseConstructor Jun 24, 2018
@lxalln
Copy link
Contributor Author

lxalln commented Jun 25, 2018

@lbargaoanu The tests I put in prove that an empty constructor cannot be used if it is being excluded by the filter. Am I missing something?

@lbargaoanu
Copy link
Contributor

If there is only the default constructor, it will be used regardless of the condition, right?

@lxalln
Copy link
Contributor Author

lxalln commented Jun 25, 2018

The default constructor is returned from GetDeclaredConstructors and will be filtered like any other constructor. If that means there are no constructors, then it will fail validation.

@lbargaoanu
Copy link
Contributor

Make this test. The condition always returns false. You have only a default constructor. The mapping works. It's not unreasonable. We'l just have to see what people expect :)

@jbogard jbogard added this to the v.Next milestone Jun 25, 2018
@jbogard jbogard merged commit 88a7c78 into LuckyPennySoftware:master Jun 25, 2018
@lock
Copy link

lock bot commented May 5, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Limit which constructors can be used

4 participants