{"id":13629431,"url":"https://github.com/flavien/QuickConstructor","last_synced_at":"2025-04-17T09:33:54.677Z","repository":{"id":85570517,"uuid":"486374014","full_name":"Flavien/quickconstructor","owner":"Flavien","description":"QuickConstructor is a reliable and feature-rich source generator that can automatically emit a constructor from the fields and properties of a class.","archived":false,"fork":false,"pushed_at":"2022-06-07T23:34:47.000Z","size":125,"stargazers_count":26,"open_issues_count":4,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-02T07:43:36.939Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Flavien.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,"roadmap":null,"authors":null}},"created_at":"2022-04-27T22:51:04.000Z","updated_at":"2024-05-07T23:37:45.000Z","dependencies_parsed_at":"2024-01-06T09:54:37.136Z","dependency_job_id":"6e60b3ca-d258-42bd-ab25-10c288cdb029","html_url":"https://github.com/Flavien/quickconstructor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flavien%2Fquickconstructor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flavien%2Fquickconstructor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flavien%2Fquickconstructor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flavien%2Fquickconstructor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flavien","download_url":"https://codeload.github.com/Flavien/quickconstructor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223597004,"owners_count":17170871,"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":[],"created_at":"2024-08-01T22:01:10.512Z","updated_at":"2024-11-08T20:30:57.492Z","avatar_url":"https://github.com/Flavien.png","language":"C#","readme":"# QuickConstructor\n[![QuickConstructor](https://img.shields.io/nuget/v/QuickConstructor.svg?style=flat-square\u0026color=blue\u0026logo=nuget)](https://www.nuget.org/packages/QuickConstructor/)\n\nQuickConstructor is a reliable and feature-rich source generator that can automatically emit a constructor from the fields and properties of a class. \n\n## Features\n\n- Decorate any class with the `[QuickConstructor]` attribute to automatically generate a constructor for that class.\n- The constructor updates in real-time as the class is modified.\n- Customize which fields and properties are initialized in the constructor.\n- Generate null checks automatically based on nullable annotations.\n- Works with nested classes and generic classes.\n- Supports derived classes.\n- Supports classes, records and structs.\n- Ability to place attributes on the parameters of the generated constructor.\n- No traces left after compilation, no runtime reference necessary.\n- Generate XML documentation automatically for the constructor.\n- Lightning fast thanks to the .NET 6.0 incremental source generator system.\n\n## Example\n\nCode without QuickConstructor:\n\n```csharp\npublic class Car\n{\n    private readonly string _registration;\n    private readonly string _model;\n    private readonly string _make;\n    private readonly string _color;\n    private readonly int _year;\n\n    public Car(string registration, string model, string make, string color, int year)\n    {\n        _registration = registration;\n        _model = model;\n        _make = make;\n        _color = color;\n        _year = year;\n    }\n}\n```\n\nWith QuickConstructor, this becomes:\n\n```csharp\n[QuickConstructor]\npublic partial class Car\n{\n    private readonly string _registration;\n    private readonly string _model;\n    private readonly string _make;\n    private readonly string _color;\n    private readonly int _year;\n}\n```\n\nThe constructor is automatically generated from the field definitions.\n\n## Installation\n\nThe requirements to use the QuickConstructor package are the following:\n\n- Visual Studio 17.0+\n- .NET SDK 6.0.100+\n\nInstall the NuGet package:\n\n```\ndotnet add package QuickConstructor\n```\n\n## Usage\n\nQuickConstructor is very easy to use. By simply decorating a class with the `[QuickConstructor]` attribute and making the class `partial`, the source generator will automatically create a constructor based on fields and properties declared in the class. The constructor will automatically update to reflect any change made to the class.\n\nQuickConstructor offers options to customize various aspects of the constructors being generated.\n\n### Fields selection\n\nQuick constructors will always initialize read-only fields as the constructor would otherwise cause a compilation error. However mutable fields can either be included or excluded from the constructor. This is controlled via the `Fields` property of the `[QuickConstructor]` attribute. The possible values are:\n\n| Value                          | Description |\n| ------------------------------ | ----------- |\n| `IncludeFields.ReadOnlyFields` | **(default)** Only read-only fields are initialized in the constructor. |\n| `IncludeFields.AllFields` | All fields are initialized in the constructor. |\n\nFields with an initializer are never included as part of the constructor.\n\n### Properties selection\n\nIt is possible to control which property is initialized in the constructor via the `Properties` property of the `[QuickConstructor]` attribute. The possible values are:\n\n| Value                    | Description |\n| ------------------------ | ----------- |\n| `IncludeProperties.None` | No property is initialized in the constructor. |\n| `IncludeProperties.ReadOnlyProperties` | **(default)** Only read-only auto-implemented properties are initialized in the constructor. |\n| `IncludeProperties.AllProperties` | All settable properties are initialized in the constructor. |\n\nProperties with an initializer are never included as part of the constructor.\n\n### Null checks\n\nQuickConstructor has the ability to generate null checks for reference parameters. This is controlled via the `NullCheck` property of the `[QuickConstructor]` attribute. The possible values are:\n\n| Value               | Description |\n| ------------------- | ----------- |\n| `NullChecks.Always` | Null checks are generated for any field or property whose type is a reference type. |\n| `NullChecks.Never` | Null checks are not generated for this constructor. |\n| `NullChecks.NonNullableReferencesOnly` | **(default)** When null-state analysis is enabled (C# 8.0 and later), a null check will be generated only if a type is marked as non-nullable. When null-state analysis is disabled, no null check is generated. |\n\nFor example, with null-state analysis enabled:\n\n```csharp\n[QuickConstructor]\npublic partial class Name\n{\n    private readonly string _firstName;\n    private readonly string? _middleName;\n    private readonly string _lastName;\n}\n```\n\nThis code will result in the following constructor being generated:\n\n```csharp\npublic Name(string firstName, string? middleName, string lastName)\n{\n    if (firstName == null)\n        throw new ArgumentNullException(nameof(firstName));\n\n    if (lastName == null)\n        throw new ArgumentNullException(nameof(lastName));\n\n    this._firstName = firstName;\n    this._middleName = middleName;\n    this._lastName = lastName;\n}\n```\n\n### Explicitely include a field or property\n\nIt is possible to explicitely include a field or property by decorating it with the `[QuickConstructorParameter]`.\n\nFor example:\n\n```csharp\n[QuickConstructor]\npublic partial class Vehicle\n{\n    [QuickConstructorParameter]\n    private int _mileage;\n\n    private int _speed;\n}\n```\n\nwill result in this constructor:\n\n```csharp\npublic Vehicle(int mileage)\n{\n    this._mileage = mileage;\n}\n```\n\nWhile both `_mileage` and `_speed` are mutable fields, and therefore are exluded by default, `_mileage` does get initialized in the constructor because it is decorated with `[QuickConstructorParameter]`.\n\n### Overriding the name of a parameter\n\nIt is possible to override the name of a parameter in the constructor using the `Name` property of the `[QuickConstructorParameter]` attribute.\n\nThis class:\n\n```csharp\n[QuickConstructor]\npublic partial class Vehicle\n{\n    [QuickConstructorParameter(Name = \"startingMileage\")]\n    private int _mileage;\n\n    private int _speed;\n}\n```\n\nwill result in this constructor:\n\n```csharp\npublic Vehicle(int startingMileage)\n{\n    this._mileage = startingMileage;\n}\n```\n\n### Derived classes\n\nIt is possible to generate a constructor for a class inheriting from a base class, however the base class must either itself be decorated with `[QuickConstructor]`, or it must have a parameterless constructor.\n\nFor example:\n\n```csharp\n[QuickConstructor(Fields = IncludeFields.AllFields)]\npublic partial class Vehicle\n{\n    private int _mileage;\n    private int _speed;\n}\n\n[QuickConstructor]\npublic partial class Bus : Vehicle\n{\n    private readonly int _capacity;\n}\n```\n\nIn that situation, a constructor will be generated for the `Bus` class, with the following implementation:\n\n```csharp\npublic Bus(int mileage, int speed, int capacity)\n    : base(mileage, speed)\n{\n    this._capacity = capacity;\n}\n```\n\n### Constructor accessibility\n\nIt is possible to customize the accessibility level of the auto-generated constructor. This is controlled via the `ConstructorAccessibility` property of the `[QuickConstructor]` attribute.\n\n## License\n\nCopyright 2022 Flavien Charlon\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and limitations under the License.\n","funding_links":[],"categories":["Content","Source Generators"],"sub_categories":["13. [QuickConstructor](https://ignatandrei.github.io/RSCG_Examples/v2/docs/QuickConstructor) , in the [Constructor](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#constructor) category","Other"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflavien%2FQuickConstructor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflavien%2FQuickConstructor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflavien%2FQuickConstructor/lists"}