Seems like repository pattern is responsible from CRUD operations and data access methods (ex: stored procedure), and service layer is utilizing repository's methods to carry on its job.
My question is, would I be able to put methods in service layer that does not use its repository's methods?
For example, if my repository has these methods,
public interface IRepository<T>
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
and in IStudentService class
public interface IStudentService
{
void AddNewStudent(Student student);
void UpdateStudent(Student student);
void DeleteStudent(Student student);
ExcelImportVM GetStudentExcelExport(List<Students> list);
List<SelectListItem> GetDateRange();
}
and StudentService class implementation:
public class StudentService : IStudentService
{
private IStudentRepository _repository;
public ShopLevelDashService(IStudentRepository repository)
{
_repository= repository;
}
public void AddNewStudent(Student student)
{
return _repository.Add(student);
}
// ..UpdateStudent & DeleteStudent methods
public List<SelectListItem> GetDateRange()
{
var dateRange = new List<ColumnValuesVM>()
{
new ColumnValuesVM { Id = 1, Value = "test" },
new ColumnValuesVM { Id = 2, Value = "test2" }
};
var selectList = new List<SelectListItem>();
// ..foreach
return selectList;
}
ExcelImportVM GetStudentExcelExport(List<Students> list)
{
// ..codes
return viewModel;
}
}
Does this make sense to put methods like StudentExcelExport()
and GetDateRange()
in service class which does not use methods in its repository? (possibly for example: _repository.GetDateRange()
)
Or is it better to put them in the controller?
As @Chetan pointed out, service layer is your data access layer(DAL). So, it would not the best practices to use StudentExcelExport()
and GetDateRange()
in your services. Service layer should have only methods to deal with database operation.
As your both method just preparing view component, that should be at controller level. For other complex logic you can use business logic layer instead mix with DAL.
Or is it better to put them in the controller?
Answer is put them in a controller.