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

[Feature Request] [Pull Request Question ] How to implement properties with auto-getter '=>' #90

Open
jeffward01 opened this issue Sep 29, 2022 · 5 comments

Comments

@jeffward01
Copy link
Collaborator

jeffward01 commented Sep 29, 2022

Hello!

I am still new to the Roslyn API, but learning fast!

I noticed currently in Testura, there are only options for: Get and GetSet types of properties.

I have a use-case where I am generating a DbContext file. I want to generate:

public class MySchoolDbContext : DbContext
{

// I want to have the property initializer be '=>' generated by Testura.Code
public DbSet<Student> Students => Set<Student>();


}

I don't think this is possible currently.

The goal is to add an additional Property Setter type of: =>

If you are busy, I can implement the feature myself in Testura.Code and test it, then open a pull request. I examined the code, and I could not figure out 'how' to modify the RoslynAPI to generate this. I suspect it hast to do something with SyntaxToken.... but im not sure exactly.

If you can point me in the right direction, I can get this implemented directly in Testura.Code then open a pull request immediately.

Thanks again for the awesome library!

@MilleBo
Copy link
Contributor

MilleBo commented Oct 18, 2022

Hey there and sorry for the late response!

I can take a look at it can come back to you, maybe it just a quick fix to add it into the framework.

MilleBo added a commit that referenced this issue Oct 18, 2022
- Added code to generate arrow expression property #90
@MilleBo
Copy link
Contributor

MilleBo commented Oct 18, 2022

I have pushed code for this now. I created it as a new ArrowExpressionProperty class and you can use it like the other properties (with the difference thast it takes a expression syntax instead of body/property type). So for example:

PropertyGenerator.Create(new ArrowExpressionProperty(
            "MyProperty",
            typeof(int),
            Statement.Expression.Invoke("Set", generics: new[] { CustomType.Create("Student") }).AsExpression(),
            new List<Modifiers> { Modifiers.Public, Modifiers.Virtual })

Will generate:

public virtual int MyProperty => Set<Student>();

Can create a new nuget version after we have updated the nuget references.

@jeffward01
Copy link
Collaborator Author

I have pushed code for this now. I created it as a new ArrowExpressionProperty class and you can use it like the other properties (with the difference thast it takes a expression syntax instead of body/property type). So for example:

PropertyGenerator.Create(new ArrowExpressionProperty(
            "MyProperty",
            typeof(int),
            Statement.Expression.Invoke("Set", generics: new[] { CustomType.Create("Student") }).AsExpression(),
            new List<Modifiers> { Modifiers.Public, Modifiers.Virtual })

Will generate:

public virtual int MyProperty => Set<Student>();

Can create a new nuget version after we have updated the nuget references.

Big thanks for this! I created a PR here with the updated packages and added some comments. I really appreciate you adding the Arrow expression, I spent a long time and could not figure out how to do it, now I have learned more thanks to you!

My PR: #97

@MilleBo
Copy link
Contributor

MilleBo commented Nov 26, 2022

Great! I'm not sure if it's possible for you to publish it as well but otherwise I can do do it after you have merged the PR.

@jeffward01
Copy link
Collaborator Author

@MilleBo - I merged the PR! Thanks for that <3

PR: #97

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