I have the following datarepository EmployeeManager class
EmployeeManager.cs namespace MyProject.GO.WebAPI.Services
{
public class EmployeeManager : IDataRepository<EmployeeModel, long>
{
GoContext ctx;
public EmployeeManager(GoContext c)
{
ctx = c;
}
public IEnumerable<EmployeeModel> GetAll()
{
var employees = ctx.goEmployee.ToList();
return employees;
}
}
}
I want to call the method GetAll() in my reportcontroller class to get all the employee name in a datatable in report controller
using MyProject.GO.WebAPI.Services; namespace MyProject.GO.UI.Controllers
{
public class ReportController : Controller
{
public IActionResult LeaversOverviewCriteria()
{ // I want to call GetAll() method here to get all the employees into a datatable , Please help
return View();
}
}
}
The standard DI constructor pattern is shown below. The Interface and startup.cs is not shown so there is no way to verify.
using MyProject.GO.WebAPI.Services;
namespace MyProject.GO.UI.Controllers
{
public class ReportController : Controller
{
private readonly IDataRepository<EmployeeModel, long> _repo;
public ReportController(IDataRepository<EmployeeModel, long> repo)
{
_repo = repo;
}
public IActionResult LeaversOverviewCriteria()
{
var employees = repo.GetAll();
return View();
}
}
}
Be aware, Entity Framework is Unit of Work/Repository. Your approach is actually an anti-pattern. If you follow this path you'll end up creating a lot of extra code and have all kinds of problems.
Member
411 Points
1327 Posts
How can I call the method in Datarepository from the controller
Oct 24, 2019 07:47 AM|polachan|LINK
Hi
I have the following datarepository EmployeeManager class
I want to call the method GetAll() in my reportcontroller class to get all the employee name in a datatable in report controller
With Thanks
Pol
All-Star
53081 Points
23655 Posts
Re: How can I call the method in Datarepository from the controller
Oct 24, 2019 11:59 AM|mgebhard|LINK
The standard DI constructor pattern is shown below. The Interface and startup.cs is not shown so there is no way to verify.
Be aware, Entity Framework is Unit of Work/Repository. Your approach is actually an anti-pattern. If you follow this path you'll end up creating a lot of extra code and have all kinds of problems.