Use of Static class in .Net core
Tags: dotnet, csharp
Published: 2021-09-11 07:41:13

We all know about static class. When we use data which will not change according to caller then we can mark that as static.

Today I will show an example to use static class in .Net core. We can use static class for retrieving common information like appsettings or database connection information.

So we need to create a class named DBConnection.cs. We all know for accessing appsettings.json in .Net core we need to implement IConfiguration interface. IConfiguration can be accessed in controller using construction which is done by Dependency injection. Here is the example of this:

```
public class MyController : ControllerBase
    {
        public IConfiguration Configuration { get; }
        public IWebHostEnvironment Environment { get; }
        
        public MyController(IConfiguration configuration, IWebHostEnvironment environment, IUnitOfWork unitOfWork)
        {
            Configuration = configuration;
            Environment = environment;            
        }
}
```
But IConfiguration is not automatically accessed in any custom class other than controller, but we have to need that object to getting data from appsettings.json. 

So our static class will do that for us. Why we use static class for this? Because this data will not be changed or need to modified by caller methods. If you still get confused read my article about static keyword where I briefly describe about static and when to use static and when not to.

So here is my code for accessing IConfiguration.
```
public static class DBConnection
    {
        private static IConfiguration config;
        public static IConfiguration Configuration {
            get {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json");
                config = builder.Build();
                return config;
            }
         }
}
```


So now any class can access IConfiguration object by calling our DBConnection class like this

```
string connectionString =  DBConnection.Configuration.GetSection("LiveConnectionStrings:CapitalMarketDB").Value;
```

Now we can also get the common information like DBPath, Connection string from our DBConnection static class.



```
public static string DBPath {
            get {
                return Configuration.GetSection("DBPath:Environment").Value;
            }            
        }
        
        public static string GetCapitalMarketConnectionString()
        {            
            return DBPath == "UAT" ? Configuration.GetSection("UATConnectionStrings:CapitalMarketDB").Value
                : Configuration.GetSection("LiveConnectionStrings:CapitalMarketDB").Value;
        }
        public static string GetCommonDBConnectionString()
        {
            return DBPath == "UAT" ? Configuration.GetSection("UATConnectionStrings:CommonDB").Value
                : Configuration.GetSection("LiveConnectionStrings:CommonDB").Value;
        }
```

SO this is a real life example of static class which is implemented on .Net perspective. 

Feel free to comment and share your views about this article.