About this book

Content of the book

You will find in this book code examples about >10 Roslyn Source Code Generator (RSCG) that can be useful for you. That means, you will write more elegant and concise code - even if the generators code is not always nice to look.

Are those examples ready for production?

I have done due diligence to test the RSCG that I have shown to you here. However, I cannot guarantee that will fit your code. That means that you can test it for your case and, because all are open source on Github.com, you can contribute to improve them wink

How to read this book

For each chapter, you will find

  1. Name of the RSCG and link to the NuGet package / GitHub repository
  2. What the RSCG can do
  3. What you need to include in .csproj file
  4. What will be the initial code
  5. How to use the Code generated by RSCG
  6. Code Generated by RSCG
  7. Link to the downloadable code to practice

I have a suggestion for a new RSCG that is worth mentioning in this book. What can I do?

Please send me an email to ignatandrei@yahoo.com

I want to make a RSCG that will be useful. How can I do?

In the introduction I have put the links to get you started with RSCG.

And, if you bought this book from Amazon, you are entitled to have 1 hour free of consultancy with me. I can help you make one.

I want the book / sponsor you

Glad that you have asked. Please goto Amazon: https://amzn.to/3f6gll3

( and this will remain free: https://ignatandrei.github.io/RSCG_Examples/)

About the author

Andrei

My name is Andrei Ignat and I have 20+ years programming experience.

I have started from VB3 , passed via plain old ASP

I am C# Microsoft Most Valuable Professional, and also https://forums.asp.net moderator .

Before that I was a teacher.

I am available also on

LinkedIN : http://ro.linkedin.com/in/ignatandrei

Facebook : http://www.facebook.com/ignat.andrei

Twitter: http://twitter.com/ignatandrei

You can ask me any .NET related question . I will be glad to answer – if I know the answer. If not, I will learn.

Introduction

What is a Roslyn Source Code Generator?

A Roslyn Source Code Generator (RSCG) is a program that generates code in the compile time, based on the previous source code and/or another data. This new source code is added to the compilation and compile with the previous source code.

How can I make a Roslyn Source Code Generator?

For creating the RSCG you will simply create a .NET Standard 2.0 project, add those 2 references

<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1" PrivateAssets="all" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />

and start implementing

public interface ISourceGenerator { void Initialize(GeneratorInitializationContext context); void Execute(GeneratorExecutionContext context); }

Start from examples at https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators Also, you can read the source code for the RSCG presented in this book.

Show me some code for RSCG

Start read

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md

and

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md .

After that, you can play with the examples from https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators or from https://sourcegen.dev/ (see AutoNotify in the dropdown)

How the RSCG can help me to write faster / better the code ?

Glad that you asked. You can see in action a RSCG for automatically generating code for automating testing (see DynamicMocking ) , parsing enum (see Enum ) , generating controllers actions from a interface ( SkinnyControllers ), currying functions and many more. In this book you will find more than 10 examples of some RSCG that can help you. Also, you can find the source code of the examples at https://github.com/ignatandrei/RSCG_Examples.

RSCG number 1 : ThisAssembly

What RSCG ThisAssembly can do

The ThisAssembly.Info allows you access to the Assembly Information as constants, instead of going to reflection each time. I found useful to see the assembly version right away in any project that I have.

Example code

Here is the csproj with the references for RSCG ThisAssembly

csprj code

The initial code that you start with is

start code

The code below will use the RSCG ThisAssembly

usage code

The code that is generated by RSCG ThisAssembly

gc code

More details about RSCG ThisAssembly

The author of RSCG ThisAssembly is Daniel Cazzulino

You cand find RSCG ThisAssembly at Nuget.org : https://www.nuget.org/packages/ThisAssembly and the sources at https://github.com/devlooped/ThisAssembly

For more usage features please read : https://www.clarius.org/ThisAssembly/

https://github.com/ignatandrei/RSCG_Examples/tree/main/ApplicationVersion

RSCG number 2 : Enum

What RSCG Enum can do

This will generate code to fast parsing a int or a string to an enum

Example code

Here is the csproj with the references for RSCG Enum

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Enum

usage code

The code that is generated by RSCG Enum

gc code

More details about RSCG Enum

The author of RSCG Enum is Andrei Ignat

You cand find RSCG Enum at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/Enum

RSCG number 3 : JsonByExampleGenerator

What RSCG JsonByExampleGenerator can do

This will generate C# a_class_bes from json files.

Example code

Here is the csproj with the references for RSCG JsonByExampleGenerator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG JsonByExampleGenerator

usage code

The code that is generated by RSCG JsonByExampleGenerator

gc code

More details about RSCG JsonByExampleGenerator

The author of RSCG JsonByExampleGenerator is Robin Hermanussen

You cand find RSCG JsonByExampleGenerator at Nuget.org : https://www.nuget.org/packages/JsonByExampleGenerator/ and the sources at https://github.com/hermanussen/JsonByExampleGenerator/

For more usage features please read : https://github.com/hermanussen/JsonByExampleGenerator/

https://github.com/ignatandrei/RSCG_Examples/tree/main/JsonToa_class_b

Author of JsonByExampleGenerator , Robin Hermanussen

Short info about you , Robin Hermanussen

I'm a Software Architect at Iquality (software company in the Netherlands). I'm passionate about software development in general, but with a particular interest in .NET.

Why did you start this JsonByExampleGenerator ?

I have written a bunch of C# analyzers in the past that served specific needs and now wanted to get some experience in writing source generators. It seemed like a good idea to me to generate a_class_bes based on example JSON, because it replaces a task that I often find a bit tedious; writing data contract a_class_bes and then tests to ensure they get properly (de)serialized with the JSON that I'm using.

How do yourself use your JsonByExampleGenerator ?

I'm not really using it in real world projects right now, as I'm not working on any projects that require it.

What other RSCG do you use ?

I've used ThisAssembly and a few others, for learning purposes.

Any other feedback ?

If anyone wants to use JsonByExampleGenerator and give me feedback from the real world, let me know! I'll be happy to help out with any issues or questions you may run into. Just add an issue to the project on GitHub to get in touch.

RSCG number 4 : CopyConstructor + Deconstructor

What RSCG CopyConstructor + Deconstructor can do

This will generate code for a POCO to generate copy constructor and deconstructor

Example code

Here is the csproj with the references for RSCG CopyConstructor + Deconstructor

csprj code

The initial code that you start with is

start code

The code below will use the RSCG CopyConstructor + Deconstructor

usage code

The code that is generated by RSCG CopyConstructor + Deconstructor

gc code

More details about RSCG CopyConstructor + Deconstructor

The author of RSCG CopyConstructor + Deconstructor is Andrei Ignat

You cand find RSCG CopyConstructor + Deconstructor at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/CopyConstructor

RSCG number 5 : GeneratedMapper

What RSCG GeneratedMapper can do

AutoMapping from a POCO to a DTO. Lots of customizations

Example code

Here is the csproj with the references for RSCG GeneratedMapper

csprj code

The initial code that you start with is

start code

The code below will use the RSCG GeneratedMapper

usage code

The code that is generated by RSCG GeneratedMapper

gc code

More details about RSCG GeneratedMapper

The author of RSCG GeneratedMapper is Thomas Bleijendaal

You cand find RSCG GeneratedMapper at Nuget.org : https://www.nuget.org/packages/GeneratedMapper/ and the sources at https://github.com/ThomasBleijendaal/GeneratedMapper

For more usage features please read : https://github.com/ThomasBleijendaal/GeneratedMapper

https://github.com/ignatandrei/RSCG_Examples/tree/main/DTOMapper

Author of GeneratedMapper , Thomas Bleijendaal

Short info about you , Thomas Bleijendaal

I'm Thomas Bleijendaal, I'm a .NET developer working for Triple in Alkmaar, The Netherlands.

Why did you start this GeneratedMapper ?

I've started GeneratedMapper to get more familiar with source generation. While the tooling is a bit rough now, I do believe that this feature can really bring a lot of value to .NET.

I've been burned before by a lot of hard-to-find bugs caused by badly behaving mappers and runtime surprises from badly handled null-references. I wanted to create a object-to-object mapper that would be very picky and raise compilation errors instead of runtime exceptions. Being able to see what the mapper will do by simply inspecting the code is very handy, and makes you trust your mapper even more.

How do yourself use your GeneratedMapper ?

I use the GeneratedMapper in a project where I map models coming from Contentful to DTOs that are used in views. I've also tried to use it in another project, but because that targeted an older runtime I could not make that work. But, since I could copy all the generated mappers and maintain those manually, switching away from it wasn't that bad. I think that is also very valuable of code generation, you still have a copy of what a generator made for you.

What other RSCG do you use ?

I haven't really used any other RSCGs yet, other than a metadata generator that Microsoft made for the out-of-process .NET-based Azure Functions (https://github.com/Azure/azure-functions-dotnet-worker/). That stuff is all very preview still, so it could be removed from the product, but it's cool to see Microsoft starting to use it too.

Any other feedback ?

Cool book!

RSCG number 6 : Skinny Controllers

What RSCG Skinny Controllers can do

This will generate code for WebAPI for each method of a field in the controller

Example code

Here is the csproj with the references for RSCG Skinny Controllers

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Skinny Controllers

usage code

The code that is generated by RSCG Skinny Controllers

gc code

More details about RSCG Skinny Controllers

The author of RSCG Skinny Controllers is Andrei Ignat

You cand find RSCG Skinny Controllers at Nuget.org : https://www.nuget.org/packages/SkinnyControllersCommon/ https://www.nuget.org/packages/SkinnyControllersGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/SkinnyControllers

RSCG number 7 : data-builder-generator

What RSCG data-builder-generator can do

Implements the Builder Design pattern for any a_class_b. Useful , at least, for test projects

Example code

Here is the csproj with the references for RSCG data-builder-generator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG data-builder-generator

usage code

The code that is generated by RSCG data-builder-generator

gc code

More details about RSCG data-builder-generator

The author of RSCG data-builder-generator is Martin Andreas Ulrich

You cand find RSCG data-builder-generator at Nuget.org : https://www.nuget.org/packages/DasMulli.DataBuilderGenerator/ and the sources at https://github.com/dasMulli/data-builder-generator

For more usage features please read : https://github.com/dasMulli/data-builder-generator

https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Builder

Author of data-builder-generator , Martin Andreas Ulrich

Short info about you , Martin Andreas Ulrich

I am a software engineer working on diverse tech stacks but focusing mostly on .NET in C#, Web Technologies and iOS/Swift.

I am a technology enthusiast who always works on improving development and DevOps processes at our company to make life easier for developers and help deliver high. I was awarded Microsoft MVP for community and open-source work around .NET Core.

Why did you start this data-builder-generator ?

How do yourself use your data-builder-generator ?

In "enterprise" contexts (I hate that term) one usually has to deal with business logic working on data objects. Writing tests is really important in these contexts but creating lots of test data for various scenarios can be quite cumbersome. You usually have some set of defaults (e.g. a base order with a dummy customer and items, an insurance application from a dummy customer etc.) and then deviate from it a little for each scenario. This is where C# 9 records would come in handy, but they were at the time not released and adopting them could be challenging (e.g. proper EF support while we're still on EF Core 3.1 anyway) so we opted for a test data builder approach where the builder a_class_bes would be generated. I did something similar a few years back at a previous company based on https://github.com/AArnott/CodeGeneration.Roslyn (which is now archived in favor of rolsyn source generators) and decided that for a current project I'll have a go and try to create something similar based on roslyn. While there are still a few bugs in the generator (get-only properties for example), this works quite well just annotating all EF model POCOs and then creating a few default builders that can be used from tests (Think TestData.DefaultOrder.WithoutCustomerAddress().Build()

  • extension methods are useful here as well to reuse builder functions that have a business meaning (.WithChecksumMismatchingIBAN())). I hope that many of these concerns can be solved with records in the future, so each .WithXYZ() can be done as a proper with-expression. But until then, autogenerating builder patterns is a good approach. I wanted to hold off refactoring / fixing bugs for advanced use cases until a few first-party generators exist and we know what performance pitfalls to look out for. Since we were using them when generators were in preview, we also had to deal with breaking changes in the API which was a bit of pain.

What other RSCG do you use ?

Currently this is the only one. I hope that OpenAPI processing / generation will be a source generator soon - this is currently patched in via NSwag/MSBuild for some projects.

Any other feedback ?

As mentioned I hope that some patterns of how to implement fast source generators emerge. This one likely isn't the best but I wanted to hold off a few months before refactoring. I don't plan on making a huge deal out of it, I just thought it's good to put useful tools we're building on GitHub for discussion and maybe it helps someone else as well.

RSCG number 8 : Metadata from object

What RSCG Metadata from object can do

This will generate code to retrieve the values of properties directly, not by reflection

Example code

Here is the csproj with the references for RSCG Metadata from object

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Metadata from object

usage code

The code that is generated by RSCG Metadata from object

gc code

More details about RSCG Metadata from object

The author of RSCG Metadata from object is Andrei Ignat

You cand find RSCG Metadata from object at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/MetadataFromObject

RSCG number 9 : MockSourceGenerator

What RSCG MockSourceGenerator can do

This will generate Mock a_class_bes directly for any interface - with your implementation.

Example code

Here is the csproj with the references for RSCG MockSourceGenerator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG MockSourceGenerator

usage code

The code that is generated by RSCG MockSourceGenerator

gc code

More details about RSCG MockSourceGenerator

The author of RSCG MockSourceGenerator is Robin Hermanussen

You cand find RSCG MockSourceGenerator at Nuget.org : https://www.nuget.org/packages/MockSourceGenerator/ and the sources at https://github.com/hermanussen/MockSourceGenerator/

For more usage features please read : https://github.com/hermanussen/MockSourceGenerator/

https://github.com/ignatandrei/RSCG_Examples/tree/main/DynamicMocking

Author of MockSourceGenerator , Robin Hermanussen

Short info about you , Robin Hermanussen

I'm a Software Architect at Iquality (software company in the Netherlands). I'm passionate about software development in general, but with a particular interest in .NET.

Why did you start this MockSourceGenerator ?

I have written a bunch of C# analyzers in the past that served specific needs and now wanted to get some experience in writing source generators. The thing about libraries like Moq or NSubstitute that I don't really like is how it takes so much code to set them up and then I don't find them very readable. MockSourceGenerator takes an approach that allows people to mock something in a single statement (with an object initializer) and I think it is more readable this way.

How do yourself use your MockSourceGenerator ?

I'm not really using it in real world projects right now, as I'm not working on any projects that require it.

What other RSCG do you use ?

I've used ThisAssembly and a few others, for learning purposes.

Any other feedback ?

If anyone wants to use MockSourceGenerator and give me feedback from the real world, let me know! I'll be happy to help out with any issues or questions you may run into. Just add an issue to the project on GitHub to get in touch.

RSCG number 10 : Method decorator

What RSCG Method decorator can do

This will generate code to decorate methods with anything you want ( stopwatch, logging , authorization...)

Example code

Here is the csproj with the references for RSCG Method decorator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Method decorator

usage code

The code that is generated by RSCG Method decorator

gc code

More details about RSCG Method decorator

The author of RSCG Method decorator is Andrei Ignat

You cand find RSCG Method decorator at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/MethodDecorator

RSCG number 11 : PartiallyApplied

What RSCG PartiallyApplied can do

This will generate curry for your functions

Example code

Here is the csproj with the references for RSCG PartiallyApplied

csprj code

The initial code that you start with is

start code

The code below will use the RSCG PartiallyApplied

usage code

The code that is generated by RSCG PartiallyApplied

gc code

More details about RSCG PartiallyApplied

The author of RSCG PartiallyApplied is Jason Bock

You cand find RSCG PartiallyApplied at Nuget.org : https://www.nuget.org/packages/PartiallyApplied/ and the sources at https://github.com/JasonBock/PartiallyApplied

For more usage features please read : https://github.com/JasonBock/PartiallyApplied

https://github.com/ignatandrei/RSCG_Examples/tree/main/PartiallyFunction

RSCG number 12 : IFormattable

What RSCG IFormattable can do

This will generate code to add IFormattable to any a_class_b, based on the properties of the a_class_b

Example code

Here is the csproj with the references for RSCG IFormattable

csprj code

The initial code that you start with is

start code

The code below will use the RSCG IFormattable

usage code

The code that is generated by RSCG IFormattable

gc code

More details about RSCG IFormattable

The author of RSCG IFormattable is Andrei Ignat

You cand find RSCG IFormattable at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/IFormattable

RSCG number 13 : AutoInterface

What RSCG AutoInterface can do

Implement the Design Pattern Decorator. Based on template - you can modify the source code generated

Example code

Here is the csproj with the references for RSCG AutoInterface

csprj code

The initial code that you start with is

start code

The code below will use the RSCG AutoInterface

usage code

The code that is generated by RSCG AutoInterface

gc code

More details about RSCG AutoInterface

The author of RSCG AutoInterface is beakona

You cand find RSCG AutoInterface at Nuget.org : https://www.nuget.org/packages/BeaKona.AutoInterfaceGenerator and the sources at https://github.com/beakona/AutoInterface

For more usage features please read : https://github.com/beakona/AutoInterface

https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Decorator

Author of AutoInterface , beakona

Short info about you , beakona

I am a dad and software developer with more than 15 years of working experience in various industries and technologies. I am working mostly on embedded and desktop solutions mainly in C and C#.

Why did you start this AutoInterface ?

After the Source Generator public announcement, I decided to give them a try. I realized that Source Generators would allow us to explore composition-over-inheritance in its new form (from C# perspective). Technically, this approach uses interface invocation (which impacts performance) but one can view it as one form of composition. I consider myself a language designer and I wanted to explore the mechanics of this approach (regardless of poor performance) on existing and reputable language that I use daily.

How do yourself use your AutoInterface ?

I use it experimentally as a tool that allows a a_class_b to be composed-of-members and yet acts as inherited-as. Young and experimental languages have similar concepts as an alternative to inheritance. Those who find this interesting can explore the approach chosen by Jonathan Blow in its language Jai

What other RSCG do you use ?

There is only one Source Generator that I use in production; AutoCoder and it is not public because I didn't find time to make it configurable and polished. It automatically implements ICoding interface for the target a_class_b (Bridge pattern). I have been using this approach for years because the existing infrastructure does not work for me. The main concept is inspired by Foundation framework a_class_b NSCoder but is implemented in the C# way. Abstraction-side has two directions IEncoding/IDecoding and there are two implementers: IEncoder and IDecoder. AutoCoder acts on behalf of the abstraction side by offering targeted fields/properties to IEncoder/IDecoder. The author of IEncoder/IDecoder can decide what, how, when, and in which context to encode/decode. In other words, AutoCoder automates boring stuff and does not make typos.

Any other feedback ?

If you have special, funny, or helpful paragraphs that acts like mentor or life coach concept I think there is worth mentioning TED talk: Why you will fail to have a great career by Larry Smith or book: "Hold on to Your Kids: Why Parents Need to Matter More Than Peers", 2019, by Dr. Gabor Maté and Gordon Neufeld.

RSCG number 14 : Property Expression Generator

What RSCG Property Expression Generator can do

This will generate code to add function to be used with Entity Framework to search for any property of a a_class_b

Example code

Here is the csproj with the references for RSCG Property Expression Generator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Property Expression Generator

usage code

The code that is generated by RSCG Property Expression Generator

gc code

More details about RSCG Property Expression Generator

The author of RSCG Property Expression Generator is Andrei Ignat

You cand find RSCG Property Expression Generator at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/PropertyExpressionGenerator

RSCG number 15 : Transplator

What RSCG Transplator can do

The Transplator is a small fast rendering engine to allow you to make rendering from any a_class_b instance.

Example code

Here is the csproj with the references for RSCG Transplator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG Transplator

usage code

The code that is generated by RSCG Transplator

gc code

More details about RSCG Transplator

The author of RSCG Transplator is Atif Aziz

You cand find RSCG Transplator at Nuget.org : https://www.nuget.org/packages/Transplator/ and the sources at https://github.com/atifaziz/Transplator/

For more usage features please read : https://github.com/atifaziz/Transplator/

https://github.com/ignatandrei/RSCG_Examples/tree/main/TemplateRender

RSCG number 16 : RSCG_AMS

What RSCG RSCG_AMS can do

The AMS will add in the CI the version and creator to your project.See https://netcoreblockly.herokuapp.com/ams for an example

Example code

Here is the csproj with the references for RSCG RSCG_AMS

csprj code

The initial code that you start with is

start code

The code below will use the RSCG RSCG_AMS

usage code

The code that is generated by RSCG RSCG_AMS

gc code

More details about RSCG RSCG_AMS

The author of RSCG RSCG_AMS is Andrei Ignat

You cand find RSCG RSCG_AMS at Nuget.org : https://www.nuget.org/packages/AMS_Base https://www.nuget.org/packages/AMSWebAPI https://www.nuget.org/packages/RSCG_AMS and the sources at https://github.com/ignatandrei/RSCG_AMS

For more usage features please read : https://github.com/ignatandrei/RSCG_AMS

https://github.com/ignatandrei/RSCG_Examples/tree/main/CI_Version

RSCG number 17 : HttpClientGenerator

What RSCG HttpClientGenerator can do

HttpClientGenerator is a tool that uses Roslyn code generator feature to write boilerplate HttpClient code for you.

Example code

Here is the csproj with the references for RSCG HttpClientGenerator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG HttpClientGenerator

usage code

The code that is generated by RSCG HttpClientGenerator

gc code

More details about RSCG HttpClientGenerator

The author of RSCG HttpClientGenerator is Jalal Amini Robati

You cand find RSCG HttpClientGenerator at Nuget.org : https://www.nuget.org/packages/HttpClientGenerator/ and the sources at https://github.com/Jalalx/HttpClientCodeGenerator

For more usage features please read : https://github.com/Jalalx/HttpClientCodeGenerator

https://github.com/ignatandrei/RSCG_Examples/tree/main/HttpClientCodeGenerator

Author of HttpClientGenerator , Jalal Amini Robati

  1. Short info about you , Jalal Amini Robati

I am working as a senior software engineer at Alibaba Travels.

  1. Why did you start this HttpClientGenerator ?

I built HttpClientGenerator to help users stop writing HttpClient code. Currently, some users use tools like swaggergen which generates too much code that is hard to maintain.

  1. How do yourself use your HttpClientGenerator ?

I am going to use it in a high-traffic website on production when it gets mature enough. I mostly use it on my personal projects and look for feedback from developers for now.

  1. What other RSCG do you use ?

Since the RSCG is in the early stages, I don't know that many tools but I think it can be applied to many areas like DI, ORMs. Currently Dapper.AOT is using this feature that would make writing database code so much faster.

RSCG number 18 : DatabaseToWebAPI

What RSCG DatabaseToWebAPI can do

This will generate code (WebAPI/Swagger) for any table/view from SqlServer. You can see the table via Angular

Example code

Here is the csproj with the references for RSCG DatabaseToWebAPI

csprj code

The initial code that you start with is

start code

The code below will use the RSCG DatabaseToWebAPI

usage code

The code that is generated by RSCG DatabaseToWebAPI

gc code

More details about RSCG DatabaseToWebAPI

The author of RSCG DatabaseToWebAPI is Andrei Ignat

You cand find RSCG DatabaseToWebAPI at Nuget.org : https://www.nuget.org/packages/QueryGenerator/ and the sources at https://github.com/ignatandrei/QueryViewer/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/QueryGenerator

RSCG number 19 : SourceInject

What RSCG SourceInject can do

Auto register services in startup

Example code

Here is the csproj with the references for RSCG SourceInject

csprj code

The initial code that you start with is

start code

The code below will use the RSCG SourceInject

usage code

The code that is generated by RSCG SourceInject

gc code

More details about RSCG SourceInject

The author of RSCG SourceInject is Giovanni Bassi

You cand find RSCG SourceInject at Nuget.org : https://www.nuget.org/packages/SourceInject/ and the sources at https://github.com/giggio/sourceinject

For more usage features please read : https://github.com/giggio/sourceinject

https://github.com/ignatandrei/RSCG_Examples/tree/main/AutoRegister

RSCG number 21 : BaseTypes

What RSCG BaseTypes can do

Generated tiny types from any value type

Example code

Here is the csproj with the references for RSCG BaseTypes

csprj code

The initial code that you start with is

start code

The code below will use the RSCG BaseTypes

usage code

The code that is generated by RSCG BaseTypes

gc code

More details about RSCG BaseTypes

The author of RSCG BaseTypes is Andreas Dorfer

You cand find RSCG BaseTypes at Nuget.org : https://www.nuget.org/packages/AndreasDorfer.BaseTypes/ and the sources at https://github.com/Andreas-Dorfer/base-types

For more usage features please read : https://github.com/Andreas-Dorfer/base-types

https://github.com/ignatandrei/RSCG_Examples/tree/main/TinyTypes

RSCG number 22 : AppSettingsEditor

What RSCG AppSettingsEditor can do

This will generate a_class_bes code from appsettings . Additionally , it generates API controller for editing and an UI interface

Example code

Here is the csproj with the references for RSCG AppSettingsEditor

csprj code

The initial code that you start with is

start code

The code below will use the RSCG AppSettingsEditor

usage code

The code that is generated by RSCG AppSettingsEditor

gc code

More details about RSCG AppSettingsEditor

The author of RSCG AppSettingsEditor is Andrei Ignat

You cand find RSCG AppSettingsEditor at Nuget.org : https://www.nuget.org/packages/appSettingsEditor/ and the sources at https://github.com/ignatandrei/appSettingsEditor

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/appSettingsEditor

RSCG number 23 : ApparatusAOT

What RSCG ApparatusAOT can do

This will generate code for investigating at runtime the properties of an object

Example code

Here is the csproj with the references for RSCG ApparatusAOT

csprj code

The initial code that you start with is

start code

The code below will use the RSCG ApparatusAOT

usage code

The code that is generated by RSCG ApparatusAOT

gc code

More details about RSCG ApparatusAOT

The author of RSCG ApparatusAOT is Stanislav Silin

You cand find RSCG ApparatusAOT at Nuget.org : https://www.nuget.org/packages/Apparatus.AOT.Reflection/ and the sources at https://github.com/byme8/Apparatus.AOT.Reflection

For more usage features please read : https://github.com/byme8/Apparatus.AOT.Reflection

https://github.com/ignatandrei/RSCG_Examples/tree/main/ApparatusAOT

Author of ApparatusAOT , Stanislav Silin

Short info about you , Stanislav Silin

I am a software developer. Interested in the app architecture and performance optimizations

Why did you start this ApparatusAOT ?

I am constantly looking for ways how to reduce the amount of boilerplate code. So, I decided to make a place where I can put all my findings. Maybe they will be useful for someone else. 'Apparatus' is the name of a community/organization. The 'AOT' is just a suffix for the indication that the tool is intended for the AOT scenarios.

How do yourself use your ApparatusAOT ?

Right now, ZeroIoc is actively used in one commercial project. Other stuff like AOT.Reflection, DuckInterface in just experiments to investigate the possibility and effectiveness of such tools.

What other RSCG do you use ?

At the moment I don't use any of them.

RSCG number 24 : RSCG_TimeBombComment

What RSCG RSCG_TimeBombComment can do

This will generate an error from the comment after a certain date

Example code

Here is the csproj with the references for RSCG RSCG_TimeBombComment

csprj code

The initial code that you start with is

start code

The code below will use the RSCG RSCG_TimeBombComment

usage code

The code that is generated by RSCG RSCG_TimeBombComment

gc code

More details about RSCG RSCG_TimeBombComment

The author of RSCG RSCG_TimeBombComment is Andrei Ignat

You cand find RSCG RSCG_TimeBombComment at Nuget.org : https://www.nuget.org/packages/RSCG_TimeBombComment/ and the sources at https://github.com/ignatandrei/RSCG_TimeBombComment

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/TimeBombComment

RSCG number 25 : StructRecordsGenerator

What RSCG StructRecordsGenerator can do

This will generate code .ToString. Usefull for debugging

Example code

Here is the csproj with the references for RSCG StructRecordsGenerator

csprj code

The initial code that you start with is

start code

The code below will use the RSCG StructRecordsGenerator

usage code

The code that is generated by RSCG StructRecordsGenerator

gc code

More details about RSCG StructRecordsGenerator

The author of RSCG StructRecordsGenerator is Sergey Teplyakov

You cand find RSCG StructRecordsGenerator at Nuget.org : https://www.nuget.org/packages/StructRecordGenerator/ and the sources at https://github.com/SergeyTeplyakov/StructRecordsGenerator

For more usage features please read : https://github.com/SergeyTeplyakov/StructRecordsGenerator

https://github.com/ignatandrei/RSCG_Examples/tree/main/DebuggerToString

RSCG number 26 : AOPMarkerCI

What RSCG AOPMarkerCI can do

This will tracing methods marked with AOPMarkerMethod in CI builds. Does not affect the code run by the programmer.

Example code

Here is the csproj with the references for RSCG AOPMarkerCI

csprj code

The initial code that you start with is

start code

The code below will use the RSCG AOPMarkerCI

usage code

The code that is generated by RSCG AOPMarkerCI

gc code

More details about RSCG AOPMarkerCI

The author of RSCG AOPMarkerCI is Andrei Ignat

You cand find RSCG AOPMarkerCI at Nuget.org : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/ and the sources at http://github.com/ignatandrei/aop_With_Roslyn/

For more usage features please read : http://msprogrammer.serviciipeweb.ro/category/roslyn/

https://github.com/ignatandrei/RSCG_Examples/tree/main/AOPMarkerCI

RSCG number 27 : BoilerplateFree

What RSCG BoilerplateFree can do

This will generate interface from a a_class_b

Example code

Here is the csproj with the references for RSCG BoilerplateFree

csprj code

The initial code that you start with is

start code

The code below will use the RSCG BoilerplateFree

usage code

The code that is generated by RSCG BoilerplateFree

gc code

More details about RSCG BoilerplateFree

The author of RSCG BoilerplateFree is Gustav Wengel

You cand find RSCG BoilerplateFree at Nuget.org : https://www.nuget.org/packages/boilerplatefree and the sources at https://github.com/GeeWee/boilerplatefree

For more usage features please read : https://github.com/GeeWee/boilerplatefree

https://github.com/ignatandrei/RSCG_Examples/tree/main/a_class_b2Interface

Author of BoilerplateFree , Gustav Wengel

Short info about you , Gustav Wengel

I'm a 28 year old software developer from Aarhus, Denmark. I work primarily within CleanTech as I think the climate emergency is one of the most pressing issues we face currently. I've worked with C# and .NET for about two years.

Why did you start this BoilerplateFree ?

Because I thought some of the limitations that the C# language offers sucked. I saw things like Project Lombok in the Java space that made the language much more ergonomic, and I wanted to see if I could bring some of that stuff into the C# space.

How do yourself use your BoilerplateFree ?

I use it for a few small-scale production projects. I think the jury is still out whether or not using source generators this way is a good idea or not, but so far it's been a real positive experience.

What other RSCG do you use ?

None yet.

Any other feedback ?

Nope!

Roslyn Source Code Generator (RSCG ) - others

There are more awesome RSCG that you could use - here is a list of 53 RSCG that you may want to look at:

Nr Name Description
1 chsienki/AutoEmbed Source generator to easily embed and access files + directory trees
2 mostmand/Cloneable Auto generate Clone method using C# Source Generator
3 jeromelaban/fonderie A source generator for the INotifyPropertyChanged interface using the Uno.SourceGenera tion framework
4 excubo-ag/Generators.Blazor This project improves the performance of Blazor components using source generators and provides helpful diagnostics.
5 excubo-ag/Generators.Grouping Some APIs have a lot of methods. When they all reside in the same ob ject, finding the right API call can be a challenge to the users. But replacing the structure of the API from a monolithic API to an API with intuitively named and smaller groups would break existing code. The solution is to offer both, without having to write the grouping yourself.
6 ladeak/JsonMergePatch Provides an implementation for Json Merge Patch, RFC7396. Library using C# source ge nerators to generate the types required for serialization. The Http package provides extension methods for HTTP requests, while the AspNetCore package provides an InputReader implementation.
7 Zoxive/MemoizeSourceGenerator Memoize
8 Tyrrrz/MiniRazor Portable Razor compiler & code generator
9 thomas-girotto/MockGen A C# mocking library based on source generators
10 Sholtee/proxygen .NET proxy generator powered by Roslyn
11 JasonBock/Rocks A mocking library based on the Compiler APIs (Roslyn + Mocks)
12 Jishun/RoslynWeave An AOP code generator
13 devlooped/SmallSharp Create, edit and run multiple C# 9.0 top-level programs in the same project by just selecting the startup program from the start button.
14 robertturner/StaticProxyGenerator Interface proxy generator. At compile time creates a_class_b th at implements target interface. Instantiation of the generated a_class_b accepts an InterceptorHandler which is called for all method calls
15 ufcpp/ValueChangedGenerator Roslyn Code Fix / Source Generator for generating PropertyChanged from inner struct members.
16 mattiasnordqvist/Web-Anchor Web Anchor provides type-safe, testable and flexible, runtime-generated access to web resources.
17 martinothamar/WrapperValueObject A .NET source generator for creating simple value objects wra pping primitive types.
18 gabriele-tomassetti/code-generation-roslyn Code generation with Roslyn and parsing w ith Sprache
19 YairHalberstadt/stronginject compile time dependency injection for .NET
20 thinktecture/article-roslyn-source-generators Roslyn Source Generators, Analyzers and Code Fixes
21 HamedFathi/MockableStaticGenerator A C# source generator to make an interface and a a_class_b wr apper to test static/extension methods.
22 mrtaikandi/MapTo A convention based object to object mapper using Roslyn source generator.
23 dominikjeske/Dnf.SourceGenerators SourceGenerators
24 Flash0ver/F0.Generators The open source of truth for general-purpose C# source code generators.
25 giggio/sourceinject A source generator for C# that uses Roslyn (the C# compiler) to allow you to generate y our dependencies injection during compile time.
26 anton-yashin/LightMock.Generator aot mock generator
27 sungaila/Cdelta A C# source generator for finite-state machines ??? easily referenced as a Roslyn analyzer.
28 JoshDiDuca/CodeSourceGenerator Generates code from templates using the new roslyn source generat or.
29 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
30 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
31 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
32 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
33 wieslawsoltes/Svg.Skia An SVG rendering library.
34 marlond18/EMDD.KtEquatable C# 9.0 Source Generator for IEnumerable of T
35 petarpetrovt/fast-enum-string A source generator for generating a faster extension method for con verting an enumeration value to string.
36 andrewlock/StronglyTypedId A Rosyln-powered generator for strongly-typed IDs
37 vrenken/EtAlii.Generators A set of Roslyn generators to simplify usage of some of the more mainstream frameworks/libraries.
38 ashmind/SourceMock SourceMock is a C# mocking framework based on source generators
39 SergeyTeplyakov/StructRecordsGenerator A set of generators helping dealing with structs in C#
40 mehmetakbulut/SignalR.Strong Strongly-typed calls from client to server and handlers for calls fro m server to client
41 canton7/RestEase Easy-to-use typesafe REST API client library for .NET Standard 1.1 and .NET Framework 4.5 and higher, which is simple and customisable. Inspired by Refit
42 byme8/DuckInterface Experiments for duck typing support in C#.
43 byme8/ZeroIoC ZeroIoC is reflectionless IoC Container for .NET
44 Cysharp/UnitGenerator C# Source Generator to create value-object, inspired by units of measure.
45 GeeWee/boilerplatefree Remove boilerplate via C# 9 source generators and attributes. Allows you to auto- generate interfaces from a_class_bes, constructors from fields and more
46 GeeWee/boilerplatefree Remove boilerplate via C# 9 source generators and attributes. Allows you to auto- generate interfaces from a_class_bes, constructors from fields and more
47 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
48 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
49 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
50 MelGrubb/BuilderGenerator A source-generator-based implementation of the Builder pattern
51 panoukos41/ViewBindingsGenerator A project that generates a partial a_class_b (view) for Activitie s/Fragments that will contain properties with the same name as their android:id in the xml view file.
52 alekshura/SourceMapper Mappings code generator based on attributes
53 Burgyn/MMLib.MediatR.Generators This generator generates controllers and their methods for you based on your MediatR requests.
# Final Chapter

Conclusion

I am happy that you have read / or practice / the RSCG presented in this book. I hope that inspired you to produce yours - and send me to review and maybe add to this book .

One hour of free consultancy

If you bought this book from Amazon, https://amzn.to/3f6gll3, you are entitled to have 1 hour free of consultancy with me.

Thanks ! Andrei