{"id":17914376,"url":"https://github.com/enisn/dotnurseinjector","last_synced_at":"2025-03-23T23:30:49.528Z","repository":{"id":45178624,"uuid":"308292765","full_name":"enisn/DotNurseInjector","owner":"enisn","description":"Simple, lightweight \u0026 useful Dependency Injector for dotnet.","archived":false,"fork":false,"pushed_at":"2023-12-26T06:30:30.000Z","size":8220,"stargazers_count":54,"open_issues_count":8,"forks_count":5,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-19T00:24:05.485Z","etag":null,"topics":["attribute-injection","csharp","dependency-injection","dependency-injector","dotnet","dotnet-core","injector","ioc"],"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/enisn.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-29T10:26:49.000Z","updated_at":"2024-05-03T10:02:45.000Z","dependencies_parsed_at":"2023-12-26T07:55:48.949Z","dependency_job_id":null,"html_url":"https://github.com/enisn/DotNurseInjector","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enisn%2FDotNurseInjector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enisn%2FDotNurseInjector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enisn%2FDotNurseInjector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enisn%2FDotNurseInjector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enisn","download_url":"https://codeload.github.com/enisn/DotNurseInjector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186442,"owners_count":20574551,"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":["attribute-injection","csharp","dependency-injection","dependency-injector","dotnet","dotnet-core","injector","ioc"],"created_at":"2024-10-28T19:57:55.999Z","updated_at":"2025-03-23T23:30:48.507Z","avatar_url":"https://github.com/enisn.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DotNurseInjector\n\n\u003ctable border=\"0\"\u003e\n\u003ctr\u003e\n\u003ctd\u003e \u003cimg width=\"95\" src=\"https://raw.githubusercontent.com/enisn/DotNurseInjector/master/art/dotnurse-icon.png\" /\u003e \u003c/td\u003e\n\n\u003ctd\u003e\n\u003ch2\u003e.Nurse Injector\u003c/h2\u003e\nSimple, lightweight \u0026 useful Dependency Injector for dotnet.\n \u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n\n![.NET Core](https://github.com/enisn/DotNurseInjector/workflows/.NET%20Core/badge.svg)\n\u003ca href=\"https://www.nuget.org/packages/DotNurse.Injector/\"\u003e\n \u003cimg alt=\"Nuget\" src=\"https://img.shields.io/nuget/v/DotNurse.Injector?logo=nuget\u0026style=flat-square\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.codefactor.io/repository/github/enisn/dotnurseinjector\"\u003e\n \u003cimg src=\"https://www.codefactor.io/repository/github/enisn/dotnurseinjector/badge\" alt=\"CodeFactor\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://gitmoji.carloscuesta.me\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=flat-square\" alt=\"Gitmoji\"\u003e\u003c/a\u003e\n\n---\n\n## Getting Started\n\n- Install Nuget package [from here](https://www.nuget.org/packages/DotNurse.Injector.AspNetCore/).\n\n- Go to your **Startup.cs**, remove all your manual injections and use `AddServicesFrom()` method with namespace.\n\n  - If you have following pattern:\n  ```diff\n  - services.AddTransient\u003cIBookRepository, BookRepository\u003e();\n  - services.AddTransient\u003cIAuthorRepository, AuthorRepository\u003e();\n  - services.AddTransient\u003cIPublisherRepository, PublisherRepository\u003e();\n  ```\n  \n  - Replace them with following:\n\n  ```csharp\n  services.AddServicesFrom(\"MyCompany.ProjectName.Repositories.Concrete\"); // \u003c-- Your implementations namespace.\n  ```\n\n- ✅ That's it! DotNurse will scan your entire assembly and referenced assemblies to find types with the given namespace then register them to ServiceCollection.\n\n### Managing in Objects\nYou can even define lifetimes and expose types from objects via using [[RegisterAs]](#register-as-attribute) attribute.\n\n- Firstly, you should add following method for registering by attributes.\n  ```csharp\n  services.AddServicesByAttributes();\n  ```\n- Then you're ready to decorate your objects with `[RegisterAs]` attribute.\n  ```csharp\n  [RegisterAs(typeof(IBookRepository))]\n  public class BookRepository : IBookRepository, IAnotherInterface, IYetAnothetInterface\n  {\n  }\n  ```\n\n***\n\n## Property/Field Injection\n\u003e This section is **optional**. You can still use default Microsoft Dependency Injection and skip this step.\n\nYou can use attribute injection instead of constructor injection. Using `[InjectService]` attribute for properties or fields is enough to inject them from IoC.\n\n### Setting Up\n\nYou must replace your Service Provider with .Nurse Injecor to use **Attribute Injection**.\n\n- Go your **Program.cs** and add `UseDotNurseInjector()` method to IHostBuilder.\n\n```csharp\n public static IHostBuilder CreateHostBuilder(string[] args) =\u003e\n            Host.CreateDefaultBuilder(args)\n                .UseDotNurseInjector() // \u003c-- Adding this one is enough!\n                .ConfigureWebHostDefaults(webBuilder =\u003e\n                {\n                    webBuilder.UseStartup\u003cStartup\u003e();\n                });\n```\n\n\n### Usage\n```csharp\n[InjectService] public IBookRepository BookRepository { get; private set; }\n```\n\n```csharp\n[InjectService] protected IBookRepository bookRepository;\n```\n\n#### Refactor your old codes\nYou can remove long constructor and place attribute to your fields or properties.\n\n- Old code before DotNurseInjector\n```csharp\npublic class BookService \n{\n  public IBookRepository BookRepository { get; private set; }\n  private readonly BookManager _bookManager;\n  // ...\n  \n  public BookService(\n    IBookRepository bookRepository,\n    BookManager bookManager,\n    // ... \n    )\n  {\n     BookRepository = bookRepository;\n     _bookManager = bookManager;\n     // ...\n  }\n}\n```\n\n- New code after DotNutseInjector\n```csharp\npublic class BookService \n{\n  [InjectService] private IBookRepository BookRepository { get; set; }\n  [InjectService] private readonly BookManager _bookManager;\n  // ...\n}\n```\n\n***\n\n\u003cimg src=\"https://raw.githubusercontent.com/enisn/DotNurseInjector/master/art/dotnurse-github.png\" alt=\"dotnurse-injector-social-preview\" /\u003e\n\n***\n\n# Customizations\n\nDotNurse meets your custom requirements such as defining lifetime, injecting into different interfaces, etc.\n\n***\n\n## Managing from Startup\n.Nurse provides fluent API to manage your injections from a single point.\n\n### Service Lifetime\n\n```csharp\nservices.AddServicesFrom(\"MyCompany.ProjectName.Services\", ServiceLifetime.Scoped);\n```\n\n### Interface Selector\nYou can define a pattern to choose interface from multiple interfaces.\n\n```csharp\nservices.AddServicesFrom(\"ProjectNamespace.Services\", ServiceLifetime.Scoped, opts =\u003e\n{\n    opts.SelectInterface = interfaces =\u003e interfaces.FirstOrDefault(x =\u003e x.Name.EndsWith(\"Repository\"));\n});\n```\n\n### Implementation Selector\nYou can define a pattern to choose which objects will be injected into IoC. For example you have a base type in same namespace and you don't want to add it into service collection. You can use this feature:\n\n- ProjectNamespace.Services\n  - BookService\n  - ~~BaseService~~  \u003c- You want to ignore this\n  - AuthorService\n  - ...\n \n```csharp\nservices.AddServicesFrom(\"ProjectNamespace.Services\", ServiceLifetime.Scoped, opts =\u003e\n{\n    opts.SelectImplementtion = i =\u003e !i.Name.StartsWith(\"Base\");\n});\n```\n\n### Implementation Base\nThink about same scenario like previous, you can choose a base type to inject all classes which is inhetired from.\n\n```csharp\nservices.AddServicesFrom(\"ProjectNamespace.Services\", ServiceLifetime.Scoped, opts =\u003e\n{\n    opts.ImplementationBase = typeof(BaseRepository);\n});\n```\n\n### Custom Registration Rule\nYou can use lambda expression to define your custom rules for registration. \n\nIn example, you can use it for registering types in a namespace recursively.\n```csharp\n// Following code will register all types under Services namespace and sub-namespaces too.\nservices.AddServicesFrom(\n    x =\u003e x.Namespace != null \u0026\u0026 (x.Namespace.StartsWith(\"MyProject.Services\"));\n```\n\n*** \n\n## Managing from Objects\n\nYou can manage your injections for class by class.\n\n### Service Lifetime Attribute\n\n```csharp\n[ServiceLifeTime(ServiceLifetime.Singleton)] // \u003c-- Only this object will be Singleton.\npublic class MyRepository : IMyRepository\n{\n    // ...\n}\n```\n\n### Ignoring Registration by Attribute\nYou can ignore some of your class from injector.\n\n```csharp\n[DontRegister] // \u003c-- This object will not be added into services\npublic class MyRepository : IMyRepository\n{\n    // ...\n}\n```\n\n### Register As Attribute\nYou can manage your service types to add into. This attribute can be used multiple times at once.\n\n```csharp\n/* \n * Following object will be added into given types.\n */\n[RegisterAs(typeof(IBookRepository))]\n[RegisterAs(typeof(IBaseRepository\u003cBook\u003e), ServiceLifetime.Scoped)]\n[RegisterAs(typeof(BookRepository), ServiceLifetime.Singleton)]\npublic class BookRepository : IBookRepository\n{\n    // ...\n}\n```\nThis injection will do following code:\n\n```csharp\nservices.AddTransient\u003cIBookRepository, BookRepository\u003e();\nservices.AddScoped\u003cIBaseRepository\u003cBook\u003e\u003e, BookRepository\u003e();\nservices.AddSingleton\u003cBookRepository\u003e();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenisn%2Fdotnurseinjector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenisn%2Fdotnurseinjector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenisn%2Fdotnurseinjector/lists"}