Должна ли служба вызывать другую службу или хранилище напрямую? - PullRequest
0 голосов
/ 01 декабря 2018

Я создаю 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 голосов
/ 01 декабря 2018

Лучшей практикой является вызов StudentService из ClassServiceMethod.Если в будущем реализация в StudentRepository изменится, вы можете создать другой метод репозитория, например StudentRepositoryNew, и использовать тот же StudentService.

...