Why You Must Use C# Source Generators

Why You Must Use C# Source Generators

Apr 27, 2022

If you are an engaged .NET Developer who makes sure to never stop learning, you have probably heard of C# Source Generators, a part of the Roslyn SDK that allows you to inspect user code as it is being compiled and create new source files which will be added to that compilation.
This means that developers now have the ability to automatically generate a lot of code they would've had to write manually, like boilerplate code.

Development Productivity

If you love programming, you probably like create new solutions and writing code, but let's be honest, writing the same code multiple times is boring and counter-productive. Let's check the following code

The code in the image is very simple, the only thing it does is returning an IQueryable of ApplicationRole, filtering by the RoleName property with the value received in the parameter roleName.

That's just one method for one table, now, let's say that you need to do a similar pattern for a lof of other tables and that all of them have a Name property.

  • ApplicationUser

  • Country

  • State

  • City

  • Company

  • Store

  • Product

  • Customer

These are 8 more methods to write, not big deal since they are small, but still, the process takes time, and is just a supporting piece of a system, and not a functional part.

Additionally, you probably do not need to write only one method to access a specific entity, but many more, for let's say a CRUD app, you will need methods to create entities, read all entities, read a specific entity, update a specific entity, and delete a specific entity, that is 5 methods, if you have 100 tables, that makes it 500 methods total to write, with the exact same pattern, just changing some names, extremely inefficient and boring process.

Code Styling

The best companies and developers out there have their established code styles and conventions, how to name the variables, the name conventions for the methods, in C#, things like where and when you should used "var" and when and where to use the type name instead, also when and where you should break the code into multiple lines, if you should return directly or instead assign the value to a variable first, and a lot more of other conventions.

While many companies have their coding styles and conventions, in practice, these are not always enforced and developers tend to miss/forget some of the rules.

Let's an example of how different developers might write a method

One developer could write it like this

While other developer could write it like this

The code does the same, the only thing that changed is the style, the second image has the Where in a new line.

While these kind of changes do not affect the functionality at all, they create visible differences when reading the code.

Now, since these piece of code have all the same pattern, it can be automatically generated, and as a result, all of the generated code will have the same coding styles.

To learn more about the potential of C# Source Generators, check my AutoGeneratedSystem POC in Github

You can support all of our Open Source Projects by donating clicking the Support button.

Enjoy this post?

Buy Eduardo Fonseca B a pizza

More from Eduardo Fonseca B