How do you expose an aggregate member for the purpose of presentation, while at the same time preventing that member from getting modified directly? So, I need something like read-only access to that member for the purpose of showing it on UI, For example:
class A {
B b;
void doSomething() {
b.update();
}
}
class B {
String getTitle() {
return title;
}
Items getItems() {
return items;
}
void update() {
...
}
}
interface SomeView {
void show(Items items);
}
The quick-and-dirty solution would be to add a method A::getB() and call someView.show(b.getItems()) but then the returned B instance could be modified outside of A (the B::update() could be called outside A).
I am thinking of several solutions but not quite sure which are best or if there is already a common practice to do so.
One of the solution that I have in mind is having a read-only version of B that is returned by A.
class A {
ReadOnlyB getReadOnlyB() {
return new ReadOnlyB(b);
}
}
class ReadOnlyB {
B b;
ReadOnlyB(B b) {
this.b = b;
}
String getTitle() {
return b.getTitle();
}
Items getItems() {
return b.getItems();
}
}
ItemPresentationinstead of justItemand haveItemMapperto map theItemtoItemPresentation. - Pablo EspantosoA::getB()would still not be a good thing. Like if there is a separate classCthat also belongs to the domain layer, it still wouldn't be good forCto be able to accessBdirectly. - Pablo Espantoso