Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Nano.DependencyInjector is simple dependency injection management for .NET Core

Notifications You must be signed in to change notification settings

peacecwz-archive/nano-dependency-injector

Repository files navigation

Nano Dependency Injector

Easy to apply dependency injection into .NET Core Projects with Attributes like Spring Boot Applications

Installation

 dotnet add package Nano.DependencyInjector

Usage

Define injection lifetime attribute to your injection class (Singleton, Scoped or Transient)

Singleton Example

    public interface ISingletonTestService
    {
        string Test();
    }

    [Singleton] // Add attribute to service class
    public class SingletonTestService : ISingletonTestService
    {
        public SingletonTestService()
        {
            Console.WriteLine("Created singleton lifetime instance");
        }

        public string Test()
        {
            return "Singleton";
        }
    }

Scoped Example

    public interface IScopedTestService
    {
        string Test();
    }

    [Scoped] // Add attribute to service class
    public class ScopedTestService : IScopedTestService
    {
        public ScopedTestService()
        {
            Console.WriteLine("Created scoped lifetime instance");
        }

        public string Test()
        {
            return "Scoped";
        }
    }

Transient Example

    public interface ITransientTestService
    {
        string Test();
    }

    [Transient] // Add attribute to service class
    public class TransientTestService : ITransientTestService
    {
        public TransientTestService()
        {
            Console.WriteLine("Created transient lifetime instance");
        }

        public string Test()
        {
            return "Transient";
        }
    }

Register Dependencies to IoC

Add code to ConfigureServices on Startup.cs file and it will register all dependencies

    services.RegisterDependencies(Assembly.GetExecutingAssembly());

TODO List

  • Setup CI for publishing package on nuget
  • Add unit tests
  • Register named typed dependency
  • Inject name typed dependency
  • Add property injection without constructor
  • Register injectable multiple interfaces
  • Generic interfaces

Contribution

Feel the free for contribution. Open issues, PR and contact to me

License

This project is licensed under the MIT License

About

Nano.DependencyInjector is simple dependency injection management for .NET Core

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages