本文介绍了服务应该直接调用另一个服务或存储库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating the WebApplication, with many layers (for now important are Model, Repository, BusinessLayer)

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

Having ClassService, ClassRepository and StudentService, StudentRepository, should ClassServiceMethod call methods from StudentService or StudentRepository?

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

Please provide as many arguments or additional links/blogs/informations as possible :)

谢谢.

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

Here is my example code, some generics are added. The question is about GetClassAndBestStudent method:

服务-业务层

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

推荐答案

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

The best practice is calling StudentService from ClassServiceMethod. If the implementation in StudentRepository changes in the future, you can create another repository method like StudentRepositoryNew and utilize the same StudentService.

这篇关于服务应该直接调用另一个服务或存储库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:44