Giter VIP home page Giter VIP logo

atlex.dependencyinjection's Introduction

AtleX.DependencyInjection

AtleX.DependencyInjection is an extension to Microsoft's dependency injection in (ASP).NET Core to enable module-like registration of services. This keeps your service registration, and especially the Startup class in ASP.net Core, sane.

Installation

AtleX.DependencyInjection is available in two NuGet packages.

For ASP.net Core with AtleX.DependencyInjection.Modules.AspNetCore:

dotnet add project.csproj package AtleX.DependencyInjection.Modules.AspNetCore

Generic .NET Core with AtleX.DependencyInjection.Modules:

dotnet add project.csproj package AtleX.DependencyInjection.Modules

Usage

ASP.net Core

Instead of filling up the Startup class with lots of service registrations and configurations, the services are configured as autodiscovered modules.

Consider the following:

(Startup.cs)

public class Startup
{
  // This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddMvc()
      .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
      .AddJsonOptions(options =>
      {
        options.SerializerSettings.Converters.Add(new StringEnumConverter());
      });

    services.AddScoped<IDataProvider>(CreateDataProvider);
  }

  private IDataProvider CreateDataProvider(IServiceProvider serviceProvider)
  {
    var result = new DefaultDataProvider();

    return result;
  }
}

When registering multiple services, the Startup class quickly fills up with lots of service registrations, factory methods, configuration, etc. AtleX.DependencyInjection.Modules.AspNetCore aims to replace that with autodiscovered modules.

To register the data provider from the previous example, create a DataProviderAspModuleRegistrar that implements IAspModuleRegistrar:

public class DataProviderAspModuleRegistrar : IAspModuleRegistrar
{
  public void Configure(IApplicationBuilder application, IHostingEnvironment environment, IConfiguration configuration)
  {
      
  }

  public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
  {
    services.AddScoped<IDataProvider>(CreateDataProvider);
  }

  private IDataProvider CreateDataProvider(IServiceProvider serviceProvider)
  {
    var result = new DefaultDataProvider();

    return result;
  }
}

In Program.cs, add UseModuleRegistrars() to the IWebHostBuilder created in CreateWebHostBuilder(string[]). This enables the autodiscovery of the modules:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseModuleRegistrars()
    .UseStartup<Startup>();

You'll notice the familiar ConfigureServices() and Configure() methods when implementing IAspModuleRegistrar. These methods work the same as the ones in Startup and provide ways to register and configure services in the application.

By default the modules are discovered in the entry assembly of the application. By configuring the options of UseModuleRegistrars() you can add additional assemblies to scan:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseModuleRegistrars(options =>
    {
      options.AddAssembly(typeof(Service1).Assembly);
      options.AddAssembly(typeof(Service2).Assembly);
    })
    .UseStartup<Startup>();

Generic .NET Core dependency injection

To register modules in the generic (non-ASP) .NET Core dependency injection, create a module registrar:

public class DataProviderAspModuleRegistrar : IModuleRegistrar
{
  public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
  {
    services.AddScoped<IDataProvider>(CreateDataProvider);
  }

  private IDataProvider CreateDataProvider(IServiceProvider serviceProvider)
  {
    var result = new DefaultDataProvider();

    return result;
  }
}

When configuring the services, call RegisterModules(IConfiguration) on the IServiceCollection:

// Register modules from the entry assembly:
services.RegisterModules(this.Configuration);

// Also register modules from another assembly:
services.RegisterModules(this.Configuration, typeof(Service1).Assembly);

License

AtleX.DependencyInjection uses the MIT license, see the LICENSE file.

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.