当您从另一个 sup class 的 sub class 调用方法时,如何从 super class 的 sub class 执行方法?

How can you execute a method from a sub class of a super class, when you are calling it from a sub class from another sup class?

我目前正在用 asp net core 构建一个 rest API。我正在关注 Medium 中的这个教程,这是一个关于 Repository 模式实现的教程。这是 Link.

我有2个超类,每个超类都有一个子类。我有超类 TController 和 TService 以及子类 UserController 和 UserService。我想从 UserController 调用 UserService 的方法。该方法在 UserService 中定义,但不在 TService 的超类中。

所以我的问题是:当您从 UserController 调用它时,如何执行在 UserController 中设置的方法?

这里是控制器:

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")
    }

}

这是服务:

 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;

    }

}

我尝试定义了两次 UserService,都可以,但是有没有更好的方法?

 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
        {
        });
    }

}

您可以选择只在基础 class 中使 service 受到保护 - 因为您使用的是通用的,所以您仍然可以访问所有 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]

}

你可以直接在导出的class:

中使用它
public class UserController : TController<UserModel, UserService>
{
    public UserController(UserService service): base(service)
    {
    }

    [HttpGet("/check")]
    public bool Check()
    {


        return service.CheckPassword(new UserModel
        {
        });
    }

}