{"id":23514144,"url":"https://github.com/Swarley97/ConstructorGenerator","last_synced_at":"2025-08-28T08:31:17.450Z","repository":{"id":45229336,"uuid":"442915950","full_name":"Swarley97/ConstructorGenerator","owner":"Swarley97","description":"C# source generator which generates constructor for fields and properties","archived":false,"fork":false,"pushed_at":"2023-10-20T15:35:26.000Z","size":116,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-01T01:14:13.694Z","etag":null,"topics":["constructor","constructor-injection","csharp","csharp-sourcegenerator","generator","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/Swarley97.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-12-29T23:34:26.000Z","updated_at":"2025-06-10T14:39:37.000Z","dependencies_parsed_at":"2022-09-15T20:12:59.536Z","dependency_job_id":"1cf00185-814f-4a2c-a25c-4ae091470b5d","html_url":"https://github.com/Swarley97/ConstructorGenerator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Swarley97/ConstructorGenerator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swarley97%2FConstructorGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swarley97%2FConstructorGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swarley97%2FConstructorGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swarley97%2FConstructorGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Swarley97","download_url":"https://codeload.github.com/Swarley97/ConstructorGenerator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swarley97%2FConstructorGenerator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272470220,"owners_count":24939927,"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","status":"online","status_checked_at":"2025-08-28T02:00:10.768Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["constructor","constructor-injection","csharp","csharp-sourcegenerator","generator","source-generator"],"created_at":"2024-12-25T14:01:24.694Z","updated_at":"2025-08-28T08:31:17.087Z","avatar_url":"https://github.com/Swarley97.png","language":"C#","readme":"[![NuGet](https://img.shields.io/nuget/v/ConstructorGenerator.svg)](https://www.nuget.org/packages/ConstructorGenerator/)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/k94ll13nn3/AutoConstructor/main/LICENSE)\n![main_workflow](https://github.com/Swarley97/ConstructorGenerator/actions/workflows/main.yml/badge.svg)\n\n# ConstructorGenerator\n\nConstructorGenerator is a C# source generator which generates constructors for you.\n\n## Installation\n\nInstall the [ConstructorGenerator NuGet package](https://www.nuget.org/packages/ConstructorGenerator/).\n\n### Package Manager Console\n\n```powershell\nInstall-Package ConstructorGenerator\n```\n\n### .NET CLI\n\n```bash\ndotnet add package ConstructorGenerator\n```\n\n## Getting started\n\n### Prerequisites\n\nConstructorGenerator requires C# 9.0 or later.\n\n### Usage\n\nAdd the `[GenerateFullConstructor]` attribute to your class or struct and a constructor will be generated.\n\n```csharp\n[GenerateFullConstructor]\npublic partial class MyClass\n{\n    private readonly ILogger _logger;\n    private readonly IDependencyA _dependency;\n    private readonly IDependencyB _dependencyB;\n}\n```\n\nThe generated constructor will look like this:\n    \n```csharp\npublic MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)\n{\n    _logger = logger;\n    _dependency = dependency;\n    _dependencyB = dependencyB;\n}\n```\n### Rules\n\nOnly for the following members a constructor parameter will be generated\n- Read only fields with no initializer\n- Get only Properties or init only properties with no initializer \n- Properties or fields with the `[ConstructorDependency]` attribute (this override the rules above)\n\n**Examples**\n```csharp\n// ignored\nprivate readonly IDependency _dependency = new Dependency();\n```\n```csharp\n// not-ignored\nprivate readonly IDependency _dependency;\n```\n```csharp\n// not-ignored\n[ConstructorDependency]\nprivate readonly IDependency _dependency = new Dependency();\n```\n\n```csharp\n// ignored\nprivate readonly IDependency _dependency { get; } = new Dependency();\n```\n```csharp\n// ignored\nprivate readonly IDependency _dependency { get; set; }\n```\n```csharp\n// not-ignored\nprivate readonly IDependency _dependency { get; init; }\n```\n```csharp\n// not-ignored\n[ConstructorDependency]\nprivate readonly IDependency _dependency { get; set; }\n```\n\n### Ignoring properties and fields\n\nYou can ignore properties and fields by adding the `[ExcludeConstructorDependency]` attribute to them.\n\n```csharp\n[GenerateFullConstructor]\npublic partial class MyClass\n{\n    private readonly ILogger _logger;\n    private readonly IDependencyA _dependency;\n    \n    [ExcludeConstructorDependency]\n    private readonly IDependencyB _dependencyB;\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\npublic MyClass(ILogger logger, IDependencyA dependency)\n{\n    _logger = logger;\n    _dependency = dependency;\n}\n```\n\n### Explicitly specifying properties and fields\n\nYou can explicitly specify the properties and fields which should be included in the generated constructor by adding the `[ConstructorDependency]` attribute to them.\nThis can be useful for the following cases: \n- If you do not want to use the `[GenerateFullConstructor]` attribute on the class or struct\n- If you want to add additional constructor dependencies which are not included by default (see [Rules](#rules)) \n- If you want to specify it as optional parameter (see [Optional parameters](#optional-parameters))\n\n```csharp\npublic partial class MyClass\n{\n    [ConstructorDependency]\n    private readonly ILogger _logger = new NullLogger();\n    \n    [ConstructorDependency]\n    private readonly IDependencyA _dependency;\n    \n    private readonly IDependencyB _dependencyB;\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\npublic MyClass(ILogger logger, IDependencyA dependency)\n{\n    _logger = logger;\n    _dependency = dependency;\n}\n```\n\n### Inheritance\n\nConstructorGenerator supports inheritance. You can put the `[GenerateFullConstructor]` attribute on a derived class.\n\n```csharp\n\n[GenerateFullConstructor]\npublic MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)\n{\n    _logger = logger;\n    _dependency = dependency;\n    _dependencyB = dependencyB;\n}\n\n[GenerateFullConstructor]\npublic partial class MyDerivedClass : MyClass\n{\n    public IDependencyC DependencyC { get; }\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\npublic MyDerivedClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB, IDependencyC dependencyC)\n    : base(logger, dependency, dependencyB)\n{\n    DependencyC = dependencyC;\n}\n```\n\nOr, if you just want to generate the base constructor call (for example because your derived class have no constructor dependencies), \nyou can put the `[GenerateBaseConstructorCall]` attribute on the derived class.\n    \n```csharp\n\n// constructor of MyClass\npublic MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)\n{\n    _logger = logger;\n    _dependency = dependency;\n    _dependencyB = dependencyB;\n}\n\n[GenerateBaseConstructorCall]\npublic partial class MyDerivedClass : MyClass\n{\n\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\npublic MyDerivedClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)\n    : base(logger, dependency, dependencyB)\n{\n}\n```\n\n\u003e **Note**: It doesn't matter whether the base class constructor is also generated by the ConstructorGenerator or manually defined in the code. In either case, the constructor call will be generated.\n\n### Optional parameters\n\n```csharp\n[GenerateFullConstructor]\npublic partial class MyClass\n{\n    private readonly ILogger _logger;\n    private readonly IDependencyA _dependency;\n    [ConstructorDependency(IsOptional = true)]\n    private readonly IDependencyB _dependencyB;\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\npublic MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB = null)\n{\n    _logger = logger;\n    _dependency = dependency;\n    _dependencyB = dependencyB;\n}\n```\n\n### Execute code in the generated constructor\n\nTo execute code in the constructor (like attaching events or something like this) you can implement the partial method `OnConstructing` which will be called after all fields and properties has been set.\n```csharp\n[GenerateFullConstructor]\npublic partial class MyClass\n{\n    private readonly ILogger _logger;\n    private readonly IDependencyA _dependency;\n    private readonly IDependencyB _dependencyB;\n    \n    partial void OnConstructing()\n    {\n        // do constructor stuff\n        _logger.Log(\"Constructor called\");\n    }\n}\n```\n\n### Constructor accessibility\n\nConstructorGenerator supports specifying the accessibility of the generated constructor. You can put the `[ConstructorAccessibility]` attribute on a class or struct and the constructor will be generated with the specified accessibility.\n\n```csharp\n[GenerateFullConstructor]\n[ConstructorAccessibility(Accessibility.Internal)]\npublic partial class MyClass\n{\n    private readonly ILogger _logger;\n    private readonly IDependencyA _dependency;\n    private readonly IDependencyB _dependencyB;\n}\n```\n\nThe generated constructor will look like this:\n\n```csharp\n\ninternal MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)\n{\n    _logger = logger;\n    _dependency = dependency;\n    _dependencyB = dependencyB;\n}\n```\n\n## License\n\nConstructorGenerator is licensed under the [MIT license](LICENSE).","funding_links":[],"categories":["Content"],"sub_categories":["179. [ConstructorGenerator](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ConstructorGenerator) , in the [Constructor](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#constructor) category"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwarley97%2FConstructorGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSwarley97%2FConstructorGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwarley97%2FConstructorGenerator/lists"}