{"id":13629389,"url":"https://github.com/patrickklaeren/AutoRegisterInject","last_synced_at":"2025-04-17T09:33:33.677Z","repository":{"id":64568935,"uuid":"576702586","full_name":"patrickklaeren/AutoRegisterInject","owner":"patrickklaeren","description":"C# Source Generator to automatically register dependencies in Microsoft Dependency Injection Service Collection","archived":false,"fork":false,"pushed_at":"2024-06-18T07:45:47.000Z","size":92,"stargazers_count":71,"open_issues_count":2,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-07T07:06:01.834Z","etag":null,"topics":["csharp","dependency-injection","microsoft","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/patrickklaeren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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-12-10T17:49:20.000Z","updated_at":"2024-10-11T13:54:15.000Z","dependencies_parsed_at":"2024-01-06T02:09:31.324Z","dependency_job_id":"d0435472-2f37-478a-8649-6d48865b14dc","html_url":"https://github.com/patrickklaeren/AutoRegisterInject","commit_stats":{"total_commits":9,"total_committers":3,"mean_commits":3.0,"dds":0.4444444444444444,"last_synced_commit":"0e1a0e3fed09ed9a9feacd31b20ea424fff947ae"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickklaeren%2FAutoRegisterInject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickklaeren%2FAutoRegisterInject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickklaeren%2FAutoRegisterInject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickklaeren%2FAutoRegisterInject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickklaeren","download_url":"https://codeload.github.com/patrickklaeren/AutoRegisterInject/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751083,"owners_count":17196566,"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","dependency-injection","microsoft","source-generator"],"created_at":"2024-08-01T22:01:09.213Z","updated_at":"2024-11-08T20:30:50.635Z","avatar_url":"https://github.com/patrickklaeren.png","language":"C#","readme":"# AutoRegisterInject\n\nAutoRegisterInject, also referred to as ARI, is a C# source generator that will automatically create Microsoft.Extensions.DependencyInjection registrations for types marked with attributes.\n\nThis is a compile time alternative to reflection/assembly scanning for your injections or manually adding to the `ServiceCollection` every time a new type needs to be registered.\n\nFor example:\n\n```cs\nnamespace MyProject;\n\n[RegisterScoped]\npublic class Foo { }\n```\n\nwill automatically generate an extension method called `AutoRegister()` for `IServiceProvider`, that registers `Foo`, as scoped.\n\n```cs\ninternal IServiceCollection AutoRegister(this IServiceCollection serviceCollection)\n{\n    serviceCollection.AddScoped\u003cFoo\u003e();\n    return serviceCollection;\n}\n```\n\nIn larger projects, dependency injection registration becomes tedious and in team situations can lead to merge conflicts which can be easily avoided.\n\nAutoRegisterInject moves the responsibility of service registration to the owning type rather than external service collection configuration, giving control and oversight of the type that is going to be registered with the container.\n\n## Installation\n\nInstall the [Nuget](https://www.nuget.org/packages/AutoRegisterInject) package, and start decorating classes with ARI attributes.\n\nUse `dotnet add package AutoRegisterInject` or add a package reference manually:\n\n```xml\n\u003cPackageReference Include=\"AutoRegisterInject\" /\u003e\n```\n\n## Usage\n\nClasses should be decorated with one of four attributes:\n- `[RegisterScoped]`\n- `[RegisterSingleton]`\n- `[RegisterTransient]`\n- `[RegisterHostedService]`\n\nVariants for keyed and the service `Try` register pattern are also available:\n- `[TryRegisterScoped]`\n- `[TryRegisterSingleton]`\n- `[TryRegisterTransient]`\n- `[RegisterKeyedScoped]`\n- `[RegisterKeyedSingleton]`\n- `[RegisterKeyedTransient]`\n\nEach keyed attribute has a `Try` counterpart.\n\nRegister a class:\n\n```cs\n[RegisterScoped]\nclass Foo;\n```\n\nand get the following output:\n\n```cs\nserviceCollection.AddScoped\u003cFoo\u003e();\n```\n\nUpdate the service collection by invoking:\n\n```cs\nvar serviceCollection = new ServiceCollection();\nserviceCollection.AutoRegister();\nserviceCollection.BuildServiceProvider();\n```\n\nYou can now inject `Foo` as a dependency and have this resolved as scoped.\n\nAlternatively, you can register hosted services by:\n\n```cs\n[RegisterHostedService]\nclass Foo;\n```\n\nand get:\n\n```cs\nserviceCollection.AddHostedService\u003cFoo\u003e();\n```\n\n### Register as interface\n\nImplement one or many interfaces on your target class:\n\n```cs\n[RegisterTransient]\nclass Bar : IBar;\n```\n\nand get the following output:\n\n```cs\nserviceCollection.AddTransient\u003cIBar, Bar\u003e();\n```\n\n**Important note:** AutoRegisterInject is opinionated and `Bar` will only be registered with its implemented interface. ARI will **not** register `Bar`. `Bar` will always need to be resolved from `IBar` in your code.\n\nImplementing multiple interfaces will have the implementing type be registered for each distinct interface.\n\n```cs\n[RegisterTransient]\nclass Bar : IBar, IFoo, IBaz;\n```\n\nwill output the following:\n\n```cs\nserviceCollection.AddTransient\u003cIBar, Bar\u003e();\nserviceCollection.AddTransient\u003cIFoo, Bar\u003e();\nserviceCollection.AddTransient\u003cIBaz, Bar\u003e();\n```\n\n**Important note:** AutoRegisterInject is opinionated and `Bar` will only be registered with its implemented interfaces. ARI will **not** register `Bar`. `Bar` will always need to be resolved from `IBar`, `IFoo` or `IBaz` in your code.\n\n### Multiple assemblies\n\nIn addition to the `AutoRegister` extension method, every assembly that AutoRegisterInject is a part of, a `AutoRegisterFromAssemblyName` will be generated. This allows you to configure your service collection from one, main, executing assembly.\n\nGiven 3 assemblies, `MyProject.Main`, `MyProject.Services`, `MyProject.Data`, you can configure the `ServiceCollection` as such:\n\n```cs\nvar serviceCollection = new ServiceCollection();\nserviceCollection.AutoRegisterFromMyProjectMain();\nserviceCollection.AutoRegisterFromMyProjectServices();\nserviceCollection.AutoRegisterFromMyProjectData();\nserviceCollection.BuildServiceProvider();\n```\n\nAutoRegisterInject will remove illegal characters from assembly names in order to generate legal C# method names. `,`, `.` and ` ` will be removed.\n\n### Ignoring interfaces\n\nBy default ARI will register a type with all the interfaces it implements, however this excludes `System.IDisposable` and its `IAsyncDisposable` counterpart.\n\nYou can ignore interfaces by telling ARI to *explicitly* register with only declared interfaces in the given attributes:\n\n```cs\npublic interface IFoo { }\npublic interface IBar { }\n[RegisterScoped(typeof(IBar))]\npublic class Foo;\n```\n\nthis will result in `Foo` ONLY being registered as `IBar` and the following output:\n\n```cs\nserviceCollection.AddTransient\u003cIBar, Foo\u003e();\n```\n\n`IFoo` will be ignored entirely.\n\nWhere you want to register as multiple interfaces, you can pass multiple types.\n\n```cs\n[RegisterScoped(typeof(IBar), typeof(IFoo))]\npublic class Foo;\n```\n\nThis works for all applicable attributes.\n\n## License\n\nAutoRegisterInject is MIT licensed. Do with it what you please under the terms of MIT.\n","funding_links":[],"categories":["Content","Source Generators"],"sub_categories":["37. [AutoRegisterInject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoRegisterInject) , in the [DependencyInjection](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#dependencyinjection) category","Dependency Injection (IoC Container)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickklaeren%2FAutoRegisterInject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickklaeren%2FAutoRegisterInject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickklaeren%2FAutoRegisterInject/lists"}