Blog

Filter posts by Category Or Tag of the Blog section!

Breaking the DI registrations of component in asp.net Core

Monday, 20 November 2017

Asp.net Core supports for dependency injection by default, you can easily register your services like the following code snippet in Startup class:


 

public class Startup

  {

      public Startup(IConfiguration configuration)

      {

          Configuration = configuration;

      }



      public IConfiguration Configuration { get; }



      public void ConfigureServices(IServiceCollection services)

      {   

           services.AddScoped<IMembershipData, MembershipData>();

       }

    }

 

Now if you want to register your services in a different class library rather than asp.net core project, in a layered architecture, as you know many DI containers support for modular resolving; we have the same capability in asp.net core:


 

public static class DIContainer

  {

      public static IServiceCollection AddDependencyInjectionServices(this IServiceCollection services)

      {

          services.AddScoped<ILogData, LogData>();

          services.AddScoped<IMembershipData, MembershipData>();

          services.AddScoped<INotificationData, NotificationData>();

          return services;

      }

  }

 

And you can easily register the class above in you Startup class:

 

public void ConfigureServices(IServiceCollection services)

      {

                services.AddDependencyInjectionServices();

       }

 

Category: Software

Tags: Asp.Net

comments powered by Disqus