Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a Request class based on the same Entity in my Domain. It currently only has property definitions. I'd like to add a method for checking a duplicate Request which I'll call from my controller. Should I add a method called CheckDuplicate in the Request class? Would I be violating the SRP? The method will need to access a database context to check already existing requests. I'm thinking creating another class altogether for this logic that accepts a datacontext as part of its constructor. But creating a whole new class for just one method seems like a waste too. Any advice?

share|improve this question
    
If I had my way, all classes would have only one method unless that class was a container for logical groups of methods. But I'm a little extreme ;). –  mortalapeman Jul 28 '13 at 22:59
add comment

2 Answers

I'd go ahead and create it.

As you mentioned, adding a new responsibility to what's currently a DTO would violate the SRP. The question arises, what happens when the next requirement comes along? Would you add another method? What happens when you add a new attribute to the DTO? Would that necessary change add the risk of messing up your duplicate checking logic? Part of the idea of the SRP is that there should only be one reason to modify your class.

Moreover, a DuplicateChecker may turn out to be useful for other domain classes as you move forward.

It's not like classes are particularly expensive.

share|improve this answer
add comment

As per the question, you just need to validate if the entry is a duplicate? If thats the case that is like validation, so you can go ahead and create a method isValid() or isDuplicate() which would return a boolean value on checking the validations that you have.

share|improve this answer
    
not necessarily. I only need to check when a certain class of users are about to begin requesting. So the request isn't even complete yet. This is not being checked at submission of request. –  Riz Jun 28 '13 at 17:20
    
@Riz Yes true, what I mean is, if you have this validation for certain users, then you call the isValid() method only for those users and if it is true, then proceed further. –  JNL Jun 28 '13 at 17:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.