illud
New member
- Joined
- Jul 7, 2023
- Messages
- 3
- Programming Experience
- 5-10
Hello, im using entity framework, i create context in controller the past into the parameters to service and then repository to make sql queries,
is ok to do that?
or theres a better way?
Controller
Services
Repository
is ok to do that?
or theres a better way?
Controller
C#:
private readonly DataContextDb _db;
public UsersController(DataContextDb contextDb)
{
_db = contextDb;
}
[HttpGet(Name = "GetUsers")]
public async Task<List<UsersModel>> Get()
{
UsersService usersService = new();
return await usersService.Get(_db); //context(_db) in function parameter
}
Services
C#:
public async Task<List<UsersModel>> Get(DataContextDb _db)
{
UsersRepository usersRepository = new();
return await usersRepository.GetUsers(_db);//context(_db) in function parameter
}
Repository
C#:
public async Task<List<UsersModel>> GetUsers(DataContextDb _db)//context(_db) in function parameter
{
return await _db.Users.ToListAsync();
}