XirzoDIContainer 1.1.1
dotnet add package XirzoDIContainer --version 1.1.1
NuGet\Install-Package XirzoDIContainer -Version 1.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XirzoDIContainer" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XirzoDIContainer" Version="1.1.1" />
<PackageReference Include="XirzoDIContainer" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XirzoDIContainer --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XirzoDIContainer, 1.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XirzoDIContainer@1.1.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XirzoDIContainer&version=1.1.1
#tool nuget:?package=XirzoDIContainer&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
XirzoDIContainer π¦
Welcome to XirzoDIContainer! This is a lightweight Dependency Injection (DI) library designed to simplify the process of managing dependencies in your C# applications. π
Features β¨
- Singleton Binding with Instance: Bind a single instance that will always return the same instance. πͺ
- Singleton Binding with Factory: Bind a factory method that will return the same instance every time it's resolved. π
- Transient Binding: Bind a factory method that will return a new instance every time it's resolved. π
- Error Handling: Provides clear error messages for issues like unregistered types or multiple singleton bindings. π¨
Getting Started π
Hereβs a quick guide to get you started with XirzoDIContainer:
1. Define Your Services βοΈ
Create interfaces and their implementations.
public interface IGreetingService
{
void Greet(string name);
}
public class GreetingService : IGreetingService
{
public void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
}
2. Use the DI Container π οΈ
Register your services in the DI container.
Singleton with Instance
var container = new ContainerDi();
var instance = new GreetingService();
container.Bind<IGreetingService>()
.Instance(instance).AsSingleton();
// Resolve the service
var greetingService = container.Resolve<IGreetingService>().Value;
greetingService.Greet("World");
Singleton with Factory
var container = new ContainerDi();
container.Bind<IGreetingService>()
.Factory(() => new GreetingService()).AsSingleton();
// Resolve the service
var greetingService1 = container.Resolve<IGreetingService>().Value;
var greetingService2 = container.Resolve<IGreetingService>().Value;
// Both instances should be the same
Console.WriteLine(ReferenceEquals(greetingService1, greetingService2)); // True
Transient
var container = new ContainerDi();
container.Bind<IGreetingService>()
.Factory(() => new GreetingService()).AsTransient();
// Resolve the service
var greetingService1 = container.Resolve<IGreetingService>().Value;
var greetingService2 = container.Resolve<IGreetingService>().Value;
// Both instances should be different
Console.WriteLine(ReferenceEquals(greetingService1, greetingService2)); // False
Error Handling π―
Handle errors for unregistered types or other issues using the result object.
var container = new ContainerDi();
container.Bind<IMock>()
.Factory(() => new Mock())
.AsSingleton();
Result<IMock> resolveResult = container.Resolve<IMock>();
if (resolveResult.IsSuccess) {
var mock = resolveResult.Value;
// Use the resolved instance
mock.Foo();
} else {
Console.WriteLine("Failed to resolve IMock: " + resolveResult.ErrorMessage);
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.