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

I am want to Use your nuget in runtime for generation class using IL #71

Open
P9avel opened this issue Apr 26, 2023 · 8 comments
Open
Labels
enhancement New feature or request

Comments

@P9avel
Copy link

P9avel commented Apr 26, 2023

Hi. I am want to generate class like this in rntime using Emit

public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

[Projectable]
public string FullName => FirstName + " " + LastName;

}

In result extension methods not generated. How to use your nuget in my case?

@koenbeuk
Copy link
Owner

You can't. The whole point of this library is to write companion expressions at compile time using roslyn source generators.

@koenbeuk koenbeuk added the question Further information is requested label Apr 26, 2023
@P9avel
Copy link
Author

P9avel commented Apr 26, 2023

Possible exists way ?

@P9avel
Copy link
Author

P9avel commented Apr 26, 2023

Possible can you to extend ProjectableAttriibute for mmy case? I am want follow

public class User
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }

   [Projectable<MyUserExtensions>(nameof(MyUserExtensions.MyFullNameImpl))]
   public string FullName => FirstName + " " + LastName;
}

public static class MyUserExtensions {
    
    public static Expression<string> MyFullNameImpl(this User user) => 
        user.FirstName +" "+ user.LastName;
}

In this case

  1. In compile-time you not generate UserExtensions class for User.FullName property
  2. In runtime you will use MyUserExtensions.MyFullNameImpl method for User.FullName query

I.e. i am want to wrote all Extensions methods in common class. And when generate IL User class, i am want to generate ProjectableAttribute and using my common class. I really need this functionality.

@koenbeuk
Copy link
Owner

This is currently already supported for members within the same type, e.g. this is allowed:

public record Entity
{
    public int Id { get; set; }
            
    [Projectable(UseMemberBody = nameof(Computed2))]
    public int Computed1 => Id;

    private int Computed2 => Id * 2;

    [Projectable(UseMemberBody = nameof(Computed4))]
    public int Computed3 => Id;

    private static Expression<Func<Entity, int>> Computed4 => x => x.Id * 3;
}

Is that sufficient for your use-case?

@P9avel
Copy link
Author

P9avel commented Apr 28, 2023

Hi, thank you for your solution. But i need to pass value to Expression function. I.e. am want to have base class with expression and set params in child class. For example

` //Base class created in compile-time/design-time VS
public record Base
{
public int Id { get; set; }

    // Use for LINQ quering from DB
    protected static Expression<Func<Base, int, int>> ComputedBase => (x, multiply) => x.Id * multiply;

    // Use for memory object calculation
    protected int ComputedBaseFunc(int multiply) => Id * multiply;
}

//Generated class in RunTime using IL
public record Entity: Base 
{
    // here 123 pass to 'multiply' param
    [Projectable(UseMemberBody = nameof(ComputedBase), 123)]
    public int Computed1 => ComputedBaseFunc(123);

    // here 777 pass to 'multiply' param
    [Projectable(UseMemberBody = nameof(ComputedBase), 777)]
    public int Computed2 => ComputedBaseFunc(777);
}

`

can you to help in implementation my scenario?

@P9avel
Copy link
Author

P9avel commented May 11, 2023

Hi, can you to help in implementation my scenario?

@P9avel
Copy link
Author

P9avel commented Jun 27, 2023

Do you have news about this?

@koenbeuk
Copy link
Owner

If I understand you correctly, you're asking for a dynamic expression body replacement strategy where the actual expression body of a projectable method can be configured at runtime.

I think this is a very niche use-case and I'm not planning on implementing it. I'm not against the feature in general and happy to accept a PR if anyone wants to pick it up. I can give some guidelines on how to get started if anyone wants to give this is a shot

@koenbeuk koenbeuk added enhancement New feature or request help wanted Extra attention is needed and removed help wanted Extra attention is needed question Further information is requested labels Jun 27, 2023
alexeypj pushed a commit to alexeypj/EntityFrameworkCore.Projectables that referenced this issue Mar 27, 2024
alexeypj pushed a commit to alexeypj/EntityFrameworkCore.Projectables that referenced this issue Apr 3, 2024
This was referenced Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants