Reputation: 107
I currently am building a rest API with asp net core. I was following this tutorial from Medium, which is a tutorial about Repository pattern implementations. Here is the Link.
I have 2 superclasses, each one has a subclass. I have Superclass TController and TService and the subclasses UserController and UserService. I want to call from UserController a method of UserService. The method is defined in UserService but not in the superclass of TService.
So my question is: How can you execute a method set in UserController, when you are calling it from UserController?
Here is the controller:
public abstract class TController<TEntity, TService> : ControllerBase
where TEntity : class, IEntity
where TService : IService<TEntity>
{
private TService service;
public TController(TService service)
{
this.service = service;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<TEntity>>> Get()
{
return await this.service.GetAll();
}
[Methods GetId, Add, Update, Delete, but cut out to keep the code short]
}
public class UserController : TController<UserModel, UserService>
{
public UserController(UserService service): base(service)
{
}
[HttpGet("/check")]
public bool Check()
{
return base.CheckPassword("Bollox")
}
}
And here is the service:
public interface IService<T> where T: class, IEntity
{
Task<List<T>> GetAll();
[Methods GetId, Add, Update, Delete, but cut out to keep the code short]
}
public abstract class TService<TEntity, TRepository>: IService<TEntity>
where TEntity: class, IEntity
where TRepository: IRepository<TEntity>
{
private TRepository repository;
public TService(TRepository repository)
{
this.repository = repository;
}
public async Task<List<TEntity>> GetAll()
{
return await this.repository.GetAll();
}
}
public class UserService : TService<UserModel, UserRepository>
{
public UserService(UserRepository repository) : base(repository)
{
}
public bool CheckPassword(String password)
{
return true;
}
}
I have tried to define UserService twice, which works, but is there any better way?
public class UserController : TController<UserModel, UserService>
{
private readonly UserService svc;
public UserController(UserService service): base(service)
{
this.svc = service;
}
[HttpGet("/check")]
public bool Check()
{
return svc.CheckPassword(new UserModel
{
});
}
}
Upvotes: 0
Views: 191
Reputation: 136094
You have the option of just making service
protected in the base class - as you're using generic you still have access to all of UserService
public abstract class TController<TEntity, TService> : ControllerBase
where TEntity : class, IEntity
where TService : IService<TEntity>
{
protected readonly TService service;
public TController(TService service)
{
this.service = service;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<TEntity>>> Get()
{
return await this.service.GetAll();
}
[Methods GetId, Add, Update, Delete, but cut out to keep the code short]
}
You can then use it directly in the derived class:
public class UserController : TController<UserModel, UserService>
{
public UserController(UserService service): base(service)
{
}
[HttpGet("/check")]
public bool Check()
{
return service.CheckPassword(new UserModel
{
});
}
}
Upvotes: 2