vendredi 30 novembre 2018

Should service call another service or repository directly?

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

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 :)

Thanks in advance.

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

Services - Business Layer

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

Repositories - Data Layer

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

Aucun commentaire:

Enregistrer un commentaire