{"id":22882369,"url":"https://github.com/loresoft/injectio","last_synced_at":"2025-04-08T09:05:56.183Z","repository":{"id":65381687,"uuid":"477474477","full_name":"loresoft/Injectio","owner":"loresoft","description":"Source generator that helps register attribute marked services in the dependency injection ServiceCollection","archived":false,"fork":false,"pushed_at":"2025-04-02T07:46:38.000Z","size":347,"stargazers_count":152,"open_issues_count":4,"forks_count":12,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-03T11:15:15.706Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loresoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"loresoft"}},"created_at":"2022-04-03T21:50:51.000Z","updated_at":"2025-04-02T07:46:35.000Z","dependencies_parsed_at":"2023-10-03T01:21:00.365Z","dependency_job_id":"7cc855e6-ab64-4f17-abde-8f4e217717ce","html_url":"https://github.com/loresoft/Injectio","commit_stats":{"total_commits":39,"total_committers":1,"mean_commits":39.0,"dds":0.0,"last_synced_commit":"f9b8e2a9d4c3acb0de0cfd57754314d0cd64984c"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loresoft%2FInjectio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loresoft%2FInjectio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loresoft%2FInjectio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loresoft%2FInjectio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loresoft","download_url":"https://codeload.github.com/loresoft/Injectio/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809963,"owners_count":20999816,"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-12-13T18:17:12.395Z","updated_at":"2025-04-08T09:05:56.157Z","avatar_url":"https://github.com/loresoft.png","language":"C#","readme":"# Injectio\n\nSource generator that helps register attribute marked services in the dependency injection ServiceCollection\n\n[![Build Project](https://github.com/loresoft/Injectio/actions/workflows/dotnet.yml/badge.svg)](https://github.com/loresoft/Injectio/actions/workflows/dotnet.yml)\n\n[![Coverage Status](https://coveralls.io/repos/github/loresoft/Injectio/badge.svg?branch=main)](https://coveralls.io/github/loresoft/Injectio?branch=main)\n\n[![Injectio](https://img.shields.io/nuget/v/Injectio.svg)](https://www.nuget.org/packages/Injectio/)\n\n[![Source generator](https://raw.githubusercontent.com/loresoft/Injectio/main/media/Injectio.Genertors.png)](https://github.com/loresoft/Injectio)\n\n## Features\n\n- Transient, Singleton, Scoped service registration\n- Factory registration\n- Module method registration\n- Duplicate Strategy - Skip,Replace,Append\n- Registration Strategy - Self, Implemented Interfaces, Self With Interfaces\n\n### Usage\n\n#### Add package\n\nAdd the nuget package project to your projects.\n\n`dotnet add package Injectio`\n\nPrevent dependances from including Injectio\n\n```xml\n\u003cPackageReference Include=\"Injectio\" PrivateAssets=\"all\" /\u003e\n```\n\n### Registration Attributes\n\nPlace registration attribute on class.  The class will be discovered and registered.\n\n- `[RegisterSingleton]` Marks the class as a singleton service\n- `[RegisterScoped]` Marks the class as a scoped service\n- `[RegisterTransient]` Marks the class as a transient service\n- `[RegisterServices]` Marks the method to be called to register services\n\n#### Attribute Properties\n\n| Property           | Description                                                                                         |\n|--------------------|-----------------------------------------------------------------------------------------------------|\n| ImplementationType | The type that implements the service.  If not set, the class the attribute is on will be used.      |\n| ServiceType        | The type of the service. If not set, the Registration property used to determine what is registered.|\n| Factory            | Name of a factory method to create new instances of the service implementation.                     |\n| Duplicate          | How the generator handles duplicate registrations. See Duplicate Strategy                           |\n| Registration       | How the generator determines what to register. See Registration Strategy                            |\n\n#### Duplicate Strategy\n\n| Value   | Description                                          |\n|---------|------------------------------------------------------|\n| Skip    | Skips registrations for services that already exists |\n| Replace | Replaces existing service registrations              |\n| Append  | Appends a new registration for existing services     |\n\n#### Registration Strategy\n\n| Value                 | Description                                                                           |\n|-----------------------|---------------------------------------------------------------------------------------|\n| Self                  | Registers each matching concrete type as itself                                       |\n| ImplementedInterfaces | Registers each matching concrete type as all of its implemented interfaces            |\n| SelfWithInterfaces    | Registers each matching concrete type as all of its implemented interfaces and itself |\n\n#### Singleton services\n\n```c#\n[RegisterSingleton]\npublic class SingletonService : IService { }\n```\n\nExplicit service type\n\n```c#\n[RegisterSingleton(ServiceType = typeof(IService))]\npublic class SingletonService : IService { }\n```\n\nSupport resolving multiple services with `IEnumerable\u003cT\u003e`\n\n```c#\n[RegisterSingleton(Duplicate = DuplicateStrategy.Append)]\npublic class SingletonService : IService { }\n```\n\n\n#### Scoped Services\n\n```c#\n[RegisterScoped]\npublic class ScopedService : IService { }\n```\n\n#### Transient Services\n\n```c#\n[RegisterTransient]\npublic class TransientService : IService { }\n```\n\n#### Factories\n\n```c#\n[RegisterTransient(Factory = nameof(ServiceFactory))]\npublic class FactoryService : IFactoryService\n{\n    private readonly IService _service;\n\n    public FactoryService(IService service)\n    { \n        _service = service;\n    }\n\n    public static IFactoryService ServiceFactory(IServiceProvider serviceProvider)\n    {\n        return new FactoryService(serviceProvider.GetService\u003cIService\u003e());\n    }\n}\n```\n\n#### Open Generic\n\n```c#\n[RegisterSingleton(ImplementationType = typeof(OpenGeneric\u003c\u003e), ServiceType = typeof(IOpenGeneric\u003c\u003e))]\npublic class OpenGeneric\u003cT\u003e : IOpenGeneric\u003cT\u003e { }\n```\n\n#### Generic Attributes\n\nYou can use generic attributes to register services if your project targets .NET 7.0+\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n  \u003cPropertyGroup\u003e\n    \u003cTargetFrameworks\u003enet7.0\u003c/TargetFrameworks\u003e\n  \u003c/PropertyGroup\u003e\n\u003c/Project\u003e\n```\n\nGeneric attributes allow declaration to be more compact by avoiding the typeof calls\n\n```c#\n[RegisterSingleton\u003cIService\u003e]\npublic class ServiceImplementation : IService { }\n```\n\n#### Keyed Services\n\nYou can register keyed services with version 8.0+ of `Microsoft.Extensions.DependencyInjection`\n\nRegister a keyed service\n\n```c#\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = \"Alpha\")]\npublic class ServiceAlphaKeyed : IServiceKeyed\n{\n}\n\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = \"Beta\")]\npublic class ServiceBetaKeyed : IServiceKeyed\n{\n}\n```\n\nRegister using an enum\n\n```c#\npublic enum ServiceType\n{\n    Alpha,\n    Beta\n}\n\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = ServiceType.Alpha)]\npublic class ServiceAlphaTypeKeyed : IServiceKeyed\n{\n}\n\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = ServiceType.Beta)]\npublic class ServiceBetaTypeKeyed : IServiceKeyed\n{\n}\n```\n\nRegister using an factory method\n\n```c#\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = \"Charlie\", Factory = nameof(ServiceFactory))]\n[RegisterSingleton\u003cIServiceKeyed\u003e(ServiceKey = \"Delta\", Factory = nameof(ServiceFactory))]\npublic class ServiceFactoryKeyed : IServiceKeyed\n{\n    public ServiceFactoryKeyed(object? serviceKey)\n    {\n        ServiceKey = serviceKey;\n    }\n\n    public object? ServiceKey { get; }\n\n    public static IServiceKeyed ServiceFactory(IServiceProvider serviceProvider, object? serviceKey)\n    {\n        return new ServiceFactoryKeyed(serviceKey);\n    }\n\n}\n```\n\n#### Register Method\n\nWhen the service registration is complex, use the `RegisterServices` attribute on a method that has a parameter of `IServiceCollection` or `ServiceCollection`\n\n```c#\npublic class RegistrationModule\n{\n    [RegisterServices]\n    public static void Register(IServiceCollection services)\n    {\n        // add and bind configuration options, Microsoft.Extensions.Configuration.Binder\n        services\n            .AddOptions\u003cPollingOption\u003e()\n            .Configure\u003cIConfiguration\u003e((settings, configuration) =\u003e configuration\n                .GetSection(PollingOption.SectionName)\n                .Bind(settings)\n            );\n    }\n}\n```\n\n#### Add to container\n\nThe source generator creates an extension method with all the discovered services registered.  Call the generated extension method to add the services to the container.  The extension method will be called `Add[AssemblyName]`.  The assembly name will have the dots removed.\n\n```c#\nvar services = new ServiceCollection();\nservices.AddInjectioTestsConsole();\n```\n\nOverride the extension method name by using the `InjectioName` MSBuild property.\n\n```xml\n\u003cPropertyGroup\u003e\n  \u003cInjectioName\u003eLibrary\u003c/InjectioName\u003e\n\u003c/PropertyGroup\u003e\n\u003cItemGroup\u003e\n \u003cCompilerVisibleProperty Include=\"InjectioName\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\n```c#\nvar services = new ServiceCollection();\nservices.AddLibrary();\n```\n\n#### Registration Tags\n\nControl what is registered when calling the generated extension method using Tags\n\nTag the service\n\n```c#\npublic interface IServiceTag\n{\n}\n\n[RegisterSingleton(Tags = \"Client,FrontEnd\")]\npublic class ServiceTag : IServiceTag\n{\n}\n```\n\nTags can be passed to register methods\n\n```c#\npublic static class ServiceRegistration\n{\n    [RegisterServices]\n    public static void Register(IServiceCollection services, ISet\u003cstring\u003e tags)\n    {\n\n    }\n}\n```\n\nSpecify tags when adding to service collection.  Note, if no tags specified, all services are registered\n\n```c#\nvar services = new ServiceCollection();\nservices.AddInjectioTestsLibrary(\"Client\");\n```\n","funding_links":["https://github.com/sponsors/loresoft"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floresoft%2Finjectio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floresoft%2Finjectio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floresoft%2Finjectio/lists"}