Skip to content

dojo-engineering/source-generators

Repository files navigation

Source generator project

There are several generators in this project: AutoInterface and AutoException

AutoInterface

The purpose of this generator is to save you from some boilerplate code. In many cases, we add an interface to a class to make it testable. However, every interface added like this pollutes a solution and adds some maintenance cost. The better approach is to automatically extract an interface with the source generator; the interface will always be up-to-date with the actual implementation.

In case you are using Visual Studio 2019 syntax highlighting can be broken. In order to fix this, please follow the following: http://stevetalkscode.co.uk/debug-source-generators-with-vs2019-1610

How to use:

  1. Add nuget package
dotnet add package Dojo.AutoGenerators
  1. Modify .csproj file like this:
<PackageReference Include="Dojo.AutoGenerators" Version="1.1.5" />
  <IncludeAssets>all</IncludeAssets>
  <PrivateAssets>analyzers</PrivateAssets>
</PackageReference>
  1. Make your class partial.
  2. Add AutoInterface attribute. It will automatically generate an interface and include all public methods.
  3. Delete manually created interface.

This code:

// Foo.cs
[AutoInterface]
public partial class Foo
{
    public ReturnType Method1()
    {
      // ...
    }
}

Will generate code quivalent to:

// IFoo.cs
public class IFoo
{
  ReturnType Method1();
}

// Foo.cs
public class Foo : IFoo
{
    public ReturnType Method1()
    {
      // ...
    }
}

You can also see generated code.

How it works

AutoException

Most of our exceptions are basic boilerplate and contain only standard constructors. Instead, you can use AutoException attribute:

using Dojo.Generators;

namespace DojoGeneratorTest.Sample
{
    [AutoException]
    public partial class TestException
    {
    }
}

Which will generate code equivalent to this one:

using System;
using System.Runtime.Serialization;

namespace DojoGeneratorTest.Sample
{
    public partial class TestException : Exception
    {
        public TestException()
        {
        }

        public TestException(string message) : base(message)
        {
        }

        public TestException(string message, Exception innerException) : base(message, innerException)
        {
        }

        protected TestException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }
}

AutoSettingsGenerator

The purpose of this generator is to generate settings POCOs from appsetting.json file. Additionally, extension code for IServiceCollection class is generated, that can be used for easy settings injection. All first level settings classes are auto-injected after calling the AddAppSettings auto-generated code.

How to use:

  1. Add nuget package
dotnet add package Dojo.AutoGenerators
  1. Inject and bind settings using autogenerated code:
public void ConfigureServices(IServiceCollection services)
{
    services.AddAppSettings(_configuration);
    ...
}