Skip to content

MicroDI is a simple and lightweight DI Container.

License

Notifications You must be signed in to change notification settings

PaulBraetz/MicroDI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MicroDI

MicroDI is a simple and lightweight DI Container.

Features

  1. Constructor Injection via Delegate Generation using Expressions
  2. Named or Typed Service Definition
  3. Smart Constructor Detection Using Provided Arguments and Registered Services

Installation

Nuget Gallery: https://www.nuget.org/packages/RhoMicro.MicroDI

.Net CLI: dotnet add package RhoMicro.MicroDI --version 4.3.1

Package Manager: Install-Package RhoMicro.MicroDI -Version 4.3.1

How To Use

  1. Instantiate a new Container
IContainer container = new Container();
  1. Construct an IServiceRegistration and add it to the container
Type serviceType = typeof(TService);
String serviceName = "MyService";

IServiceDefinition definition = new ServiceDefinition(serviceType, serviceName);

Type implementationType = typeof(TImplementation);
Object constructorArg1 = new Object();
Object constructorArg2 = new Object();

IServiceFactoryInstructions instructions = new ServiceFactoryInstructions(serviceImplementationType, new Object[] { arg1, arg2 });

IServiceFactory factory = new TransientServiceFactory(instructions);
			
IServiceRegistration registration = new ServiceRegistration(definition, factory);
container.Add(registration);
  1. Resolve your service
Type serviceType = typeof(TService);
String serviceName = "MyService";

IServiceDefinition definition = new ServiceDefinition(serviceType, serviceName);

Object service = container.Resolve(definition);

Extension Methods

Extension Methods found in Extensions.cs simplify using the container. The previous workflow is reduced to the following:

  1. Instantiate a new Container
IContainer container = new Container();
  1. Add a service registration to the container
Object constructorArg1 = new Object();
Object constructorArg2 = new Object();
container.AddTransient<TService, TImplementation>("MyService", new Object[]{constructorArg1, constructorArg2});
  1. Resolve your service by type or name
TService service = container.Resolve<TService>();
service = container.Resolve<TService>("MyService");

Note: The provided Container class makes use of ServiceDefinitionEqualityComparer.cs for definition comparisons and resolving services based on definitions. Two IServiceDefinitions are defined as equal when both their ServiceName and ServiceType fields are equal.

Implementing your own Service Factory

Using Helpers.GetConstructorExpression you may create your own IServiceFactory. Looking at the implementation of TransientServiceFactory may help you with getting started.

About

MicroDI is a simple and lightweight DI Container.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages