nanoFramework.DependencyInjection 1.1.11

Prefix Reserved
dotnet add package nanoFramework.DependencyInjection --version 1.1.11                
NuGet\Install-Package nanoFramework.DependencyInjection -Version 1.1.11                
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="nanoFramework.DependencyInjection" Version="1.1.11" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.DependencyInjection --version 1.1.11                
#r "nuget: nanoFramework.DependencyInjection, 1.1.11"                
#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 nanoFramework.DependencyInjection as a Cake Addin
#addin nuget:?package=nanoFramework.DependencyInjection&version=1.1.11

// Install nanoFramework.DependencyInjection as a Cake Tool
#tool nuget:?package=nanoFramework.DependencyInjection&version=1.1.11                

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework Dependency Injection Library repository

Provides Dependency Injection (DI) for Inversion of Control (IoC) between classes and their dependencies built for .NET nanoFramework.

Build status

Component Build Status NuGet Package
nanoFramework.DependencyInjection Build Status NuGet

Samples

Dependency Injection Samples

Dependency Injection Unit Tests

Dependency Injection Container

A Dependency Injection (DI) Container provides functionality and automates many of the tasks involved in Object Composition, Interception, and Lifetime Management. It's an engine that resolves and manages object graphs. These DI Containers depend on the static information compiled into all classes. Then using reflection they can analyze the requested class and figure out which Dependencies are required.

This API mirrors as close as possible the official .NET DependencyInjection. Exceptions are mainly derived from the lack of generics support in .NET nanoFramework.

The .NET nanoFramework Generic Host provides convenience methods for creating dependency injection (DI) application containers with preconfigured defaults.

Usage

Service Collection

Creating a dependency injection container required three basic components.

  • Object Composition - A object composition defining a set of objects to create and couple.
  • Registering Services - Define an instance of the ServiceCollection and register the object composition with a specific service lifetime.
  • Service Provider - Creating a service provider to retrieve the object.

Object Composition

Define an object composition to create and couple.

Note: You can not currently define your objects as 'struct' or include array type parameters (ie. byte[] bytes) as constructor parameters. Doing so will create a null execption when trying to activate object.

public class RootObject
{
    public int One { get; }
    
    public string Two { get; }

    public ServiceObject ServiceObject { get; protected set; }

    public RootObject(ServiceObject serviceObject)
    {
        ServiceObject = serviceObject;
    }

    // constructor with the most parameters will be used for activation
    public RootObject(ServiceObject serviceObject, int one, string two)
    {
        ServiceObject = serviceObject;
        One = one;
        Two = two;
    }
}

public class ServiceObject
{
    public string Three { get; set; }
}

Registering Services

Create a Service Collection and register singleton or transient type services to the collection.

var serviceProvider = new ServiceCollection()
    .AddSingleton(typeof(ServiceObject))
    .AddSingleton(typeof(RootObject))
    .BuildServiceProvider();

Service Provider

Create a Service Provider to access or update an object.

var service = (RootObject)serviceProvider.GetService(typeof(RootObject));
service.ServiceObject.Three = "3";

Create a scoped Service Provider providing convient access to crate and distroy scoped object.

var serviceProvider = new ServiceCollection()
    .AddScoped(typeof(typeof(ServiceObject))
    .BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
    var service = scope.ServiceProvider.GetServices(typeof(ServiceObject));
    service.ServiceObject.Three = "3";
}

Activator Utilities

An instance of an object can be created by calling its constructor with any dependencies resolved through the service provider. Automatically instantiate a type with constructor arguments provided from an IServiceProvider without having to register the type with the DI Container.

var instance = (RootObject)ActivatorUtilities.CreateInstance(
                        serviceProvider, typeof(RootObject), 1, "2"
                    );

Debug.WriteLine($"One: {instance.One}");
Debug.WriteLine($"Two: {instance.Two}");
Debug.WriteLine($"Three: {instance.ServiceObject.Three}");
Debug.WriteLine($"Name: {instance.ServiceObject.GetType().Name}");

Validate On Build

A check is performed to ensure that all services registered with the container can actually be created. This can be particularly useful during development to fail fast and allow developers to fix the issue. Validate on build is configured false by default.

var serviceProvider = new ServiceCollection()
    .AddSingleton(typeof(IServiceObject), typeof(ServiceObject))
    .BuildServiceProvider(new ServiceProviderOptions() { ValidateOnBuild = true });

Validate Scopes

A check verifying that scoped services never gets resolved from root provider. Validate on build is configured false by default.

var serviceProvider = new ServiceCollection()
    .AddSingleton(typeof(IServiceObject), typeof(ServiceObject))
    .BuildServiceProvider(new ServiceProviderOptions() { ValidateScopes = true });

Example Application Container

using System;
using System.Device.Gpio;
using System.Threading;

using nanoFramework.Logging.Debug;
using nanoFramework.DependencyInjection;

using Microsoft.Extensions.Logging;

nanoFramework.DiApplication
{
    public class Program
    {
        public static void Main()
        {
            var services = ConfigureServices();
            var application = (Application)services.GetRequiredService(typeof(Application));

            application.Run();
        }

        private static ServiceProvider ConfigureServices()
        {
            return new ServiceCollection()
                .AddSingleton(typeof(Application))
                .AddSingleton(typeof(IHardwareService), typeof(HardwareService))
                .AddSingleton(typeof(ILoggerFactory), typeof(DebugLoggerFactory))
                .BuildServiceProvider();
        }
    }

    internal class Application
    {
        private readonly ILogger _logger;
        private readonly IHardwareService _hardware;
        private readonly IServiceProvider _provider;

        public Application(
            IServiceProvider provider,
            IHardwareService hardware, 
            ILoggerFactory loggerFactory)
        {
            _provider = provider;
            _hardware = hardware;
            _logger = loggerFactory.CreateLogger(nameof(Application));

            _logger.LogInformation("Initializing application...");
        }

        public void Run()
        {
            var ledPin = 23; // Set pin number to blink 15=ESP32; 23=STM32

            _logger.LogInformation($"Started blinking led on pin {ledPin}.");
            _hardware.StartBlinking(ledPin);
        }
    }

    internal interface IHardwareService
    {
        public void StartBlinking(int ledPin) { }
    }

    internal class HardwareService : IHardwareService, IDisposable
    {
        private Thread _thread;
        private readonly ILogger _logger;
        private readonly GpioController _gpioController;

        public HardwareService()
        {
            _gpioController = new GpioController();

            var loggerFactory = new DebugLoggerFactory();
            _logger = loggerFactory.CreateLogger(nameof(HardwareService));
        }

        public HardwareService(ILoggerFactory loggerFactory)
        {
            _gpioController = new GpioController();
            _logger = loggerFactory.CreateLogger(nameof(HardwareService));
        }

        public void StartBlinking(int ledPin)
        {
            GpioPin led = _gpioController.OpenPin(ledPin, PinMode.Output);
            led.Write(PinValue.Low);

            _thread = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(2000);

                    led.Write(PinValue.High);
                    _logger.LogInformation("Led status: on");

                    Thread.Sleep(2000);

                    led.Write(PinValue.Low);
                    _logger.LogInformation("Led status: off");
                }
            });

            _thread.Start();
        }

        public void Dispose()
        {
            _gpioController.Dispose();
        }
    }
}

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (30)

Showing the top 5 NuGet packages that depend on nanoFramework.DependencyInjection:

Package Downloads
MakoIoT.Device.Services.Interface

Interfaces for MAKO-IoT services (.NET nanoFramework C# projects).

MakoIoT.Device.Services.ConfigurationManager

Configuration mode manager for MAKO-IoT

MakoIoT.Device.Services.Server

Simple WebServer for .NET nanoFramework C# projects. Based on nanoFramework.WebServer by Laurent Ellerbach and contributors

MakoIoT.Device.Services.WiFi.AP

WiFi Access Point library for MAKO-IoT

MakoIoT.Device.Services.ConfigurationApi

REST API for configuration with MAKO-IoT

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on nanoFramework.DependencyInjection:

Repository Stars
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
Version Downloads Last updated
1.1.11 2,616 11/25/2024
1.1.3 42,996 11/9/2023
1.1.1 1,844 11/6/2023
1.0.35 5,291 10/4/2023
1.0.22 9,624 12/23/2022
1.0.12 971 10/12/2022
1.0.5 894 8/4/2022
1.0.2 745 7/14/2022
1.0.0.11 471 7/5/2022