Skip to content

Latest commit

 

History

History
102 lines (70 loc) · 3.42 KB

README.md

File metadata and controls

102 lines (70 loc) · 3.42 KB

DependencyInjection.InConsole

Build status

DependencyInjection.InConsole allows you to use dependency injection in .net core app.

Supported platforms

  • .Net core 2.0+

Installation

Nuget package

Basic use

// extends a class from Injector, and override it's Inject method to do inject
public class ExampleInjector : Injector
{
    public override void Inject()
    {
        services.AddTransient<Interface, Implication>();
    }
}

For .net core 2.0 or 2.1, add the follow codes at the first line in Main method:

var provider = Startup.ConfigureServices();
// or, if you don't need a provider
Startup.ConfigureServices();

But for .net core 2.2, you can choose using Hooks, so there's nothing need to be added into Main method, but if you need to use provider, you can get one by this:

var provider = Singletons.Provider;

You need to add an environment variable, name as DOTNET_STARTUP_HOOKS, and key as the absolute path of DependencyInjection.InConsole.dll, for example C:\DependencyInjection.InConsole.dll.

That's it, now you can use the type that you just injected.

And this injected IConfiguration from appsettings.json by default, so you can use it without extra injection:

var configuration = provider.GetRequiredService<IConfiguration>();
Console.WriteLine(configuration["foo"]);

You can check the example code in

example

Since there ISN'T a provider if you use HOOKS to inject, the only choice you have is to use Property Injection, learn more from Advance Features.

Want something more

If you want something more, please check it out from Advance Features.

Other DI frameworks

Autofac is supported since 1.2.3-preview🎉🎉🎉.

You can use Autofac by the following steps.

  1. Add Autofac.Extensions.DependencyInjection package into you project.

  2. Extend a class from AutofacInjector

    public class ExampleInjector : AutofacInjector
    {
        public override IServiceProvider Inject()
        {
            // Create the container builder.
            var builder = new ContainerBuilder();
            // register your service
            // ...
            var container = builder.Build();
        }
    }

    The codes that you need to into the Main method, just as same as you don't use Autofac.

    There are somethings you need to remember.

    • It's different from extending class from Injector, you can only provide one class that extends from AutofacInjector.
    • I strongly recommend that DO NOT use builder.Populate(services); in the Autofac register, actually you can't do that, because I don't provide one services for you to use😋.

TODO

  • Other DI frameworks support
    • Autofac
    • ...
  • Property injection
  • Performance optimization

Cooperation

Please feel free to open an issue or PR, if you have any problem or new thoughts.