Skip to content

toucham/DICSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DICSharp

To be able to automatically register services without having to explicitly call the .Add{LIFETIME}() method. The features will include the following:

  • using attributes to register services as Singleton, Scope, or Transient.
  • only using the extension AddServicesToContainer() in Program.cs to register all the services that have the attribute
  • support both multiproject folder structure and simplified folder structure

Project

Structure

  • DIRegisterService contains the .NET 5.0+ lib with the logic for automatic service registeration extension.
  • Dev contains the ASP.NET project that contains the mock extension to automaticreated inside
  • OtherServices contains the code for testing multi-assemblies project

Possible Cases to Cover

These are all the possible features and cases that the auto-register services lib able to do. This means that the library will not get unexpected behavior in any of these cases.

  • add service as singleton lifetime
  • add service as transient lifetime
  • add service as scoped lifetime
  • resolve multiple implementations for one service
  • resolve service with generic
  • add services in multi-assemblies project

For multi-assemblies project, we are assuming that project reference assemblies will not have PublicKeyToken. It is currently analyzing the assemblies without the PublicKeyToken for the classes with the attribute, DILifetimeAttribute.

Dependency Injection

Multiple Implementations

If there are multiple implementations of a service registered, only the latest one is injected:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Services.AddSingleton<IMessageWriter, ConsoleMessageWriter>();
builder.Services.AddSingleton<IMessageWriter, LoggingMessageWriter>();
builder.Services.AddSingleton<ExampleService>();

In this case, LoggingMessagewriter is injected when a class has IMessageWriter in its constructor. A class can get all the implementations that are registered by wrapping the interface in IEnumerable<IMessageWriter>.

Implementation with Generic Service

public class StringGenericService : IGenericService<string> {
    ...
}

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Services.AddSingleton<IGenericService<T>, GenericService<T>>();

Services with Different Lifetime

It is possible to register the same services with different lifetime; however, the container will inject the lifetime from shortest to longest. For example:

services.AddTransient<IMyService, MyService>();
services.AddSingleton<IMyService, MyService>();

The service container will resolve with a transient MyService object, no matter where it is requested. Therefore, it is not RECOMMENDED to add different lifetime for one service.

About

auto service registration in .NET

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages