user2062383
user2062383

Reputation: 959

Creating a Service Layer for MVC4 Applications while honoring Single Responsibility

I'm trying to learn some best practices while I ramp up on MVC4. I have a solution with three projects:

  1. Web: MVC stuff
  2. Core: Data Model
  3. Tests: Testing classes

I'm now trying to add:

4: Services: Business Logic

This will take logic such as "GetFilteredItems()" from my controllers and place them into a Service project, which depends on Core, and upon which Web depends. As I'm beginning to do this, I want to honor single responsibility and create one class per method. My questions:

1) Is this a good approach?
2) Say I need a GetFilteredItems method that takes an ID and returns a List. What's the convention here in terms of naming classes and methods? I can't have a class SampleClass with a method SampleClass() that returns something, right?

Upvotes: 2

Views: 3272

Answers (2)

Kevin Junghans
Kevin Junghans

Reputation: 17540

There is a good discussion on creating a service layer in MVC here.

Single responsibility does not require classes with single methods. I have never heard of this approach before. Where is there a discussion on this approach and its benefits?

Upvotes: 1

Rik Leigh
Rik Leigh

Reputation: 1328

Yes this seems like a good approach, although the single responsibility principle doesn't necessarily mean that you need to create a separate class per method.

Just make sure that your service classes have a single responsibility.

For your GetFilteredItems method, you could create a class called ItemService to contain this. This service would also contain any other methods for returning collections of items.

You could then put methods pertaining to persisting items in a different service, and so on...

Upvotes: 0

Related Questions