I'm reading Implementing Domain Driven Design by Vaughn Vernon. In one of the examples he shows a Forum being created in the Collaboration bounded context. Before it's created, a Creator value object is instantiated. The information for the Creator object comes from a different bounded context. A HTTP request is made to a REST API to retrieve a User from the Identity and Access bounded context. It is then translated to a Creator object.
private Forum startNewForum(
Tenant aTenant,
String aCreatorId,
String aModeratorId,
String aSubject,
String aDescription,
String anExclusiveOwner) {
Creator creator =
this.collaboratorService().creatorFrom(aTenant, aCreatorId);
Moderator moderator =
this.collaboratorService().moderatorFrom(aTenant, aModeratorId);
Forum newForum =
new Forum(
aTenant,
this.forumRepository().nextIdentity(),
creator,
moderator,
aSubject,
aDescription,
anExclusiveOwner);
this.forumRepository().save(newForum);
return newForum;
}
UserInRoleAdapter makes a call to a REST API in another bounded context and translates that into a Creator object.
public class TranslatingCollaboratorService implements CollaboratorService {
private UserInRoleAdapter userInRoleAdapter;
...
@Override
public Creator creatorFrom(Tenant aTenant, String anIdentity) {
Creator creator =
this.userInRoleAdapter()
.toCollaborator(
aTenant,
anIdentity,
"Creator",
Creator.class);
return creator;
}
...
}
JSON retrieved from REST API is used to instantiate a Creator object.
private T extends <Collaborator> T newCollaborator(String aUsername,
String aFirstName,
String aLastName,
String aEmailAddress,
Class<T> aCollaboratorClass)
throws Exception {
Constructor<T> ctor =
aCollaboratorClass.getConstructor(
String.class, String.class, String.class);
T collaborator =
ctor.newInstance(
aUsername,
(aFirstName + " " + aLastName).trim(),
aEmailAddress);
return collaborator;
}
Is this call to the REST API equivalent to calling an application service method in the other bounded context directly? Or how is it different? Is calling application services directly in other bounded contexts and then translating the result allowed? Is having a REST API for communicating directly between bounded contexts common?
EDIT: My question is similar to this one but I just want a bit more information. In that question it is said that it is preferable to use events and keep local copies of data over calling an application method in another bounded context. If these application methods are put behind REST interfaces, is it still preferable to use events. Is it always frowned upon to call application methods of other bounded contexts directly or should it just be used as least as possible?