1

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?

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • 1
    The service should return only business data. Converting business data to viewmodels and other viewable types for UI should be done at the controller level. So method of this service should return data only and in controller you should create list of selectitem from that data. – Chetan Apr 22 '17 at 00:27

1 Answers1

0

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Hi Div, so based on your point, all the methods that does not deal with database operation should stay in controller. And can you elaborate more on your complex logic please? – bbusdriver Apr 23 '17 at 00:19
  • Yes! but *not exactly* it depends. For instance, user login using token, and at your end you need decrypt that token to check the user is valid user or not. Now the decryption logic is complex logic, and it doesn't deal with database right? so that logic should not goes to controller, it should be in BL(business logic) and that is what I meant by complex logic. http://imgr.es/3OI3 – Divyang Desai Apr 23 '17 at 16:09
  • Okay. So basically if my GetDateRange() function contains database operation which gets student info in its foreach loop, first I need to put this function in the controller and mix with business logic (_studentService.GetStudent(student)) inside of that function? – bbusdriver Apr 23 '17 at 19:45
  • Yes, exactly! keep in mind, the logic behind to separate out all the thing is loose coupling. – Divyang Desai Apr 24 '17 at 07:25
  • I wouldn't say the "service layer is your data access layer (DAL)", they're separate layers (and if @chetan did say that it looks like it was prior to a comment edit). But I agree that the two methods the OP has asked about should not go in the service class as they are returning presentation based objects. – d219 Apr 21 '20 at 11:12