I've recently read an article that describes how they clearly may break the SRP. And now I'm totally confused, because I wrote single classes with setters and getters for a long time.
Also, I've found this, but it has nothing to do with the SRP
Well, at first glance, both getters and setters do not break the Single Responsibility Principle because they have logic which "belongs" only to the current class. They can access/write the class members which "serves" a single purpose. Fine.
But wait, let's define the basic terms first:
Data Access = both setters and getters
Data Handling = Data handling, operations such as CRUD
, validation etc
If so, then we have two different responsibilities within a single class, thus breaking the SRP.
Let's assume for a moment, that in order not to break the SRP, we'll define Data Access and Data Manipulation in different classes.
class DA { // <- Data Access
public string getName() {
return this.name;
}
public string setName(name) {
this.name = name;
}
}
class DataHandler {
public DataHandler(da) { // <- Inject an instance of DA
this.da = da;
}
public bool validate() {
// validation stuff
}
}
It looks fine, because we do not violate said SRP. But here I have only one setter and only getter in the DA class.
Now the questions:
1) Should I always create another DA class even if I have only one setter and getter, just so that it does not break the SRP?
2) Do setters and getters really break the SRP, and should they never be used within any class?
3) And, if so, is Dependency Injection always the answer!?