Skip to content

nreco/dependencyinjection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NReco.DependencyInjection

Extension to standard .NET DI-container that implements declarative IoC configuration (JSON) and components factory.

NuGet Tests
NuGet Release Tests
  • components (services) may be defined via JSON config (can be in appsettings.json or loaded from a separate file)
  • you can mix IoC JSON config with classic in-code services definitions
  • property injections
  • supports mix of explicit / implicit (by-type) injections
  • compatible with all modern .NET Core (3.1+) / NET6+ apps

How to use

services.LoadComponentsFromJsonFile("components.json");

To load services definitions from 'appsettings.json' section:

services.LoadComponentsFromJsonFile("appsettings.json", "Components");

Declarative component JSON definition

{
  "Type": "SomeNamespace.ComponentClass,ComponentAssembly",  // required. assembly name may be omitted
  "Name": "c1",  // optional. If set, registered as 'keyed' service
  "Lifetime" : "Singleton",  // Optional. Can be: Transient, Scoped, Singleton (Transient by default)
  "InjectDependencyAttr": false,  // Optional. enable by-type injections for properties marked with [Dependency] (false by default)
  "Constructor": [ /* constructor injections */  ],  // Optional 
  "Properties": {  // Optional. Explicit properties injections
     "PropService": { },  // means: resolve service by property type 
	 "PropStr": "strVal",  // inject simple value
	 "PropRefTo1": {"$ref": "c2"}, // inject keyed service "c2", use property type as service type 
	 "PropRefTo2": {
	   "$ref": "c2",  // inject keyed service "c2" (type="ServiceType")
 	   "ServiceType": "SomeNamespace.ServiceType"
	 },
     "PropArr": [ 1, 2, 3 ],  // inject array of values (or refs to another services)
	 "PropDict": {
	   "KeyStr": "KeyValue"   // inject dictionary (string->object)
	 },
	 "InlineService": {
	   "Type": "SomeNamespace.ComponentClass"  // not registered in 'services' but injections are supported
	 }
  }
}

License

Copyright 2023 Vitaliy Fedorchenko and contributors

Distributed under the MIT license

About

Standard DI-container extensions: properties injection, declarative services definitions (JSON) and explicit injections

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages