{"id":21701202,"url":"https://github.com/russkyc/dependency-injection","last_synced_at":"2025-08-31T17:35:35.992Z","repository":{"id":157927984,"uuid":"633738058","full_name":"russkyc/dependency-injection","owner":"russkyc","description":"Minimal Dependency Injection for dotnet","archived":false,"fork":false,"pushed_at":"2023-11-17T18:36:06.000Z","size":107,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-26T07:11:35.101Z","etag":null,"topics":["csharp","dependency-injection","dependency-inversion","di"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Russkyc.DependencyInjection","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/russkyc.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},"funding":{"github":"russkyc","patreon":"russkyc","custom":["https://paypal.me/jrcmo"]}},"created_at":"2023-04-28T07:04:39.000Z","updated_at":"2024-09-23T06:03:32.000Z","dependencies_parsed_at":"2023-11-17T18:45:19.041Z","dependency_job_id":null,"html_url":"https://github.com/russkyc/dependency-injection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/russkyc%2Fdependency-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/russkyc%2Fdependency-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/russkyc%2Fdependency-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/russkyc%2Fdependency-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/russkyc","download_url":"https://codeload.github.com/russkyc/dependency-injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244645356,"owners_count":20486971,"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","dependency-inversion","di"],"created_at":"2024-11-25T20:18:32.358Z","updated_at":"2025-03-20T15:44:47.399Z","avatar_url":"https://github.com/russkyc.png","language":"C#","funding_links":["https://github.com/sponsors/russkyc","https://patreon.com/russkyc","https://paypal.me/jrcmo"],"categories":[],"sub_categories":[],"readme":"\u003ch2 align=\"center\"\u003eRusskyc.DependencyInjection\u003c/h2\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.nuget.org/packages/Russkyc.DependencyInjection\"\u003e\n        \u003cimg src=\"https://img.shields.io/nuget/v/Russkyc.DependencyInjection?color=1f72de\" alt=\"Nuget\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"#\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/-.NET%202.0-blueviolet?color=1f72de\u0026label=NET\" alt=\"\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.nuget.org/packages/Russkyc.DependencyInjection\"\u003eRusskyc.DependencyInjection\u003c/a\u003e is a fast and minimal dependency injection container with auto dependency resolving.\n\u003c/p\u003e\n\n---\n\n## What's New in 2.2.2\n\n- Added Hosting style setup\n\n**Sample with new setup**\n\n```csharp\n// Hosting setup\nvar host = ApplicationHost\u003cMainView\u003e.CreateDefault();\n\n// Root is typeof(Window) so we call Show();\nhost.Root.Show();\n```\n\nThat simple! It automatically registers the attribute decorated services for the\nroot assembly and dependency assemblies. But you can also configure services manually as well\nbefore calling the root\n\n```csharp\n// Configuring services that don't have attribute decorations\nhost.ConfigureServices(services =\u003e\n{\n    // All the registration methods work here\n    services.AddSingleton\u003cILogger, ConsoleLogger\u003e();\n});\n```\nThat easy!\n\n## Basic Setup\n\n### 1. Service Registration\n\nServices are automatically registered to the container using the `[Service]` attribute and are injected using constructor injection.\n\nDefault\n```csharp\n[Service]\npublic class ConsoleLogger : ILogger\n{\n    private string? _name;\n\n    public void setName(string name)\n    {\n        _name = name;\n    }\n\n    public void Log(string message)\n    {\n        Console.WriteLine(_name + message);\n        Debug.WriteLine(_name + message);\n    }\n}\n```\n\nWith defined scope and registration conditions\n\n```csharp\n[Service(Scope.Transient, Registration.AsInterfaces)]\npublic partial class MainViewModel : ViewModelBase, IMainViewModel\n{\n    private readonly ILogger _logger;\n\n    public MainViewModel( ILogger logger)\n    {\n        _logger = logger;\n        _logger.Log(\"I am an injected service!\");\n    }\n}\n```\n\n\u003e **NOTE:** The scope and registration options are optional, each can be set if needed. By default the scope is _Scope.Tranient_ and the registration option is _Registration.AsSelf_\n\n\n### 2. Setup and Run in Application Entry\nUse the `AddServices()` to resolve the services from the current assembly.\n```csharp\n// Build Container\nvar container = new ServicesCollection()\n    .AddServices() // Add Services From Entry Assembly\n    .AddServicesFromReferenceAssemblies() // Add Services From External Referenced Assemblies (Eg; Project References)\n    .Build();\n\n// You are now Ready to Go!\nvar window = container.Resolve\u003cMainView\u003e();\nwindow.Show();\n```\n\n#### Working with External/Referenced Assemblies\nIf you want to work with specific projects/assemblies you can also use one of these methods before build:\n- Specific assembly: `.AddServicesFromAssembly(assembly)`\n- External assembly references of specific assembly: `.AddServicesFromReferenceAssemblies(assembly)`\n\n```csharp\n// Build Container\n\nvar assembly = Assembly.Load(\"AssemblyName\"); // Specific Assembly\n\nvar container = new ServicesCollection()\n    .AddServicesFromAssembly(assembly) // Add Services From Assembly\n    .AddServicesFromAssemblyReferences(assembly) // Add Services From External Referenced Assemblies\n    .Build();\n```\n---\n\n## Manual Setup\n\nYou can also register dependencies manually if needed, this also works alongside the default setup.\n### 1. Creating the Container\n\n```csharp\nvar services = new ServicesCollection()\n            .AddSingleton\u003cILogger, ConsoleLogger\u003e()\n            .AddSingleton\u003cIMainViewModel, MainViewModel\u003e()\n            .AddTransient\u003cMainView\u003e()\n            .Build();\n```\n\n### 2. Resolving\n\n```csharp\nvar window = services.Resolve\u003cMainView\u003e();\nwindow.Show();\n```\n\n\u003e **NOTE:** No Attributes are required in manual setup. Registration is done manually using the `AddSingleton` and `AddTransient` methods\n\n---\n\n## Auto-resolved dependencies\nDependency auto-wiring is done through constructor injection. Here's what that looks like with a sample ViewModel with injected `ILogger` service\n```csharp\npublic class MainViewModel : IMainViewModel\n{\n    private readonly ILogger _logger;\n    \n    // Constructor injection of logger\n    public MainViewModel(ILogger logger)\n    {\n        _logger = logger;\n    }\n}\n```\n\n---\n\n## Accessing the container in other parts of the application\n\nThe container is automatically injected and can be accessed using a constructor injection of an `IServicesContainer`\n\n```csharp\npublic MainViewModel(IServicesContainer container)\n{\n    // Using the injected container\n    var logger = container.Resolve\u003cILogger\u003e();\n}\n```\n\n---\n\n## Sponsors\nSpecial thanks to [JetBrains](https://www.jetbrains.com/) for supporting this project by providing licences to the JetBrains Suite!\n\n\u003ca href=\"https://www.jetbrains.com/community/opensource/#support\"\u003e\n\u003cimg width=\"200px\" src=\"https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png\" alt=\"JetBrains Logo (Main) logo.\"\u003e\n\u003c/a\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frusskyc%2Fdependency-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frusskyc%2Fdependency-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frusskyc%2Fdependency-injection/lists"}