AutoCtor
A Roslyn source generator for creating constructors.
Install / Use
/learn @distantcam/AutoCtorREADME
AutoCtor
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
<a id='toc'></a>
<!-- toc -->Contents
- NuGet packages
- Examples
- Post Constructor Initialization
- Argument Guards
- Keyed Services
- Other
- Stats<!-- endToc -->
NuGet packages
https://nuget.org/packages/AutoCtor/
Examples
Basic
<!-- snippet: Basic --><a id='snippet-Basic'></a>
[AutoConstruct]
public partial class Basic
{
private readonly IService _service;
private readonly IList<string> _list = new List<string>();
}
<sup><a href='/src/Tests/ReadmeExamples/Basic.cs#L6-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-Basic' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Basic.cs#Basic.g.verified.cs --><a id='snippet-Basic.cs#Basic.g.verified.cs'></a>
//HintName: Basic.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------
partial class Basic
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public Basic(global::IService service)
{
this._service = service;
}
}
<sup><a href='/src/Tests/ReadmeExamples/Basic.cs%23Basic.g.verified.cs#L1-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-Basic.cs#Basic.g.verified.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> </details>Inherited
<!-- snippet: Inherited --><a id='snippet-Inherited'></a>
public abstract class BaseClass
{
protected IAnotherService _anotherService;
public BaseClass(IAnotherService anotherService)
{
_anotherService = anotherService;
}
}
[AutoConstruct]
public partial class Inherited : BaseClass
{
private readonly IService _service;
}
<sup><a href='/src/Tests/ReadmeExamples/Inherited.cs#L6-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-Inherited' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Inherited.cs#Inherited.g.verified.cs --><a id='snippet-Inherited.cs#Inherited.g.verified.cs'></a>
//HintName: Inherited.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------
partial class Inherited
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public Inherited(
global::IAnotherService anotherService,
global::IService service
) : base(anotherService)
{
this._service = service;
}
}
<sup><a href='/src/Tests/ReadmeExamples/Inherited.cs%23Inherited.g.verified.cs#L1-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-Inherited.cs#Inherited.g.verified.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> </details>Properties
<!-- snippet: Properties --><a id='snippet-Properties'></a>
// AutoCtor will initialize these
public string GetProperty { get; }
protected string ProtectedProperty { get; }
public string InitProperty { get; init; }
public required string RequiredProperty { get; set; }
// AutoCtor will ignore these
public string InitializerProperty { get; } = "Constant";
public string GetSetProperty { get; set; }
public string FixedProperty => "Constant";
public string RedirectedProperty => InitializerProperty;
<sup><a href='/src/Tests/ReadmeExamples/Properties.cs#L4-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-Properties' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Properties.cs#Properties.g.verified.cs --><a id='snippet-Properties.cs#Properties.g.verified.cs'></a>
//HintName: Properties.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------
partial class Properties
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public Properties(
string getProperty,
string protectedProperty,
string initProperty,
string requiredProperty
)
{
this.GetProperty = getProperty;
this.ProtectedProperty = protectedProperty;
this.InitProperty = initProperty;
this.RequiredProperty = requiredProperty;
}
}
<sup><a href='/src/Tests/ReadmeExamples/Properties.cs%23Properties.g.verified.cs#L1-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-Properties.cs#Properties.g.verified.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> </details><a href='#toc' title='Back to Contents'>Back to Contents</a>
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.
<a id='snippet-PostConstruct'></a>
[AutoConstruct]
public partial class PostConstruct
{
private readonly IService _service;
[AutoPostConstruct]
private void Initialize()
{
}
}
<sup><a href='/src/Tests/ReadmeExamples/PostConstruct.cs#L5-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-PostConstruct' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: PostConstruct.cs#PostConstruct.g.verified.cs --><a id='snippet-PostConstruct.cs#PostConstruct.g.verified.cs'></a>
//HintName: PostConstruct.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------
partial class PostConstruct
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public PostConstruct(global::IService service)
{
this._service = service;
Initialize();
}
}
<sup><a href='/src/Tests/ReadmeExamples/PostConstruct.cs%23PostConstruct.g.verified.cs#L1-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-PostConstruct.cs#PostConstruct.g.verified.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> </details>Extra Parameters
Post construct methods can also take parameters. The generated constructor will include these parameters.
<!-- snippet: PostConstructWithParameter --><a id='snippet-PostConstructWithParameter'></a>
[AutoConstruct]
public partial class PostConstructWithParameter
{
Related Skills
node-connect
339.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
