I want to generate boiler plate code in my reposit

2019-08-01 01:31发布

问题:

As title suggests, I am creating open source project that is in .net core 2.0. here is the architecture of it.

Now, it's working fine with everything including code first, seeders, swagger UI, TDD etc. But there are many places where I have to add/modify classes when I want to add new Table in Database (see SimpleCRUD.Model > Entities)

So, I think I can reduce that boilerplate code, but I am not sure what is best way to do it.

What I did so far?

I tried to create a windows app, which will check and generate code for new added entity.

What I am trying to achieve?

Is there anyway I can add some kind of code in my current project and that will check after each build? is it feasible? any other suggestion to make it working perfectly?

Reference

I have checked this working in few other frameworks like serenity, asp.net boilerplate etc.

回答1:

T4 temples can help cut down the boiler plate...

https://dotnetthoughts.net/generate-your-database-entities-using-t4-templates/



回答2:

You've asked for my help here

I agree with other posters that you might want to look into T4. It sounds like you also want to create an MSBuild task.

I outlined the steps to do this for a different question in post here

You can find my code generators under this folder, CodeGen.SessionProxies

The t4 example can be found here: AppSessionPartials.tt

The MSBuild task can be found here: GenerateSessionProxies.cs

I had it generating a nuget file through the CodeGen.SessionProxies.nuspec. You won't find it on nuget.com; I had a local nuget repository. It would be helpful to you to look at the corresponding install.ps1 to understand how to set the generator up as a msbuild task.

Disclaimer: All of the GitHub links are subject to break if I ever decide to clean up that repo.

Cheers