Comments (8)
Thank you for opening this issue and sponsoring the project!
We are always happy to welcome new contributors and to assist them for their contributions.
If everything works correctly EF should only query the data needed for the DTO. If I understand you correctly, you would additionally want to provide a list of column names as strings, which columns should be queried?
Mapperly has no reference to the EF model and does not know how to map between property and column names. This mapping is the job of EF Core not Mapperly. Can you elaborate on your use-case? Why do you want to have properties present in your DTO which you donβt want to query?
May also be related to #953 (the mapping generated by Mapperly results in too mich data loaded by EF).
from mapperly.
We would make all the properties in the DTO nullable, and then have the client of the api specify which columns it needs, thus having 1 endpoint that can be used for all client.
// This fictive code would do the job
var posts = context.Posts.Select(p => new PostDto{
// Using the condition this way, does not work I guess?
Id = colums.Contains("Id") ? p.Id : null
});
And we are currently using this code (no mapper) that we would like to replace:
private static IQueryable<TDto> ApplySelect<TIn, TDto>(this IQueryable<TIn> query, string? select) where TIn : class, new() where TDto : class, new()
{
if (!string.IsNullOrWhiteSpace(select))
{
var inType = typeof(TIn);
var dtoType = typeof(TDto);
var properties = select.Split(',');
var parameter = Expression.Parameter(inType, "p");
var newExpression = Expression.New(dtoType);
var bindings = new List<MemberBinding>();
foreach (var property in properties)
{
var inPropertyInfo = inType.GetProperty(property);
if (inPropertyInfo == null)
{
throw new ArgumentException($"The $select parameter contains a property name '{property}' that does not exist on type '{inType.Name}'.");
}
var dtoPropertyInfo = dtoType.GetProperty(property);
if (dtoPropertyInfo == null)
{
throw new ArgumentException($"The $select parameter contains a property name '{property}' that does not exist on type '{dtoType.Name}'.");
}
var propertyAccess = Expression.MakeMemberAccess(parameter, inPropertyInfo);
var binding = Expression.Bind(dtoPropertyInfo, propertyAccess);
bindings.Add(binding);
}
var memberInit = Expression.MemberInit(newExpression, bindings);
var selectExp = Expression.Lambda<Func<TIn, TDto>>(memberInit, parameter);
return query.Select(selectExp);
}
return query.SelectAll<TIn, TDto>();
}
from mapperly.
I see your use-case but I'm afraid this is out of scope of Mapperly.
It is probably not even possible with source generation while not using reflection.
If you can rewrite the code you provided without reflection it is possible using source generation π
I'm closing this for now as out of scope.
from mapperly.
Here is the full code that I generated myself without reflection, I'm still hoping this code could also be auto generated.
public static IQueryable<PostDto> SelectDtoColumns(this IQueryable<Post> posts, string[] columns)
{
return posts.Select(p => new PostDto
{
Id = colums.Contains(nameof(Post.Id)) ? p.Id : null,
Title = colums.Contains(nameof(Post.Title)) ? p.Title : null,
....
});
}
from mapperly.
Does it work like this? I didn't try it but just from reading the code I'd think EF Core would try to translate the columns.Contains
part to SQL too since it is part of the select expression.
from mapperly.
The condition is evaluated before sending to the server. So the resulting SQL (with coulmns = new [] {"Id"}
is:
SELECT Id, null as Title FROM Posts .....
So the expression is evaluated before sending to SQL. Which is at least better then selecting the values and then removing them when sending to the client. This way they are not loaded from the database.
from mapperly.
@latonz with these last comments, would it be something that might be accepted in this library? And can you un-close the issue if that is the case?
from mapperly.
I don't think this should be included in Mapperly as it seems to be very specific and therefore outside the scope of Mapperly.
from mapperly.
Related Issues (20)
- Disable Auto Mapping HOT 1
- Raise diagnostic for memberpaths that mistakenly specify the type name
- Ignoring a property based on the attribute on the property instead of the mapper attribute HOT 5
- Add the GeneratedCodeAttribute to generated code HOT 3
- Add support for dynamic object mapping
- Bug in nullability and external mappers HOT 2
- UseDeepCloning with Action Properties
- `EnumMappingStrategy.ByName` not working for `IQueryable` mapping
- Duplicate source generated mapping methods for collection properties HOT 5
- Add support for EnumMemberAttribute HOT 3
- Consider adding strong name signing HOT 2
- Mapperly does not work on .NET Framework 4.x. HOT 8
- Mapping a collection property of nullable type elements affects other non-related mapping methods HOT 2
- Property specific static mapper HOT 1
- Mention contributors in the release notes
- IReadOnlyCollection as interface param uses Length instead of Count when source is an array HOT 11
- Mapperly does not properly map DateTimeOffset to gRPC entities HOT 3
- Emit warning when a mapping doesn't use the source value. HOT 2
- Support `From` and `To` static factory methods HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapperly.