How to Initialize the Instances or Services within ConfigServices in Startup

Today in this article we will see how to Initialize the Instances or Services within ConfigServices in the Startup file.

This could be a legitimate need for you to want to initialize the services and instances at the Startup in .NET Core-based application.

Today in this article we will see how to Initialize the services/ instances within ConfigServices in ASP.NET Core.

This approach can also be used in non-host applications as well if you are following dependency injection design in applications like Console or DI in Windows Form/WPF app as well

Today in this article, we will cover below aspects,

We shall be using the below pattern to resolve the dependency and initialize the instances.

Initialize Instances while registering IoC Container

As shown in the below pattern, you get the flexibility to initialize an instance while you declare it.

         
services.AddSingleton<IYourInterface, YourInterface>(op => {
               //Initialize the instance here
                return obj; //Return the instance
            });

In the above example, we are doing below steps,

  • Registering the IYourInterface in the IOC DI container
  • Same time YourInterface class object will be initialized with its properties filled as required

Example

Let’s look into a real example of the above-discussed pattern

We already looked at the IOption pattern which lets you inject a Key-Value pair using DI.

Let’s initialize the ICustomConfig Interface with the configuration value available in the configuration file i.e apsettings.json.

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddSingleton<ICustomerConfig, CustomerConfig>(op => {
                CustomerConfig obj = new CustomerConfig();
                obj.Agency = new Agency();
                obj.CustomerKeyurl = Configuration.GetValue<string>("Customer:CustomerKeyurl");
                obj.CustomerdetailsUrl = Configuration.GetValue<string>("Customer:CustomerKeyurl");
                obj.Agency.AccountKey = Configuration.GetValue<int>("Customer:Agency:AccountKey");
                obj.Agency.AgencyID = Configuration.GetValue<string>("Customer:Agency:AgencyID");

                return obj;
            });
        }

Once executed interface ICustomerConfig gets resolved and initialized using Constructor injection in Controller or any other class wherever module you need that instance.

With the above techniques ICustomerConfig will be available as DI anywhere in the code including the class libraries.

Initialize Instances using registered services in IoC Container

In the below example, we are registering the instance and the same time initializing it.

Now let’s use the instances which is already registered in the IoC Container.

Example:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddScoped<IUniqueIdService, UniqueIdService>();
            services.AddScoped<ICustomerConfig, CustomerConfig>(op =>
            {
                CustomerConfig obj = new CustomerConfig();
                var myCustomService = op.GetService<IUniqueIdService>();
                obj.Guid = myCustomService.GetDetails();
                return obj;
            });
        }


  • With the above, we are registering and initializing the ICustomerConfig 
  • We are registering the ICustomerConfig using registered IUniqueIdService.
  • We are calling IUniqueIdService getting the required data and assigning it to ICustomerConfig 

Similarly, ICustomerConfig will be available as DI anywhere in the code including the class libraries.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *