Normally I would inject an IConfiguration Object into the constructor of my controller class which I would use to initialize a local variable of type IConfiguration. Then I would use this local variable to call the GetConnectionConnectionString method which would return a connection string from the appsetting.json file. Finally I would use the resulting connection string to create a SQLConnection object and get whatever I need from the database.
Instead of that, now I want to place all code related to CRUD operations in the Data Access Layer but ran into some issues when I try to instantiate the DAL class in the API controllers. The issue is the DAL class constructor takes an IConfiguration object as an argument but when I try to instantiate the DAL class in the controller, I don’t know what to put in it as a parameter.
“the DAL class”
There’s not, to my knowledge, a standard DAL class. So… which class are you loading from where?
Code would be helpful at this point.
The Problem is, you are manually instantiating your DAL class inside the controller & struggling to provide the required IConfiguration dependency.
The Solution, do not instantiate the DAL manually, use Dependency Injection instead. Since you already have IConfiguration available through DI in your app (like in Startup.cs or Program.cs), you can register your DAL class there too.
you can do,
- Register DAL in DI container:
services.AddScoped<IMyDalClass, MyDalClass>();
- Update DAL constructor to accept IConfiguration:
public class MyDalClass : IMyDalClass
{
private readonly IConfiguration _configuration;
public MyDalClass(IConfiguration configuration)
{
_configuration = configuration;
}
// CRUD methods using _configuration.GetConnectionString(...)
}
- Inject DAL into controller:
public class MyController : ControllerBase
{
private readonly IMyDalClass _dal;
public MyController(IMyDalClass dal)
{
_dal = dal;
}
// Use _dal.YourMethod() wherever needed
}
This way, you let the framework manage the injection of dependencies & you will not need to worry about what to pass manually.
Good Luck..
The DAL class is a class that I created, it looks some thing like this
`
public class DAL
{
public IConfiguration _config ;
public DAL(IConfiguration config)
{
this._config = config
}
//Below will be ADO.Net code in my Web API project
}
`
Then in my API controller I would instantiate the DAL class as follows:
`
using MyProject.DAL
namespace MyProject.Controller
{
[Route("api/[controller]")]
[ApiController]
public class MyController: ControllerBase
{
[HttpGet]
public List<Product> getProducts(string ID )
{
var dal = new DAL(a value of type IConfiguration needs to go here);
return dal.getProds();
}
}
}
`
Thanks for replying. I assume the IMyDalClass interface will contain signatures of all the methods I will need to perform the CRUD operations. Is there anything else that I might need?
-
make sure interface not only includes the method names but also clearly reflects expected inputs/outputs.
-
rather than manually instantiating the DAL in your controller consider registering IMyDalClass & its implementation in the Startup.cs
services.AddScoped<IMyDalClass, MyDalClass>();
controller,
public MyController(IMyDalClass myDal)
{
_myDal = myDal;
}
- If you are accessing a database, your interface should likely expose asynchronous versions of your methods (Task return types).
You can inject IConfiguration into your controller and pass it to your DAL instance, or better—register the DAL in Startup.cs and let dependency injection handle it cleanly.