Skip to content

Latest commit

 

History

History
227 lines (140 loc) · 6.23 KB

File metadata and controls

227 lines (140 loc) · 6.23 KB

AutoCtor

Build Status NuGet Status Nuget Downloads

AutoCtor is a Roslyn Source Generator that will automatically create a constructor for your class for use with constructor Dependency Injection.

+[AutoConstruct]
public partial class AService
{
    private readonly IDataContext _dataContext;
    private readonly IDataService _dataService;
    private readonly IExternalService _externalService;
    private readonly ICacheService _cacheService;
    private readonly ICacheProvider _cacheProvider;
    private readonly IUserService _userService;

-    public AService(
-        IDataContext dataContext,
-        IDataService dataService,
-        IExternalService externalService,
-        ICacheService cacheService,
-        ICacheProvider cacheProvider,
-        IUserService userService
-    )
-    {
-        _dataContext = dataContext;
-        _dataService = dataService;
-        _externalService = externalService;
-        _cacheService = cacheService;
-        _cacheProvider = cacheProvider;
-        _userService = userService;
-    }
}

Star History

Star History Chart

toc

NuGet packages

https://nuget.org/packages/AutoCtor/

Examples

Basic

snippet: Basic

What gets generated

snippet: Basic.cs#Basic.g.verified.cs

Inherited

snippet: Inherited

What gets generated

snippet: Inherited.cs#Inherited.g.verified.cs

Properties

snippet: Properties

What gets generated

snippet: Properties.cs#Properties.g.verified.cs

Back to Contents

Post Constructor Initialization

You can mark a method to be called at the end of the constructor with the attribute [AutoPostConstruct]. This method must return void.

snippet: PostConstruct

What gets generated

snippet: PostConstruct.cs#PostConstruct.g.verified.cs

Extra Parameters

Post construct methods can also take parameters. The generated constructor will include these parameters.

snippet: PostConstructWithParameter

What gets generated

snippet: PostConstructWithParameter.cs#PostConstructWithParameter.g.verified.cs

out/ref Parameters

Parameters marked with out or ref are set back to any fields that match the same type. This can be used to set readonly fields with more complex logic.

snippet: PostConstructWithOutParameter

What gets generated

snippet: PostConstructWithOutParameter.cs#PostConstructWithOutParameter.g.verified.cs

Optional Parameters

snippet: PostConstructWithDefaultParameter

What gets generated

snippet: PostConstructWithDefaultParameter.cs#PostConstructWithDefaultParameter.g.verified.cs

Back to Contents

Argument Guards

Null guards for the arguments to the constructor can be added in 2 ways.

In your project you can add a AutoCtorGuards property.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <AutoCtorGuards>true</AutoCtorGuards>
  </PropertyGroup>

</Project>

OR

In each AutoConstruct attribute you can add a setting to enable/disable guards.

snippet: Guarded

What gets generated

snippet: Guarded.cs#Guarded.g.verified.cs

Back to Contents

Keyed Services

When using Microsoft.Extensions.DependencyInjection you can mark fields and properties with [AutoKeyedService] and it will be included in the constructor.

snippet: Keyed

What gets generated

snippet: Keyed.cs#Keyed.g.verified.cs

Back to Contents

Other

Embedding The Attributes

By default, the [AutoConstruct] attributes referenced in your project are contained in an external dll. It is also possible to embed the attributes directly in your project. To do this, you must do two things:

  1. Define the constant AUTOCTOR_EMBED_ATTRIBUTES. This ensures the attributes are embedded in your project.
  2. Add compile to the list of excluded assets in your <PackageReference> element. This ensures the attributes in your project are referenced, instead of the AutoCtor.Attributes.dll library.

Your project file should look like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!--  Define the MSBuild constant    -->
    <DefineConstants>$(DefineConstants);AUTOCTOR_EMBED_ATTRIBUTES</DefineConstants>
  </PropertyGroup>

  <!-- Add the package -->
  <PackageReference Include="AutoCtor"
                    PrivateAssets="all"
                    ExcludeAssets="compile;runtime" />
  <!--                             ☝ Add compile to the list of excluded assets. -->

</Project>
What gets generated

snippet: GeneratedAttributeTests.cs#AutoConstructAttribute.g.verified.cs

Keeping Attributes In Code

The [AutoConstruct] attributes are decorated with the [Conditional] attribute, so their usage will not appear in the build output of your project. If you use reflection at runtime you will not find the [AutoConstruct] attributes.

If you wish to preserve these attributes in the build output, add the define constant AUTOCTOR_USAGES.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!--  Define the MSBuild constant    -->
    <DefineConstants>$(DefineConstants);AUTOCTOR_USAGES</DefineConstants>
  </PropertyGroup>

</Project>

Back to Contents

Stats

Alt