{"id":24230780,"url":"https://github.com/chaowlert/primaryconstructor","last_synced_at":"2025-10-06T11:22:16.470Z","repository":{"id":41915772,"uuid":"307407597","full_name":"chaowlert/PrimaryConstructor","owner":"chaowlert","description":"Generate primary constructor from readonly fields","archived":false,"fork":false,"pushed_at":"2024-01-26T10:17:57.000Z","size":38,"stargazers_count":107,"open_issues_count":3,"forks_count":12,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-11T04:56:00.591Z","etag":null,"topics":["csharp-sourcegenerator","primary-constructor"],"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/chaowlert.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-26T14:54:23.000Z","updated_at":"2025-03-19T14:00:56.000Z","dependencies_parsed_at":"2024-06-21T13:09:50.297Z","dependency_job_id":"644fc9f2-1b7a-43c8-b6b0-182da7c9296b","html_url":"https://github.com/chaowlert/PrimaryConstructor","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/chaowlert/PrimaryConstructor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaowlert%2FPrimaryConstructor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaowlert%2FPrimaryConstructor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaowlert%2FPrimaryConstructor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaowlert%2FPrimaryConstructor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chaowlert","download_url":"https://codeload.github.com/chaowlert/PrimaryConstructor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaowlert%2FPrimaryConstructor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278599590,"owners_count":26013511,"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-10-06T02:00:05.630Z","response_time":65,"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":["csharp-sourcegenerator","primary-constructor"],"created_at":"2025-01-14T13:30:24.738Z","updated_at":"2025-10-06T11:22:16.454Z","avatar_url":"https://github.com/chaowlert.png","language":"C#","readme":"# PrimaryConstructor\nGenerate primary constructor from readonly fields \u0026 properties\n\n[![NuGet](https://img.shields.io/nuget/v/PrimaryConstructor.svg)](https://www.nuget.org/packages/PrimaryConstructor)\n\n![image](https://user-images.githubusercontent.com/5763993/97197488-4b65ad80-17e0-11eb-9eef-305ce284eb78.png)\n\n### Get it\n```\nPM\u003e Install-Package PrimaryConstructor\n```\n\n### Prerequisites\n\nVisual Studio version 16.8 and above is required as its first version to support source generators.\n\n### Usage\n\nDeclare class with `partial`, and annotate with `[PrimaryConstructor]`.  \nAnd then you can declare your dependencies with readonly fields.\n\n```csharp\n[PrimaryConstructor]\npublic partial class MyService\n{\n    private readonly MyDependency _myDependency;\n\n    ...\n}\n```\n\nWhen compile, following source will be injected.\n\n```csharp\npartial class MyService\n{\n    public MyService(MyDependency myDependency)\n    {\n        this._myDependency = myDependency;\n    }\n}\n```\n\n### Rules\n\n1. **readonly** fields \u0026 properties **without initializer** will be automatically injected.\n2. if the service has based type, based type must also annotated with `PrimaryConstructor` in order to inject members from based type.\n3. you can exclude members from injection by annotated with `IgnorePrimaryConstructor`.\n4. you can include members to injection by annotated with `IncludePrimaryConstructor`.\n\n### Emit generated files\n\nVisual Studio still not fully support source generator, it sometimes shows error marker on symbols referred to the generated code.\nEmitting the generated files will allow you to see the code, and also solve Visual Studio error marker problem.\n\nTo emit generated files, add following code to your `csproj` file.\n\n```xml\n\u003cPropertyGroup\u003e\n  \u003cCompilerGeneratedFilesOutputPath\u003e$(MSBuildProjectDirectory)/generated\u003c/CompilerGeneratedFilesOutputPath\u003e\n  \u003cEmitCompilerGeneratedFiles\u003etrue\u003c/EmitCompilerGeneratedFiles\u003e\n\u003c/PropertyGroup\u003e\n\n\u003cTarget Name=\"ExcludeGenerated\" BeforeTargets=\"AssignTargetPaths\"\u003e\n  \u003cItemGroup\u003e\n    \u003cGenerated Include=\"generated/**/*.g.cs\" /\u003e\n    \u003cCompile Remove=\"@(Generated)\" /\u003e\n  \u003c/ItemGroup\u003e\n  \u003cDelete Files=\"@(Generated)\" /\u003e\n\u003c/Target\u003e\n```\n\nPlease check [PrimaryConstructor.Sample.csproj](https://github.com/chaowlert/PrimaryConstructor/blob/main/PrimaryConstructor.Sample/PrimaryConstructor.Sample.csproj) for sample.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaowlert%2Fprimaryconstructor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchaowlert%2Fprimaryconstructor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaowlert%2Fprimaryconstructor/lists"}