{"id":13629115,"url":"https://github.com/crwsolutions/sourcedepend","last_synced_at":"2026-01-05T06:24:21.517Z","repository":{"id":60664365,"uuid":"538634244","full_name":"crwsolutions/sourcedepend","owner":"crwsolutions","description":"A source generator library for C# that simplifies Dependency Injection by automatically generating constructors with dependencies marked by the `[Dependency]` attribute.","archived":false,"fork":false,"pushed_at":"2024-07-15T09:50:01.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T17:05:50.543Z","etag":null,"topics":["dependency-injection","di","source-generators"],"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/crwsolutions.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":"2022-09-19T18:06:14.000Z","updated_at":"2025-01-09T17:23:45.000Z","dependencies_parsed_at":"2024-02-19T00:12:04.722Z","dependency_job_id":"25ece0a7-4462-400f-949d-98ad7b2de84a","html_url":"https://github.com/crwsolutions/sourcedepend","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crwsolutions%2Fsourcedepend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crwsolutions%2Fsourcedepend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crwsolutions%2Fsourcedepend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crwsolutions%2Fsourcedepend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crwsolutions","download_url":"https://codeload.github.com/crwsolutions/sourcedepend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249316054,"owners_count":21249884,"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":["dependency-injection","di","source-generators"],"created_at":"2024-08-01T22:01:02.758Z","updated_at":"2026-01-05T06:24:21.475Z","avatar_url":"https://github.com/crwsolutions.png","language":"C#","readme":"# Source Depend\n\nSimply tag members with a `[Dependency]` attribute and those will be added to the constructor, saving you from manual coding the constructor. Example:\n\n```csharp\npublic partial class ExampleService\n{\n    [Dependency]\n    private readonly IRepository repository;\n\n    // Etc...\n}\n```\n\nA C# source generator focused on dependency injection (DI). \n\n[![NuGet version (sourcedepend)](https://img.shields.io/nuget/v/sourcedepend?color=blue)](https://www.nuget.org/packages/sourcedepend/)\n[![License](https://img.shields.io/github/license/crwsolutions/sourcedepend.svg)](https://github.com/crwsolutions/sourcedepend/blob/master/LICENSE.txt)\n\n### Version history\n\n- v1.0 Handle more complex scenarios':\n    + class without namespace should not write a namespace.\n    + repeat type arguments of generics.\n    + repeat base constructor parameter if necessary.\n    + (added unit tests).\n- v0.4\\. Added null-checks.\n- v0.3\\. Complete Rewrite: reorganized the code.\n    + allow one level of inheritance. \n- v0.2\\. Complete rewrite from ISourceGenerator to IIncrementalGenerator, this should boost performance\n    + keep sealed and accessibility intact.\n- v0.1\\. First implementation.\n\n## How to use it\n\nInstall it and add the attribute to the fields or properties you want be set in your constructor, like so:\n\n```csharp\npublic partial class ExampleService\n{\n    [Dependency]\n    private readonly AnotherService anotherService;\n\n    [Dependency]\n    AnotherService Prop { get; }\n}\n```\n\n### Alternative assignment\n\nIt is also possible that the generated assignment is to an alternative property:\n\n```csharp\npublic partial class ExampleService\n{\n    [Dependency(nameof(BindingContext))]\n    AnotherService ViewModel =\u003e BindingContext as AnotherService;\n}\n```\n\n### Inheritance\n\nAnd it is possible to inherit from a base implementation that also uses the **\\[Dependency\\]** attribute:\n\n```csharp\ninternal partial class BaseExampleService\n{\n    [Dependency]\n    private readonly IForBaseService _someBaseService;\n}\n\ninternal partial class ExampleService : BaseExampleService\n{\n\n}\n```\n### Add construction work\n\nBecause your constructor is highjacked, there are the alternative methods PreConstruct/PostConstruct to do your construction work:\n\n```csharp\npublic partial class ExampleService\n{\n    [Dependency]\n    private readonly AnotherService anotherService;\n\n    ///This method will be called before the generated assignments\n    partial void PreConstruct()\n    {\n        Initialize()\n    }\n\n    ///This method will be called after the generated assignments\n    partial void PostConstruct() =\u003e anotherService.ConstructValue = \"Hello from post-construct!\";\n}\n```\n\nThese samples give the following combined generated code:\n\n```csharp\n// \u003cauto-generated/\u003e\n#pragma warning disable\n#nullable enable\nnamespace ConsoleApp\n{\n    /// \u003cinheritdoc/\u003e\n    public partial class ExampleService\n    {\n        public ExampleService(ConsoleApp.IAnotherService anotherService, ConsoleApp.AnotherService prop, ConsoleApp.AnotherService viewModel, ConsoleApp.IForBaseService someBaseService) : base(someBaseService)\n        {\n\n#if NET6_0_OR_GREATER\n            ArgumentNullException.ThrowIfNull(anotherService);\n            ArgumentNullException.ThrowIfNull(prop);\n            ArgumentNullException.ThrowIfNull(viewModel);\n#endif\n            PreConstruct();\n\n            this.anotherService = anotherService;\n            Prop = prop;\n            BindingContext = viewModel;\n\n            PostConstruct();\n        }\n\n        partial void PreConstruct();\n        partial void PostConstruct();\n    }\n}\n\n// \u003cauto-generated/\u003e\n#pragma warning disable\n#nullable enable\nnamespace ConsoleApp\n{\n    /// \u003cinheritdoc/\u003e\n    internal partial class BaseExampleService\n    {\n        public BaseExampleService(ConsoleApp.IForBaseService someBaseService)\n        {\n\n#if NET6_0_OR_GREATER\n            ArgumentNullException.ThrowIfNull(someBaseService);\n#endif\n            PreConstruct();\n\n            this._someBaseService = someBaseService;\n\n            PostConstruct();\n        }\n\n        partial void PreConstruct();\n        partial void PostConstruct();\n    }\n}\n```\n\n## Installing\n\nThe package is available [on NuGet](https://www.nuget.org/packages/sourcedepend).\nTo install from the command line:\n\n```shell\ndotnet add package sourcedepend\n```\n\nOr use the Package Manager in Visual Studio.\n\n## Contributing\n\nThe main supported IDE for development is Visual Studio 2019.\n\nQuestions, comments, bug reports, and pull requests are all welcome.\nBug reports that include steps to reproduce (including code) are\npreferred. Even better, make them in the form of pull requests.\n\n## Maintainers/Core team\n\nContributors can be found at the [contributors](https://github.com/crwsolutions/sourcedepend/graphs/contributors) page on Github.\n\n## License\n\nThis software is open source, licensed under the MIT License.\nSee [LICENSE](https://github.com/crwsolutions/sourcedepend/blob/master/LICENSE) for details.\nCheck out the terms of the license before you contribute, fork, copy or do anything\nwith the code. If you decide to contribute you agree to grant copyright of all your contribution to this project and agree to\nmention clearly if do not agree to these terms. Your work will be licensed with the project at MIT, along the rest of the code.\n","funding_links":[],"categories":["Content","Source Generators"],"sub_categories":["113. [sourcedepend](https://ignatandrei.github.io/RSCG_Examples/v2/docs/sourcedepend) , 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%2Fcrwsolutions%2Fsourcedepend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrwsolutions%2Fsourcedepend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrwsolutions%2Fsourcedepend/lists"}