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;
- }
}https://nuget.org/packages/AutoCtor/
snippet: Basic
What gets generated
snippet: Basic.cs#Basic.g.verified.cs
snippet: Inherited
What gets generated
snippet: Inherited.cs#Inherited.g.verified.cs
snippet: Properties
What gets generated
snippet: Properties.cs#Properties.g.verified.cs
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
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
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
snippet: PostConstructWithDefaultParameter
What gets generated
snippet: PostConstructWithDefaultParameter.cs#PostConstructWithDefaultParameter.g.verified.cs
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
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
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:
- Define the constant
AUTOCTOR_EMBED_ATTRIBUTES. This ensures the attributes are embedded in your project. - Add
compileto 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
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>