how to register Application Service with built-in IOC container and use it in our application.

Registering Application Service
  •  In order to let the IoC container automatically inject our application services, we first need to register them with an IOC container.
  • Consider the following example of a simple ILog interface and its implementation class. We will see how to register it with a built-in IoC container and use it in our application.

public interface ILog {

void info(string str);

}

class MyConsoleLogger : ILog {

public void info(string str)

{

Console.WriteLine(str);

}

}


  • ASP.NET Core allows us to register our application services with IoC container, in the ConfigureServices method of the Startup class. The ConfigureServices method includes a parameter of IServiceCollection type which is used to register application services.
  • Let's register above ILog with IoC container in ConfigureServices() method asshown below. 
  • Example: Register Service

public class Startup {

public void ConfigureServices(IServiceCollection services) {

services.Add(new ServiceDescriptor(typeof(ILog),

new MyConsoleLogger()));

} // other code removed for clarity..

}


  • In the above ex:
  •  Add() method of IServiceCollection instance is used to register a service with an IoC container.
  • ServiceDescriptor is used to specify a service type and its instance. We have specified ILog as service type and MyConsoleLogger as its instance. This will register the ILog service as a singleton by default.
  • Now, an IoC container will create a singleton object of MyConsoleLogger class and inject it in the constructor of classes wherever we include ILog as a constructor or method parameter throughout the application.
  • Thus, we can register our custom application services with an IoC container in the ASP.NET Core application. There are other extension methods available for quick and easy registration of services.


Understanding Service Lifetime for Registered Service

  • Built-in IoC container manages the lifetime of a registered service type. It automatically disposes of a service instance based on the specified lifetime.
  •  The built-in IoC container supports three kinds of lifetimes:

1. Singleton: IoC container will create and share a single instance of service throughout the application's lifetime.

2. Transient: The IoC container will create a new instance of the specified service type every time you ask for it.

3. Scoped: IoC container will create an instance of the specified service type once per request and will be shared in a single request.

The following example shows how to register a service with different lifetimes.

 Example: Register a Service with Lifetime

public void ConfigureServices(IServiceCollection services)

{

// singleton

services.Add(new ServiceDescriptor(typeof(ILog), new MyConsoleLogger()));

services.Add(new ServiceDescriptor(typeof(ILog), typeof(MyConsoleLogger),

ServiceLifetime.Transient)); // Transient

services.Add(new serviceDescriptor(typeof(ILog), typeof(MyConsoleLogger),

ServiceLifetime.Scoped)); // Scoped

}

Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Explain network topology .Explain tis types with its advantages and disadvantges.