Support generic type argument inference when mapping method calls#99
Closed
mycroes wants to merge 1 commit intoAutoMapper:masterfrom
Closed
Support generic type argument inference when mapping method calls#99mycroes wants to merge 1 commit intoAutoMapper:masterfrom
mycroes wants to merge 1 commit intoAutoMapper:masterfrom
Conversation
Member
|
We can do this for As you've noted I disagree because of the potential for more work e.g. the following works with public void When_Apply_Where_Clause_Over_Queryable_As_Data_Source()
{
// Arrange
var mapper = CreateMapper();
var models = new List<Model> {new Model {Value = 1}, new Model {Value = 2 } };
var queryable = models.AsQueryable();
Expression<Func<DTO, bool>> dtoPropertyFilter = (dto) => dto.Value == 1;
// Act
var result = queryable.UseAsDataSource(mapper).For<DTO>().Where(dtoPropertyFilter).ToList();
// Assert
result.ShouldNotBeNull();
result.Count.ShouldBe(1);
}
private static IMapper CreateMapper()
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Model, DTO>();
});
var mapper = mapperConfig.CreateMapper();
return mapper;
}
private static IMapper CreateMapper1()
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Model, DTO>()
.ForMember(src => src.Value, opts => opts.MapFrom(dest => (int?)dest.Value));
});
var mapper = mapperConfig.CreateMapper();
return mapper;
}
private class Model
{
public DateTimeOffset Timestamp { get; set; }
public decimal Value { get; set; }
}
private class DTO
{
public DateTimeOffset? Timestamp { get; set; }
public int? Value { get; set; }
}We want to keep |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is mostly a proof of concept. It works, passes all unit tests and passes a few new unit tests. It does solve #97, although it doesn't change anything about the exception that's thrown from the example using
Mapper.MapExpression()that's used there.I guess that in some sense it's an improvement over
GetConvertingTypeIfExistssince it infers the types from the generic type parameters instead of trying to match it to a single target type.Let me know what you think.