Coder Social home page Coder Social logo

teacher-zhou / autoinject Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 22 KB

The extension of Microsoft.Extensions.DependencyInjection to inject all services automatically for implementations.

License: MIT License

C# 100.00%
autoservice dependency-injection automatically aspnetcore

autoinject's Introduction

AutoInject

The extension of Microsoft.Extensions.DependencyInjection to inject all services automatically for implementations.

Where to get?

The package deploy on Nuget and the code can view on Github. You can install from Package Management Console with command as below:

Install-Package Micpro.AutoInject

Release Note for v1.0.0

  • [New]AddAutoInject extensions method of IServiceCollection object.
  • [New]Resolve the service automatically while the instance implement from ITransient,IScoped or ISingleton.

Why to use?

In DI of .NET Core, user always inject the services in ConfigureServices manually, but sometimes the would get the message Unable to resolve xxxxx from service while running because forgot to inject to the container.

And if should have a lot of codes to add the service to the container like this:

services.AddTransient<xxx>();
services.AddTransient<xxx>();
services.AddScoped<xxx>();
services.AddSingletone<xxx>();
services.AddScoped<xxx>();
services.AddTransient<xxx>();
services.AddScoped<xxx>();
services.AddSingletone<xxx>();
services.AddScoped<xxx>();
services.AddTransient<xxx>();
services.AddSingletone<xxx>();
//.....

once you forget some service to inject, you will get the exception thrown Unable to resolve xxxxxx from service.

So I consider about the injection could be automatically.

How to use?

Just implement ITransient,IScoped or ISingleton interface for your injection service, such as your class or interface that the service type you wanna be.

class MyService : ITransient { } //the service of class

or

interface IMyService : IScoped { } // the service of interface

class MyService : IMyService { }

The lifetime should be the similar name of ITransient for transient, IScoped for scoped and ISingleton for singleton.

and they have the same called in ConfigureServices like:

class MyService : ITransient
//same as
services.AddTransient<MyService>();

or

interface IMyService : IScoped { }

class MyService : IMyService { }

//same as
services.AddScoped<IMyService, MyService>();

Just call AddAutoInject method in ConfigureServices in Startup class.

//scan all assembly
services.AddAutoInject();

//scan specify assemblies
services.AddAutoInject(new []{ Assembly.Load("XXXX.AAA"), Assembly.Load("XXXX.BBB") })

//scan specify search pattern of assemblies
services.AddAutoInject("XXX.YY.*");

//scan specify types
services.AddAutoInject(new []{ Type.LoadType("AAA.BB"), Type.LoadType("CC.DD") });

and you can get your service from constructor or injection container.

public MyController(IMyService service)//get the injected service
{
    
}

Support Generic Type

You will inject generic type manully like:

service.AddScoped(typeof(IRepository<>), typeof(Repository<>));

AutoInject also support this kind of expression without write any codes :

public interface IRepository<T> : IScoped

public interface IRepository<TSource, TResult> : ITransient

it will resolve automatically and inject the correct generic arguments for generic type

public MyController(IRepository<Entity> repository, IRepository<int, List<int>> repository2) //still get the generic type service
{
    
}

Any Issues, please report here or email me [email protected]

autoinject's People

Contributors

teacher-zhou avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.