https://github.com/xirzo/xirzodicontainer
Lightweight Dependency Injection library
https://github.com/xirzo/xirzodicontainer
dependency-injection dicontainer
Last synced: about 2 months ago
JSON representation
Lightweight Dependency Injection library
- Host: GitHub
- URL: https://github.com/xirzo/xirzodicontainer
- Owner: xirzo
- Created: 2025-01-16T11:58:16.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-02-22T18:04:56.000Z (3 months ago)
- Last Synced: 2025-02-22T18:23:58.222Z (3 months ago)
- Topics: dependency-injection, dicontainer
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# XirzoDIContainer 📦

[](https://github.com/xirzo/XirzoDIContainer/commits/main)Welcome to **XirzoDIContainer**! This is a lightweight Dependency Injection (DI) library designed to simplify dependency management in your C# applications. 🚀
## Features ✨
- **Type Binding**: Bind concrete types directly with `BindType()` 🎯
- **Interface Binding**: Map interfaces to implementations using `Bind().To()` 🔄
- **Instance Binding**: Bind existing instances using `ToInstance()` 📦
- **Factory Binding**: Create custom instantiation logic with `ToFactory()` 🏭
- **Lifetime Management**:
- Singleton: One instance for all resolutions
- Transient: New instance per resolution
- **Fluent API**: Intuitive and chainable configuration methods 🔗## Getting Started 🌟
### Basic Usage
1. Create your container:
```csharp
var container = new ContainerDi();
```2. Register your dependencies:
#### Direct Type Binding
```csharp
// Singleton
container.BindType()
.AsSingleton();// Transient
container.BindType()
.AsTransient();
```#### Interface to Implementation Binding
```csharp
// Singleton
container.Bind()
.To()
.AsSingleton();// Transient
container.Bind()
.To()
.AsTransient();
```#### Instance Binding
```csharp
var myInstance = new MyService();
container.Bind()
.ToInstance(myInstance);
```#### Factory Binding
```csharp
container.Bind()
.ToFactory(() => new MyService());
```### Resolving Dependencies
```csharp
// Resolve your service
var service = container.Resolve();
```## Best Practices 🎯
1. **Singleton vs Transient**:
- Use `AsSingleton()` when you need the same instance throughout your application
- Use `AsTransient()` when you need a new instance each time2. **Instance Binding**:
- Use `ToInstance()` when you have pre-configured instances
- Note: You cannot bind multiple instances to the same type3. **Factory Binding**:
- Use `ToFactory()` when you need custom instantiation logic
- Factories always create new instances## Example Scenario 📝
```csharp
public interface IGreetingService
{
void Greet();
}public class GreetingService : IGreetingService
{
public void Greet()
{
Console.WriteLine("Hello!");
}
}// Setup container
var container = new ContainerDi();// Register as singleton
container.Bind()
.To()
.AsSingleton();// Resolve and use
var service = container.Resolve();
service.Greet();
```