Giter Site home page Giter Site logo

lucadecamillis / dependency-injection-configuration Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 40 KB

Provide JSON configuration for ServiceCollection

License: Apache License 2.0

C# 100.00%
appsettings appsettings-json json microsoft-configuration microsoft-dependency-injection service-collection asp-net asp-net-core web-api web-api-core

dependency-injection-configuration's Introduction

Dependency Injection Configuration Build status NuGet

Provide support for configuring ServiceCollection in ASP.NET WebAPI or via IConfiguration

Installation

Install the package via nuget:

dotnet add package DependencyInjection.Configuration

Web Api

Configuration is read using the extension FromConfiguration():

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.FromConfiguration(builder.Configuration);

IConfiguration

For console or WPF applications configuration can still be read using the extension FromConfiguration():

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
    .Build();

IServiceCollection services = new ServiceCollection();

services.FromConfiguration(configuration);

Definition

Services definition is read from appsettings.json:

{
  "Services": {
    "Collection": [
      {
        "ServiceType": "Samples.Lib.Interfaces.IRepository, Samples.Lib",
        "ImplementationType": "Samples.Lib.Impl.Repository, Samples.Lib",
        "Lifetime": "Scoped"
      },
      {
        "ServiceType": "Samples.Lib.Interfaces.IService, Samples.Lib",
        "ImplementationType": "Samples.Lib.Impl.Service, Samples.Lib"
      },
      {
        "ServiceType": "Samples.Lib.Impl.Context, Samples.Lib",
        "Lifetime": "Singleton"
      },
      {
        "ServiceType": "Samples.Lib.Interfaces.IGenericService`1, Samples.Lib",
        "ImplementationType": "Samples.Lib.Impl.GenericService`1, Samples.Lib"
      },
      {
        "ServiceType": "Samples.Lib.Interfaces.IComplexService, Samples.Lib",
        "Factory": {
          "Type": "Samples.Lib.Factories.ComplexServiceFactory, Samples.Lib",
          "Method": "Create"
        },
        "Lifetime": "Transient"
      }
    ]
  }
}

Per registration the following parameters can be set

  • ServiceType: Assembly Qualified Name of the type to register (Required)
  • ImplementationType: Assembly Qualified Name of the type that implements ServiceType (Optional)
  • Lifetime: lifetime of the registration (Optional). Values are taken from enum ServiceLifetime. If not provided it defaults to ServiceLifetime.Transient
  • Factory: create the service via factory method.
    • Type: Type where the factory method is defined
    • Method: Factory method (has to be defined as static)

The JSON location of services definition can be changed in options:

var options = new DependencyInjectionConfigurationOptions
{
    Path = "SomeOtherPath"
};

services.FromConfiguration(configuration, options);

For console or WebApi examples check out the samples directory.

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.