In addition, I found this, but it has nothing to do with SRP
Well, at first glance, neither getterters nor setters violate the Single Responsibility Principle because they only Have the logic of “belonging” to the current class. They can access/write class members for a purpose of “service”. Fine.
But wait, let’s first define the basic terms:
Data Access = setter and getter
Data processing = data processing, CRUD, verification and other operations
If so, then we have two different responsibilities in one class, thus breaking SRP.
Let us temporarily assume that in order not to destroy SRP, we will 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 good because we have not violated the said SRP. But here I have only one setter and only the getter in the DA class.
The question now is:
1) Even if I only have a setter and getter, should I always create another DA class so that it does not break the SRP ?
2) Do the determiners and inhales really break the SRP, should they be used in any class?
3) If so, dependency injection is always the answer! ?
Setters and Getters are not the point. The point of SRP is that a class should have only one responsibility.
Representing domain objects is a major responsibility. Perform this operation The objects are usually called “data objects”. Due to language design or conventions, data objects usually have setters and getters, but they are not a separate responsibility themselves; they are just pipes.
Output persistent storage is another major responsibility. The object that performs this operation is usually called a “data access object” (DAO). A DAO that is not a data object may not need setters and getters for the properties of the data object type it manages, although I can imagine a very bad framework that requires them. Like DAO, using data objects (displaying them, serializing and deserializing them, performing calculations on them, etc.) and other types of objects that are not the data object itself may not The setter and getter of the data object need to be mirrored.
Therefore, setting the setter and getter means that your object is a data object. If it is a data object, and it is also a DAO or there are other major responsibilities, It may violate SRP.
Side note: You mentioned verification. In a typical application verification, at least a single data object belongs to the data object itself, because it represents a domain object and enforces a relationship between domain object properties Individual correctness and relationship are almost the same responsibility.
Even if I only have one setter and getter, should I always create another DA class?
Generally speaking, yes. The point is not the number of attributes; the point is that representation and access are two different responsibilities and belong to different classes.
There are many typical applications. Domain objects, so if it makes sense to separate domain objects from their access rights, then it makes sense to operate consistently on all domain objects (even single-attribute objects).
Dependency injection is always the answer NS?
It depends on your architecture and framework. You may have immutable data objects and DAOs whose methods take them as parameters; there is no DI there (although you can inject DAOs into higher Level components). You may have a DAO instantiated with a data object or a data object with a DAO reference (I have seen but I don’t like these two modes); you may need DI there. Either way, it’s the same as The rest of the discussion does not matter much.
I recently read an article that describes that they may obviously break SRP. And now I am completely confused because I have written some for a long time Single course with setters and getters.
Also, I found this, but it has nothing to do with SRP
Well, at first glance, getters and Neither setter violates the Single Responsibility Principle, because they only have the logic to “belong” to the current class. They can access/write class members that “service” a purpose. Fine.
But wait, we define first Basic terminology:
Data access = setter and getter
Data processing = data processing, CRUD, verification and other operations
If this is the case, then we are in one There are two different responsibilities in the class, thus breaking the SRP.
Let us temporarily assume that in order not to break the SRP, we will define data access and data operations in different classes.
< p>
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
}< br />}
It looks good because we have not violated the said SRP. But here I have only one setter and only the getter in the DA class.
The problem now is :
1) Even if I only have one setter and getter, should I always create another DA class so that it does not break the SRP?
2) Do the determiners and inhales really break the SRP, should they be used in any class?
3) If so, dependency injection is always the answer! ?
Did the setters and inhales break the SRP?
Setters and Getters are not the point. The point of SRP is that a class should have only one responsibility.
Representing domain objects is a major responsibility. Perform this operation The objects are usually called “data objects”. Due to language design or conventions, data objects usually have setters and getters, but they are not a separate responsibility themselves; they are just pipes.
Output persistent storage is another major responsibility. The object that performs this operation is usually called a “data access object” (DAO). A DAO that is not a data object may not need setters and getters for the properties of the data object type it manages, although I can imagine a very bad framework that requires them. Like DAO, using data objects (displaying them, serializing and deserializing them, performing calculations on them, etc.) and other types of objects that are not the data object itself may not The setter and getter of the data object need to be mirrored.
Therefore, setting the setter and getter means that your object is a data object. If it is a data object, and it is also a DAO or there are other major responsibilities, It may violate SRP.
Side note: You mentioned verification. In a typical application verification, at least a single data object belongs to the data object itself, because it represents a domain object and enforces a relationship between domain object properties Individual correctness and relationship are almost the same responsibility.
Even if I only have one setter and getter, should I always create another DA class?
Generally speaking, yes. The point is not the number of attributes; the point is that representation and access are two different responsibilities and belong to different classes.
There are many typical applications. Domain objects, so if it makes sense to separate domain objects from their access rights, then it makes sense to operate consistently on all domain objects (even single-attribute objects).
Dependency injection is always the answer NS?
It depends on your architecture and framework. You may have immutable data objects and DAOs whose methods take them as parameters; there is no DI there (although you can inject DAOs into higher Level components). You may have a DAO instantiated with a data object or a data object with a DAO reference (I have seen but I don’t like these two modes); you may need DI there. Either way, it’s the same as The rest of the discussion does not matter much.