DEV Community

Cover image for Use of Static class in .Net core
shariaretanvir
shariaretanvir

Posted on

Use of Static class in .Net core

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;            
        }
}
Enter fullscreen mode Exit fullscreen mode

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;
            }
         }
}
Enter fullscreen mode Exit fullscreen mode

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

string connectionString =  DBConnection.Configuration.GetSection("LiveConnectionStrings:CapitalMarketDB").Value;
Enter fullscreen mode Exit fullscreen mode

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;
        }
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)