Question Details

No question body available.

Tags

c# encapsulation single-responsibility-principle open-closed-principle

Answers (1)

December 18, 2025 Score: 9 Rep: 9,000 Quality: Medium Completeness: 80%

I think you misunderstood the single responsible principle. Especially, I don't think it is wise to separate the data into your order class and the logic into your service class. One idea of object-oriented programing is to have data and methods that work with the data encapsulated into a class.

I don't know how your validation works. But I just imagine you check whether the ID matches a format (like first 4 digits are the year, the next 4 digits are a running number, and the last digit is a checksum) and whether your order amount is positive. Hence the validation is highly coupled to the properties of the order class. If you change the properties of your order class, it is very likely that you also have to change your validation. You always have to change two classes if you have to change something about your order structure.

There are good reasons to have a separate class for order validation: 1st: If your validation logic is really complex and your order classes would become too big. 2nd: If your validation class can also validate other stuff than orders. 3rd: If your validation consists only of I/O, which I like to separate always from the rest of the code. But if any of these cases is true for your case, I would still do the following approach:

 public class Order : IOrder
 {
     private IValidationService validation;

public int OrderID { get; private set; }

public int NumberOfPieces { get; private set; }

public bool IsValidated { get; private set; }

public Order(IValidationService validation) { validation = validation; }

public void Validate() { if(_validation.Validate(this)) { IsValidated = true; } } }

This has the following advantage: When you see the order class, you can see directly that there is the possibility to validate an order and where it is done in the code. If you add a property to your order class, it is more likely that you won't forget to also check this property in your validation service. Moreover, if you have an order object at some point of your code and you have to validate it, you don't have to think about from where to get an validation object, because the order will deliver this object by itself.

You can also see this question and its answer for more thoughts about the SRP: Am I following the SRP with this structure?