Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal - ProjectToType as NonGeneric #260

Closed
JMPSequeira opened this issue Aug 8, 2020 · 3 comments
Closed

Proposal - ProjectToType as NonGeneric #260

JMPSequeira opened this issue Aug 8, 2020 · 3 comments

Comments

@JMPSequeira
Copy link

Hello,

IMHO it would add great value to your library if one could use ProjectoToType with the following:

IEnumerable<object> result = someDbContext.SomeDbSet.ProjectToType(typeof(SomeProjectionType));

This is already possible with the Adapt method.

This would allow for, i.e., dynamic api requests mapped straight in the projection.

Let me know if you think this to be useful?

@chaowlert
Copy link
Collaborator

I think IEnumerable<Poco> can be assigned to IEnumerable<object> right?

If your type is dynamic, for now Mapster doesn't support your use case. The closest, you can call config.CreateMapExpression to create LambdaExpression and create query from there.

@JMPSequeira
Copy link
Author

No dynamics, I expressed the idea poorly.

I meant projection to types unknown at design time in order to provide flexibility and segregation between the query and the projection.

The example was also expressed poorly :) 2 for 2 I'm on a roll.

Imagine a scenario where you have a web api that performs a query and the return result can be any of the possible projections for the type being queried, something like:

public ActionResult Get(int projectionToken)
{
    Type projectionType = GetProjectionType(projectionToken);

    if(projectionType != null)
    {
        IEnumerable<object> result = someDbContext.SomeDbSet.ProjectToType(projectionType);

        return Ok(result);
    }
    return BadRequest();
}

If I understood correctly your idea would be:

var config = new TypeAdapterConfig().NewConfig(typeof(Source), typeof(Destination));

var lambda = config.Config.CreateMapExpression(new TypeTuple(typeof(Source), typeof(Destination)), MapType.Projection);

var projection = someDbContext.SomeDbSet.AsQueryable().Provider.CreateQuery(lambda);

var result = projection.Cast<object>().ToList();

This is what I envision to be under the hood :)

@JMPSequeira
Copy link
Author

@chaowlert 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants