https://github.com/shuttle/shuttle.core.dependencyinjection
Add dependencies to `IServiceCollection` by convention.
https://github.com/shuttle/shuttle.core.dependencyinjection
Last synced: 9 months ago
JSON representation
Add dependencies to `IServiceCollection` by convention.
- Host: GitHub
- URL: https://github.com/shuttle/shuttle.core.dependencyinjection
- Owner: Shuttle
- License: bsd-3-clause
- Created: 2022-06-17T11:53:42.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-02T05:28:47.000Z (11 months ago)
- Last Synced: 2025-02-02T06:23:28.608Z (11 months ago)
- Language: C#
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Shuttle.Core.DependencyInjection
```
PM> Install-Package Shuttle.Core.DependencyInjection
```
Add components to `IServiceCollection` by convention:
```c#
IServiceCollection services = new ServiceCollection();
services
.FromAssembly(assembly)
.Add();
```
The above would be the simplest case and adds all types using either a matching interface (with the same name as the class prefixed with `I`) or the first interface found. The default service lifetime is `Singleton`.
In order to filter the types add a `Filter` function:
```c#
IServiceCollection services = new ServiceCollection();
services
.FromAssembly(assembly)
.Filter(type => type.Name.Equals("FilteredType", StringComparison.InvariantCultureIgnoreCase))
.Add();
```
If a particular interface should be used for a selected type it may be specified as follows:
```c#
IServiceCollection services = new ServiceCollection();
services
.FromAssembly(assembly)
.GetServiceType(type => typeof(ISomeInterface))
.Add();
```
The service lifetime may also be specified:
```c#
IServiceCollection services = new ServiceCollection();
services
.FromAssembly(assembly)
.GetServiceLifetime(type => ServiceLifetime.Transient)
.Add();
```
Since this is a builder interface all the bits may be used in combination:
```c#
IServiceCollection services = new ServiceCollection();
services
.FromAssembly(assembly)
.Filter(type => type.Name.Equals("FilteredType", StringComparison.InvariantCultureIgnoreCase))
.GetServiceType(type => typeof(ISomeInterface))
.GetServiceLifetime(type => ServiceLifetime.Transient)
.Add();
```