I would like to ask you a question about dividing the method into small units. It hasn't been long since I learned about DDD(Domain Driven Design).
In both ways, branch logic is likely to increase somewhere if the value of the field is changed or added.
I'm thinking, "What's a design that can better adapt to change?"
I'm ashamed that the attached code seems too low, but from the designer's point of view, I'd like to know how to decide on a good design.
I want to ask you about the design method that will be flexible, but I don't think that can be a clear question.
Please let me know which of the two methods below can be a better design.
I implemented a separate method for updating field values in domain entities for each field.
For example, updateName()
and updatePassword()
have been implemented to update updatedAt for each method.
If a field is added, add a method.
The more fields are added to an entity, the more code call touch()
..!
Within an entity object, a method of a clear role can be identified and will be used in conjunction with branch logic when used elsewhere.
public class User {
private final UUID id;
private final Instant createdAt;
private Instant updatedAt;
private String name;
private String password;
public void touch() {
this.updatedAt = Instant.now();
}
public void updatePassword(String password) {
this.password = password;
touch();
}
public void updateName(String name) {
this.name = name;
touch();
}
}
receive multiple parameters in one method called update()
,
I was using the method of updating only the parameters that received the value and updating the updated At.
I should also pay attention to the method signature, right..?
Elsewhere, you only need to call 'update()' but pay attention to the parameter order.
public class User {
private final UUID id;
private final Instant createdAt;
private Instant updatedAt;
private String name;
private String password;
public void update(String name, String password) {
boolean anyValueUpdated = false;
if (name!= null && !name.equals(this.username)) {
this.name= name;
anyValueUpdated = true;
}
if (password != null && !password.equals(this.password)) {
this.password = password;
anyValueUpdated = true;
}
if (anyValueUpdated) {
this.updatedAt = Instant.now();
}
}
P.S If you have any advice on my concerns, please let me know! Keywords are really good :)
Firstly, keeping your properties private is the preferable thing to do. In DDD, always use a public method to update private properties. This is where you can enforce invariants before updating the property and declare your method using ubiquitous language.
Secondly, and importantly, your public methods should be oriented around your use cases. Specifically, if there is a use case where only a name is to be updated without the password being changed, then it makes sense to have an UpdateName method. Similarly, if there is a use case where user only wants to change password and not the name then an UpdatePassword method is appropriate. I.E. Method1
If they are potentially both updated as part of a single action then Method2 is better, but I'd avoid the approach of 'ignoring' nulls. You'll lose clarity of method's purpose.