0
votes

I am new to ASP.net core, and trying to convert my existing RESTful WepAPIs to ASP.Net Core API (2.2).

Following is my present application structure: [WebAPI Methods] calls -> [Business Logic Layer (BLL)] calls -> [Data Access Layer (DAL)].

In order to open DB connection from DAL, I am calling static method of another class library (Infrstructure.Data.dll) and passing connection string name as Enum. This library gets the actual connection string from the Web.Config of the webAPI using the following

string connString = ConfigurationManager.ConnectionStrings[GetDatabaseHint(dbSource)].ConnectionString;

and returns IDbConnection which my DAL uses it to transact with DB.

Now, I am looking for similar options in ASP.net core Web API.

I have so far come up with the following:

  1. In the Startup.cs file of API, added services.AddSingleton(Configuration);

  2. In the Infrstructure.Data.dll class library, added

    private static IConfiguration _config;
    public ConnectionFactory(IConfiguration config)
    {
        _config = config;
    }
    
    public static IDbConnection GetConnection(DBSource dbSource)
    {
        string connString = _config.GetConnectionString(GetDatabaseHint(dbSource));
    
    
        //... Code to create connection using DB Providers
    
        return conn;
    }
    

IConfiguration is always null here in this Infrstructure.Data.dll.

Please let me know if in case this is an incorrect approach, and what should be the right way of achieving this.

1
Post your exact code for DI registration in the startup.cs file. Also, how or who creates the object for ConnectionFactory? - Thangadurai

1 Answers

0
votes

Follow steps below to access IConfiguration in Infrstructure.Data

  1. Define ConnectionFactory

    public class ConnectionFactory
    {
        private static IConfiguration _config;
        public ConnectionFactory(IConfiguration config)
        {
            _config = config;
        }
        public string GetConnection()
        {
            return _config.GetConnectionString("any connection string name");
        }
        //public static IDbConnection GetConnection(DBSource dbSource)
        //{
        //    return new dbc
        //}
    }
    
  2. Register ConnectionFactory in Startup.cs

    services.AddScoped<ConnectionFactory>();
    
  3. Useage

    public class HomeController : Controller
    {
        private ApplicationDbContext _context;
        private readonly ConnectionFactory connectionFactory;
        public HomeController(ApplicationDbContext context
            , ConnectionFactory connectionFactory)
        {
            _context = context;
            this.connectionFactory = connectionFactory;
        }
        public IActionResult Index()
        {
            string result = connectionFactory.GetConnection();            
            return View();
        }
    }
    

    Note IConfiguration is registed default by WebHost.CreateDefaultBuilder(args)

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }