My controller action to update user status in mvc:
public class UserController : AdminController
{
public async Task UpdateUserStatus(int id, int status)
{
await UpdateTheStatus<UserService, User>(id, status);
}
}
I have a base controller to update the user status
public abstract class AdminController : ControllerBase
{
public async Task UpdateTheStatus<TService, T>(int id, int status)
where TService : StatusService<T>, new()
{
await new TService().UpdateStatus(id, status);
}
}
My UserService.cs
have constructor
public class UserService : StatusService<User>
{
public UserService(MyContext context) : base(context)
{
}
....
}
Base class StatusService.cs
public abstract class StatusService<T>
{
protected MyContext ctx = null;
public StatusService(MyContext context)
{
ctx = context;
}
public async virtual Task UpdateStatus(int id, int status)
{
...
await ctx.SaveChangesAsync();
}
}
Then my code have the error:
'UserService' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TService' in the generic type or method
How do I pass the constructor with parameter as generic type?