views:

34

answers:

1

I have an interface for Client Registration called IRegistrationService. This contains one method called Register and it is implemented through the class RegistrationService. If I wanted to have methods for Delete, Update, Retrieve for example, would I create a separate interface for each action such as IDeletionService, IUpdateService, IRetrieveService or just put all the methods into IRegistrationService. The reason I ask this is because this is what the SOLID principles especially the SRP principle seem to ask.

+2  A: 

One way of stating the Single Responsibility Principle is that a class should have only one reason to change. That doesn't necessarily mean that it only does one thing, but rather that it only deals with one sphere of responsibility.

So it's fine for your registration service to know all about people getting registered, and I'd include removing, updating, retrieving registrations in that. If the registration process changes (e.g. you decide that all new or updated users get sent an email) than the class changes. However, the implementation details of how the registration email gets sent do not belong in this service -- that would be a second reason the class might change (e.g. you realize you want to send emails via an external SMTP server rather than locally, or via SMS rather than email, etc.).

JacobM
So going off your answer, Registration encapsulates RegisterUser, DeleteUser, UpdateUser methods, but if I want to send out notifications to users through Email or Sms, I probably should have another class that is called something like UserNotifications which handles Messages. Did I understand you correctly? The one thing you said that still throws me off is that the class does not do one thing, it deals with a sphere of responsibility. What do you mean by sphere? Do you have an example? Thanks
Xaisoft
Most of the SOLID principles are pretty concrete, but the SRP is somewhat subjective. Capable developers' opinions can differ on what constitutes a single responsibility, or as JacobM put it, a "sphere" of responsibility. That said, I believe your understanding is correct based on the UserNotifications example you give. Perhaps the SRP can be best explained by thinking of a clear violation of it: A class which provides methods to register clients, calculate their mortgage interest, and notify them if they're late on making a payment would have multiple responsibilities.
Kevin D.
Agreed that defining a sphere of responsibility can be difficult. And agreed that user notifications would be a separate class. One way to think about it is to think about the type of conversation you would have when figuring out how a class should work. You'd have one type of conversation (probably with your client) to think about changes to the registration process, and a different conversation with someone else to figure out, say, what database driver to use to connect when registering. A single class shouldn't know about both of those things.
JacobM