https://github.com/russkyc/dependency-injection
Minimal Dependency Injection for dotnet
https://github.com/russkyc/dependency-injection
csharp dependency-injection dependency-inversion di
Last synced: 3 months ago
JSON representation
Minimal Dependency Injection for dotnet
- Host: GitHub
- URL: https://github.com/russkyc/dependency-injection
- Owner: russkyc
- License: mit
- Created: 2023-04-28T07:04:39.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-17T18:36:06.000Z (over 1 year ago)
- Last Synced: 2025-02-26T07:11:35.101Z (3 months ago)
- Topics: csharp, dependency-injection, dependency-inversion, di
- Language: C#
- Homepage: https://www.nuget.org/packages/Russkyc.DependencyInjection
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Russkyc.DependencyInjection
Russkyc.DependencyInjection is a fast and minimal dependency injection container with auto dependency resolving.---
## What's New in 2.2.2
- Added Hosting style setup
**Sample with new setup**
```csharp
// Hosting setup
var host = ApplicationHost.CreateDefault();// Root is typeof(Window) so we call Show();
host.Root.Show();
```That simple! It automatically registers the attribute decorated services for the
root assembly and dependency assemblies. But you can also configure services manually as well
before calling the root```csharp
// Configuring services that don't have attribute decorations
host.ConfigureServices(services =>
{
// All the registration methods work here
services.AddSingleton();
});
```
That easy!## Basic Setup
### 1. Service Registration
Services are automatically registered to the container using the `[Service]` attribute and are injected using constructor injection.
Default
```csharp
[Service]
public class ConsoleLogger : ILogger
{
private string? _name;public void setName(string name)
{
_name = name;
}public void Log(string message)
{
Console.WriteLine(_name + message);
Debug.WriteLine(_name + message);
}
}
```With defined scope and registration conditions
```csharp
[Service(Scope.Transient, Registration.AsInterfaces)]
public partial class MainViewModel : ViewModelBase, IMainViewModel
{
private readonly ILogger _logger;public MainViewModel( ILogger logger)
{
_logger = logger;
_logger.Log("I am an injected service!");
}
}
```> **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_
### 2. Setup and Run in Application Entry
Use the `AddServices()` to resolve the services from the current assembly.
```csharp
// Build Container
var container = new ServicesCollection()
.AddServices() // Add Services From Entry Assembly
.AddServicesFromReferenceAssemblies() // Add Services From External Referenced Assemblies (Eg; Project References)
.Build();// You are now Ready to Go!
var window = container.Resolve();
window.Show();
```#### Working with External/Referenced Assemblies
If you want to work with specific projects/assemblies you can also use one of these methods before build:
- Specific assembly: `.AddServicesFromAssembly(assembly)`
- External assembly references of specific assembly: `.AddServicesFromReferenceAssemblies(assembly)````csharp
// Build Containervar assembly = Assembly.Load("AssemblyName"); // Specific Assembly
var container = new ServicesCollection()
.AddServicesFromAssembly(assembly) // Add Services From Assembly
.AddServicesFromAssemblyReferences(assembly) // Add Services From External Referenced Assemblies
.Build();
```
---## Manual Setup
You can also register dependencies manually if needed, this also works alongside the default setup.
### 1. Creating the Container```csharp
var services = new ServicesCollection()
.AddSingleton()
.AddSingleton()
.AddTransient()
.Build();
```### 2. Resolving
```csharp
var window = services.Resolve();
window.Show();
```> **NOTE:** No Attributes are required in manual setup. Registration is done manually using the `AddSingleton` and `AddTransient` methods
---
## Auto-resolved dependencies
Dependency auto-wiring is done through constructor injection. Here's what that looks like with a sample ViewModel with injected `ILogger` service
```csharp
public class MainViewModel : IMainViewModel
{
private readonly ILogger _logger;
// Constructor injection of logger
public MainViewModel(ILogger logger)
{
_logger = logger;
}
}
```---
## Accessing the container in other parts of the application
The container is automatically injected and can be accessed using a constructor injection of an `IServicesContainer`
```csharp
public MainViewModel(IServicesContainer container)
{
// Using the injected container
var logger = container.Resolve();
}
```---
## Sponsors
Special thanks to [JetBrains](https://www.jetbrains.com/) for supporting this project by providing licences to the JetBrains Suite!