Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/balazs-kis/dilite

DiLite /dɪˈlʌɪt/ is a minimalist, lightweight DI framework.
https://github.com/balazs-kis/dilite

csharp csharp-library dependency-injection dependency-injection-container inversion-of-control ioc ioc-container netcore netstandard

Last synced: 3 days ago
JSON representation

DiLite /dɪˈlʌɪt/ is a minimalist, lightweight DI framework.

Awesome Lists containing this project

README

        

![DiLite](/logo.png)

DiLite /dɪˈlʌɪt/ is a minimalist, lightweight DI framework. Registrations and resolutions will look very familiar to those accustomed to Autofac.

[![Build Status](https://github.com/balazs-kis/dilite/workflows/build/badge.svg "Build Status")](https://github.com/balazs-kis/dilite/actions?query=workflow%3A%22build%22)
[![Coverage Status](https://codecov.io/gh/balazs-kis/dilite/branch/master/graph/badge.svg)](https://codecov.io/gh/balazs-kis/dilite)
[![Nuget](https://img.shields.io/nuget/v/di-lite)](https://www.nuget.org/packages/di-lite)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Usage

Create a `ContainerBuilder`, make the registrations, then build your `Container`:
```csharp
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType().As();
var container = containerBuilder.Build();
```
Then simply resolve what you need from the `Container`:
```csharp
var myInstance = container.Resolve();
```

## Features

### 1. Single instance
Each resolution call to the container will create a new instance of the registered type, unless it is specified as a singleton during registration. To register a type as a singleton, just use the `AsSingleInstance` method:
```csharp
containerBuilder.RegisterType().As().AsSingleInstance();
```

### 2. As self
If no *alias* is specified during the registration of a type, it will be registered as itself.
```csharp
containerBuilder.RegisterType();
```
It can be resolved as:
```csharp
var myInstance = container.Resolve();
```

If another *alias* is specified, but the registration as itself is also needed, the `AsSelf` method must be used:
```csharp
containerBuilder.RegisterType().As().AsSelf();
```

### 3. Multiple registrations for the same *alias*
Multiple registrations can be made for the same *alias*:
```csharp
containerBuilder.RegisterType().As();
containerBuilder.RegisterType().As();
```
In this case, the `Resolve` method will return an instance of `MyClassImplementation2` because it was the last one registered for that *alias*. By using the `ResolveAll` method, all registrations for the *alias* will be returned:
```csharp
IEnumerable myInstances = container.ResolveAll();
```

### 4. Factory method registrations
Factory methods can also be registered as `Func`:
```csharp
containerBuilder.RegisterFactoryMethod(container =>
{
var dep1 = container.Resolve();
var dep2 = container.Resolve();
return new MyClass(dep1, dep2);
}).As();
```
These registrations can be resolved as their simple type counterparts:
```csharp
var myInstance = container.Resolve();
```