{"id":13629430,"url":"https://github.com/distantcam/AutoCtor","last_synced_at":"2025-04-17T09:33:42.479Z","repository":{"id":44336526,"uuid":"512087796","full_name":"distantcam/AutoCtor","owner":"distantcam","description":"A Roslyn source generator for creating constructors.","archived":false,"fork":false,"pushed_at":"2024-04-13T11:40:12.000Z","size":441,"stargazers_count":75,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-13T15:00:55.660Z","etag":null,"topics":["csharp","csharp-sourcegenerator","dotnet","roslyn","source-generator"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/distantcam.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"distantcam"}},"created_at":"2022-07-09T03:56:12.000Z","updated_at":"2024-04-15T06:16:13.218Z","dependencies_parsed_at":"2023-12-09T01:37:13.408Z","dependency_job_id":"22161f58-cf48-4d5c-a65f-3b22949ea7be","html_url":"https://github.com/distantcam/AutoCtor","commit_stats":{"total_commits":56,"total_committers":3,"mean_commits":"18.666666666666668","dds":0.375,"last_synced_commit":"69360b61c80d6898ea156a7ae9837beeaa5502c7"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distantcam%2FAutoCtor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distantcam%2FAutoCtor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distantcam%2FAutoCtor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distantcam%2FAutoCtor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distantcam","download_url":"https://codeload.github.com/distantcam/AutoCtor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751107,"owners_count":17196572,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csharp-sourcegenerator","dotnet","roslyn","source-generator"],"created_at":"2024-08-01T22:01:10.465Z","updated_at":"2025-04-17T09:33:42.472Z","avatar_url":"https://github.com/distantcam.png","language":"C#","readme":"\u003c!--\nGENERATED FILE - DO NOT EDIT\nThis file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).\nSource File: /readme.source.md\nTo change this file edit the source file and then run MarkdownSnippets.\n--\u003e\n\n# AutoCtor\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/distantcam/autoctor/build.yml)](https://github.com/distantcam/AutoCtor/actions/workflows/build.yml)\n[![NuGet Status](https://img.shields.io/nuget/v/AutoCtor.svg)](https://www.nuget.org/packages/AutoCtor/)\n[![Nuget Downloads](https://img.shields.io/nuget/dt/autoctor.svg)](https://www.nuget.org/packages/AutoCtor/)\n\n\nAutoCtor is a Roslyn Source Generator that will automatically create a constructor for your class for use with constructor Dependency Injection.\n\n\u003ca id='toc'\u003e\u003c/a\u003e\n\u003c!-- toc --\u003e\n## Contents\n\n  * [NuGet packages](#nuget-packages)\n  * [Usage](#usage)\n    * [Your code](#your-code)\n    * [What gets generated](#what-gets-generated)\n  * [More Features](#more-features)\n    * [Post constructor Initialisation](#post-constructor-initialisation)\n    * [Initialise with parameters](#initialise-with-parameters)\n    * [Initialise readonly fields with ref or out](#initialise-readonly-fields-with-ref-or-out)\n    * [Argument Guards](#argument-guards)\n    * [Property Initialisation](#property-initialisation)\n  * [More examples](#more-examples)\n  * [Embedding the attributes in your project](#embedding-the-attributes-in-your-project)\n  * [Preserving usage of the `[AutoConstruct]` attribute](#preserving-usage-of-the-autoconstruct-attribute)\n  * [Stats](#stats)\u003c!-- endToc --\u003e\n\n## NuGet packages\n\nhttps://nuget.org/packages/AutoCtor/\n\n## Usage\n\n### Your code\n\n\u003c!-- snippet: Basic --\u003e\n\u003ca id='snippet-Basic'\u003e\u003c/a\u003e\n```cs\n[AutoConstruct]\npublic partial class ExampleClass\n{\n    private readonly IService _service;\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L6-L14' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-Basic' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n### What gets generated\n\n\u003c!-- snippet: BasicGeneratedCode --\u003e\n\u003ca id='snippet-BasicGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic ExampleClass(IService service)\n{\n    _service = service;\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L18-L23' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-BasicGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003ca href='#toc' title='Back to Contents'\u003eBack to Contents\u003c/a\u003e\n## More Features\n\n### Post constructor Initialisation\n\nYou can mark a method to be called at the end of the constructor with the attribute `[AutoPostConstruct]`. This method must return void.\n\n\u003c!-- snippet: PostConstruct --\u003e\n\u003ca id='snippet-PostConstruct'\u003e\u003c/a\u003e\n```cs\n[AutoConstruct]\npublic partial class PostConstructMethod\n{\n    private readonly IService _service;\n\n    [AutoPostConstruct]\n    private void Initialize()\n    {\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L10-L23' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstruct' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: PostConstructGeneratedCode --\u003e\n\u003ca id='snippet-PostConstructGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic PostConstructMethod(IService service)\n{\n    _service = service;\n    Initialize();\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L27-L33' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstructGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n### Initialise with parameters\n\nPost constructor methods can also take parameters. These parameters will be passed in from the constructor.\n\n\u003c!-- snippet: PostConstructWithParameters --\u003e\n\u003ca id='snippet-PostConstructWithParameters'\u003e\u003c/a\u003e\n```cs\npublic partial class PostConstructMethodWithParameters\n{\n    private readonly IService _service;\n\n    [AutoPostConstruct]\n    private void Initialize(IInitialiseService initialiseService)\n    {\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L36-L48' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstructWithParameters' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: PostConstructWithParametersGeneratedCode --\u003e\n\u003ca id='snippet-PostConstructWithParametersGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic PostConstructMethodWithParameters(IService service, IInitialiseService initialiseService)\n{\n    _service = service;\n    Initialize(initialiseService);\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L52-L58' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstructWithParametersGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n### Initialise readonly fields with ref or out\n\nIf a parameter is marked `ref` or `out` and matches the type of a readonly field, it can set that field during construction.\n\n\u003c!-- snippet: PostConstructWithOutParameters --\u003e\n\u003ca id='snippet-PostConstructWithOutParameters'\u003e\u003c/a\u003e\n```cs\npublic partial class PostConstructWithOutParameters\n{\n    private readonly IService _service;\n    private readonly IOtherService _otherService;\n\n    [AutoPostConstruct]\n    private void Initialize(IServiceFactory serviceFactory, out IService service)\n    {\n        service = serviceFactory.CreateService();\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L61-L75' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstructWithOutParameters' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: PostConstructWithOutParametersGeneratedCode --\u003e\n\u003ca id='snippet-PostConstructWithOutParametersGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic PostConstructWithOutParameters(IOtherService otherService, IServiceFactory serviceFactory)\n{\n    _otherService = otherService;\n    Initialize(serviceFactory, out _service);\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/PostConstructExamples.cs#L79-L85' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PostConstructWithOutParametersGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n### Argument Guards\n\nNull guards for the arguments to the constructor can be added in 2 ways.\n\nIn your project you can add a `AutoCtorGuards` property.\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n\n  \u003cPropertyGroup\u003e\n    \u003cAutoCtorGuards\u003etrue\u003c/AutoCtorGuards\u003e\n  \u003c/PropertyGroup\u003e\n\n\u003c/Project\u003e\n```\n\nIn each `AutoConstruct` attribute you can add a setting to enable/disable guards.\n\n\u003c!-- snippet: Guards --\u003e\n\u003ca id='snippet-Guards'\u003e\u003c/a\u003e\n```cs\n[AutoConstruct(GuardSetting.Enabled)]\npublic partial class GuardedClass\n{\n    private readonly Service _service;\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/GuardsExamples.cs#L5-L13' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-Guards' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: GuardsGeneratedCode --\u003e\n\u003ca id='snippet-GuardsGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic GuardedClass(Service service)\n{\n    _service = service ?? throw new ArgumentNullException(nameof(service));\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/GuardsExamples.cs#L18-L23' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-GuardsGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n### Property Initialisation\n\nAutoCtor can set properties that are considered as read only properties.\n\n\u003c!-- snippet: PropertyExamples --\u003e\n\u003ca id='snippet-PropertyExamples'\u003e\u003c/a\u003e\n```cs\n// AutoCtor will initialise these\npublic string GetProperty { get; }\nprotected string ProtectedProperty { get; }\npublic string InitProperty { get; init; }\npublic required string RequiredProperty { get; set; }\n\n// AutoCtor will ignore these\npublic string InitializerProperty { get; } = \"Constant\";\npublic string GetSetProperty { get; set; }\npublic string FixedProperty =\u003e \"Constant\";\npublic string RedirectedProperty =\u003e InitializerProperty;\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L80-L94' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PropertyExamples' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003ca href='#toc' title='Back to Contents'\u003eBack to Contents\u003c/a\u003e\n## More examples\n\nYou can also initialise readonly fields, and AutoCtor will not include them in the constructor.\n\n\u003c!-- snippet: PresetField --\u003e\n\u003ca id='snippet-PresetField'\u003e\u003c/a\u003e\n```cs\n[AutoConstruct]\npublic partial class ClassWithPresetField\n{\n    private readonly IService _service;\n    private readonly IList\u003cstring\u003e _list = new List\u003cstring\u003e();\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L26-L35' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PresetField' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: PresetFieldGeneratedCode --\u003e\n\u003ca id='snippet-PresetFieldGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic ClassWithPresetField(IService service)\n{\n    _service = service;\n    // no code to set _list\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L39-L45' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-PresetFieldGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nIf there is a single base constructor with parameters, AutoCtor will include that base constructor in the constructor it creates.\n\n\u003c!-- snippet: Inherit --\u003e\n\u003ca id='snippet-Inherit'\u003e\u003c/a\u003e\n```cs\npublic abstract class BaseClass\n{\n    protected IAnotherService _anotherService;\n\n    public BaseClass(IAnotherService anotherService)\n    {\n        _anotherService = anotherService;\n    }\n}\n\n[AutoConstruct]\npublic partial class ClassWithBase : BaseClass\n{\n    private readonly IService _service;\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L48-L66' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-Inherit' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003c!-- snippet: InheritGeneratedCode --\u003e\n\u003ca id='snippet-InheritGeneratedCode'\u003e\u003c/a\u003e\n```cs\npublic ClassWithBase(IAnotherService anotherService, IService service) : base(anotherService)\n{\n    _service = service;\n}\n```\n\u003csup\u003e\u003ca href='/src/AutoCtor.Example/BasicExamples.cs#L70-L75' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-InheritGeneratedCode' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\u003ca href='#toc' title='Back to Contents'\u003eBack to Contents\u003c/a\u003e\n## Embedding the attributes in your project\n\nBy 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:\n\n1. Define the MSBuild constant `AUTOCTOR_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project.\n2. Add `compile` to the list of excluded assets in your `\u003cPackageReference\u003e` element. This ensures the attributes in your project are referenced, instead of the _AutoCtor.Attributes.dll_ library.\n\nYour project file should look like this:\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n\n  \u003cPropertyGroup\u003e\n    \u003c!--  Define the MSBuild constant    --\u003e\n    \u003cDefineConstants\u003eAUTOCTOR_EMBED_ATTRIBUTES\u003c/DefineConstants\u003e\n  \u003c/PropertyGroup\u003e\n\n  \u003c!-- Add the package --\u003e\n  \u003cPackageReference Include=\"AutoCtor\"\n                    PrivateAssets=\"all\"\n                    ExcludeAssets=\"compile;runtime\" /\u003e\n\u003c!--                               ☝ Add compile to the list of excluded assets. --\u003e\n\n\u003c/Project\u003e\n```\n\n\u003ca href='#toc' title='Back to Contents'\u003eBack to Contents\u003c/a\u003e\n## Preserving usage of the `[AutoConstruct]` attribute\n\nThe `[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.\n\nIf you wish to preserve these attributes in the build output, you can define the `AUTOCTOR_USAGES` MSBuild variable.\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n\n  \u003cPropertyGroup\u003e\n    \u003c!--  Define the MSBuild constant    --\u003e\n    \u003cDefineConstants\u003eAUTOCTOR_USAGES\u003c/DefineConstants\u003e\n  \u003c/PropertyGroup\u003e\n\n  \u003c!-- Add the package --\u003e\n  \u003cPackageReference Include=\"AutoCtor\" PrivateAssets=\"all\" /\u003e\n\n\u003c/Project\u003e\n```\n\n\u003ca href='#toc' title='Back to Contents'\u003eBack to Contents\u003c/a\u003e\n## Stats\n\n![Alt](https://repobeats.axiom.co/api/embed/8d02b2c004a5f958b4365abad3d4d1882dca200f.svg \"Repobeats analytics image\")\n","funding_links":["https://github.com/sponsors/distantcam"],"categories":["Content","Source Generators"],"sub_categories":["14. [AutoCtor](https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoCtor) , in the [Constructor](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#constructor) category","Dependency Injection (IoC Container)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistantcam%2FAutoCtor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistantcam%2FAutoCtor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistantcam%2FAutoCtor/lists"}