0

I'm about to build my service layer and I've got some doubts. Should I create particular service(cs class + interface) related with controller or should I create the service related with poco class exclusively?

Let me give you an example. I've got: StudentPoco, TeacherPoco, StudentService, TeacherService, StudentController, TeacherController.

And now form StudentController I'd like to call service's method - GetAllTeachers(...) - student wants to see a list. Should I put this function in StudentService because I call it from StudentController or should I put it in TeacherService bacause it's related with TeacherPocos - we're dealing with teachers. What's gonna happen if we call GetOnlyMyTeachers(...) from StudentController?

Next issue: Should one service reference to services which it uses? What if StudentService has reference to TeacherService and TeacherService has reference to StudentService? Is it OK?

I'd like to notice that I'm familiar with DI.

I use MVC5 and EF6 code first. I don't want to use repository and UoW patterns. I'll have DbContext reference in all services. Is it right to call savechanges method many times?

What do you think about my doubts?

WaltLift0
  • 43
  • 1
  • 5
  • Read this article: https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=92 and this one as well: https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91 – trailmax Sep 13 '14 at 20:50

1 Answers1

0

First and foremost it's important to understand that generally a service layer (sometimes called a business layer) is about encapsulating business logic. As you build your service layer bear this in mind when grouping your methods. Try and ignore the types that it returns, there may be instances where a possible StudentService needs to return a teacher poco for a method. The implementation shouldn't drive the design of your business layer.

In this instance of your GetAllTeachers() method. This would belong in the TeacherService regardless of whether it's called from the StudentController or TeacherController.

It is also fine for services to call services. Inside of a StudentService you may want to get a GetTeacher() method from the TeacherService. If you're familiar with Dependency Injection then your best solution would be to inject these dependencies via your IoC of choice.

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63