服务应该直接调用另一个服务或存储库吗?

时间:2018-11-30 21:02:47

标签: oop model-view-controller design-patterns architecture

我正在创建具有许多层的WebApplication(目前重要的是Model,Repository,BusinessLayer)

具有ClassService,ClassRepository和StudentService,StudentRepository,ClassServiceMethod应该从StudentService或StudentRepository调用方法吗?

请提供尽可能多的参数或其他链接/博客/信息:)

谢谢。

这是我的示例代码,添加了一些泛型。问题是关于GetClassAndBestStudent方法:

服务-业务层

public class ClassService :  BaseService<Class>, IClassService
{
    IClassRepository classRepository; // Resolved by IoC, will be injected to BaseService
    IStudentRepository studentRepository;
    IStudentService studentService;

    public virtual Class GetClassWithHighestNotes() { ... } // Do some stuff and call classRepository.GetClassWithHighestNotes()
    public virtual Teacher GetTeachersByClass(int classId) { ... } // Do some stuff and call classRepository.GetTeachersByClass()

    public virtual GetClassAndBestStudent(int classId)
    {
        // Question here: Which call is valid?
        var best = studentRepository.GetStudentWithHighestNotes()
        var best = studentService.GetStudentWithHighestNotes();
    }
}

public class StudentService : BaseService<Student>, IStudentService
{
    IStudentRepository studentRepository; // Resolved by IoC, will be injected to BaseService

    public virtual IEnumerable<Student> GetStudentsByClass(int classId) { ... } // Do some stuff and call studentRepository.GetStudentsByClass()
    public virtual Student GetStudentWithHighestNotes() { ... } // Do some stuff and call studentRepository.GetStudentWithHighestNotes()
}

// Abstract, generic CRUD service 
public abstract class BaseService<T> : IBaseService<T> where T : MyBase
{
    IRepository<T> repository;

    public virtual IEnumerable<T> GetAll() { ... } // Do some stuff and call repository.GetAll()
    public virtual T GetById(int id) { ... } // Do some stuff and call repository.GetById()
    public virtual T Insert(T entity) { ... } // Do some stuff and call repository.Insert()
    public virtual T Update(T entity) { ... } // Do some stuff and call repository.Update()
    public virtual bool Delete(T entity) { ... } // Do some stuff and call repository.Delete()
    public virtual bool Delete(int id) { ... } // Do some stuff and call repository.Delete()
}

存储库-数据层

public class ClassRepository : BaseRepository<Class>, IClassRepository
{
    public virtual Class GetClassWithHighestNotes() { ... }
    public virtual Teacher GetTeachersByClass(int classId) { ... }
}

public class StudentRepository: BaseRepository<Student> IStudentRepository
{
    public virtual IEnumerable<Student> GetStudentsByClass(int classId) { ... }
    public virtual Student GetStudentWithHighestNotes() { ... }
}

// Abstract, generic CRUD repository
public abstract class BaseRepository<T> : IRepository<T> where T : MyBase
{
    public virtual IEnumerable<T> GetAll() { ... }
    public virtual T GetById(int id) { ... }
    public virtual T Insert(T entity) { ... }
    public virtual T Update(T entity) { ... }
    public virtual bool Delete(T entity) { ... }
    public virtual bool Delete(int id) { ... }
}

1 个答案:

答案 0 :(得分:0)

最佳做法是从ClassServiceMethod调用StudentService。如果将来StudentRepository中的实现发生变化,则可以创建其他存储库方法(例如StudentRepositoryNew)并使用相同的StudentService。