A controller method calls this ValidateUser method. For example to validate user. The service then calls the dbcontext. For example to make the db query.
What scope (transient/scoped/singleton) is suitable for the service class?
As I understand, suppose if scope is set to singleton, then there will be only 1 instance of this class throughout the lifetime of the application. And even though the DbContext by default is scoped, only 1 connection will exist (from this service class's instance) throughout the lifetime.
Suppose I set to transient, then each request for the service class will create a new instance of the class. Since dbcontext is scoped, it will be a 1 instance for 1 user's request lifetime.
Suppose I set to scoped, then each user's request for the service class will create new instance of the class. Since dbcontext is scoped, it will be a 1 instance for 1 user's request lifetime
What are the thread safety concerns?
namespace MyApp.Services
{
public class ValidateService : IValidateService
{
private readonly MyAppContext _context;
public ValidateService(MyAppContext context)
{
_context = context;
}
public bool ValidateUser(UserDTO user)
{
return await _context.Users.AnyAsync(x => x.username == user.UserName);
}
}
}