LocalDTO term refers to Fowler's article.
Say I have a domain entity Person
:
public class Person {
private String id;
private String name;
private int age;
}
This entity has its own semantics in the domain & application layer and has its behavior. In other words, it is not an anemic model.
Now I have a presentation class (assume an MVController or an MVPresenter) that needs to show a list of Persons and have a form that user fills to update a stored Person.
That's why I currently have the following contract.
public interface PersonService {
List<PersonDto> getPersons();
void updatePerson(PersonDto personDto);
}
And now my presentation class depends on person service. It does not know that Person
class exists and does not have not access to the domain world. I am aware that this is good thing.
However, the application is a desktop application. Fowler in his article says that DTO's should be used for remote communication (I understand why). In a desktop application, there is no remote communication (actually there is - I use DTOs there anyway). So my question, is this PersonDto
a LocalDTO?
If I follow Fowler's advice:
start with a locally invocable Service Layer whose method signatures deal in domain objects
I end up with:
public interface PersonService {
List<Person> getPersons();
void updatePerson(Person p);
}
I have the following problem: The presentation class will be able to call (say for example) person.setAge(int x)
. Which something I want to avoid since I consider this a "leakage". Also, to the application, calling setAge does only the validation. It does not have a meaning if this person is not persisted (passed to the service).
In order to avoid this leakage and make the domain entity read-only, I can have the setter package private
. Then service can manipulate Person objects and presentation class can only read from them. Which is great.
However, there is a problem with this approach as well. Testing. Testing the presentation classes require to create models. For example, given a person, is its name and age on the form? If Person objects are read only in presentation layer, I cant create Person objects for (in) presentation layer tests. But if I had DTOs, who cares? Just new PersonDto.
So what is the solution? DTO's?
Person
, it's hard to offer suggestions. Your mention ofsetAge
is suspicious as that's not behavior, so it's unclear what behavior the UI has here. From a layering standpoint, there's no problem having the presentation layer be aware of domain behavior -- in fact it has to be aware of it. The presentation layer would have a JavaBean, which is a form of DTO. And I'm unclear on what testing you're trying to do in the presentation layer.isAdultCheckBox
is selected (ticked) and cannot be changed. Something like this. When the X, the Button Y cannot be pressed. Regarding my actual problem, I cannot recall what exactly I was thinking back then but I have solved it on my own. Thanks for taking the time to read and comment my question. :)