LiteLib.DI-Lite 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package LiteLib.DI-Lite --version 1.0.0
NuGet\Install-Package LiteLib.DI-Lite -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LiteLib.DI-Lite" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteLib.DI-Lite --version 1.0.0
#r "nuget: LiteLib.DI-Lite, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LiteLib.DI-Lite as a Cake Addin
#addin nuget:?package=LiteLib.DI-Lite&version=1.0.0

// Install LiteLib.DI-Lite as a Cake Tool
#tool nuget:?package=LiteLib.DI-Lite&version=1.0.0

DI-Lite

DI-Lite is a small size, high performance tool for storing and retrieving objects for dependency injection. Library is written in C# and has no external dependencies.

Table of contents

Features

  • Registering singletons
  • Registering factories
  • Registering scoped dependencies
  • Removing registered dependencies
  • Getting dependencies
  • Scoping dependencies
  • Auto creating objects base on their constructor

Examples

// creating a DI container
var Container = new Container();
// register singleton 
// object will be created once and then every Get call will return same object
Container.Single<DependencyType>(() => new DependencyImp());

// dependency can have a tag to distinguish dependencies of the same type
Container.Single<DependencyType>("tag", () => new DependencyImp());

// already instantiated objects can be used
Container.Single<DependencyType>(dependencyInstance);

// we can already use our container for creating new dependencies
Container.Single<DependencyType>("tag2", () => new DependencyImp(Container.Get<DependencyOfOurDependencyType>()));

// when no creation function is provided the object will be created using it's  constructor, but we have to provide its concrete class. Dependency class has to have exactly 1 constructor
Container.Single<DependencyType, DependencyImp>("tag3");

// if for some reason we don't want an abstraction then call can be simplified
Container.Single<DependencyImp>("tag4");
// register factory
// new object will be created with every Get call
Container.Factory<DependencyType>();
// most overloads working with Single works with Factory with minor exceptions
// register scoped dependency
// same object will be returned for a given scope
// different scopes will return different objects
Container.Scoped<DependencyType>();
// most overloads working with Single works with Scoped with minor exceptions
// register variants
// every register method has Try and Force variant
Container.Factory(() => "1");
Container.Factory(() => "2"); // this will throw an exception
/*******/
Container.Factory(() => "1");
Container.TryFactory(() => "2"); // this will not override the dependency
/*******/
Container.Factory(() => "1"));
Container.ForceFactory(() => "2"); // this will override the dependency
// every depedency can be removed from a Container at any given time
// remove all dependencies registed for a type
Container.Remove<DependencyType>();
// remove all dependencies registed with a tag
Container.Remove("tag");
// remove dependency registed for a type with a tag (type and tag create KEY)
Container.Remove<DependencyType>("tag");
// remove all dependencies that match predicate
Container.Remove(dep =>
	 dep.Type != typeof(DependencyType) ||
	 dep.Tag != null);
// remove dependency with a given KEY
var key = Container.Dependencies.ElementAt(0).Key;
Container.Remove(key);
// remove all dependencies by collection of KEYS
var keys = new DependencyKey[] {
	new DependencyKey(typeof(DependencyType), null),
	new DependencyKey(typeof(AnotherDependencyType), null),
};
Container.Remove(keys);
// getting dependency object from container
// regular containers are unable to Get scoped dependencies
var dependency = Container.Get<DependencyType>();
// we can also get dependencies registered with tag
var dependency = Container.Get<DependencyType>("tag");
// to Get scoped dependencies we first have to create a scope
var scope = Container.CreateScope();
// scope is able to Get all kinds of dependencies
// it is performed the same way as with Container
var dependency = scope.Get<DependencyType>();
// or
var dependency = scope.Get<DependencyType>("tag");

Todos

  • Write MORE Tests
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 404 8/9/2022
1.0.5 397 5/17/2022
1.0.4 382 5/15/2022
1.0.3 399 4/21/2022
1.0.2 266 12/5/2021
1.0.1 1,875 11/26/2021
1.0.0 1,449 11/26/2021