core-java
core-java copied to clipboard
Context description in Protobuf
A Bounded Context is a logical and structural boundary of a model.
Currently, the structural part is implemented via the BoundedContext
class, while the logical part is implemented by the similarly-named @BoundedContext
Java annotation. The problem with having an annotation in Java is that it might not be available to other users of the model, such as JS/Dart/mobile clients.
What is proposed is a package-info
and model-info
like Protobuf file, which would describe the Context. For example, consider a users_context.proto
:
syntax = "proto3";
package acme.users;
import "spine/context_options.proto";
option (context) = {
name: "Users"
multitenant: true
};
option (type_url) = "type.acme.corp";
The _context.proto
suffix here is a convention, just like the _events.proto
and similar.
Now, in Java, we construct an instance of BoundedContext
:
BoundedContext.from(UsersContext.getDescriptor())
.add(...)
.add(...)
.build();
This way, we join the logical and the structural parts.
All the Proto types are mapped to their BoundedContext by the common package, i.e. all the Users
types should belong to the acme.users
package or its sub-packages.
We may also want to add options that would help to create and update a Bounded Context Canvas automatically.
@alexander-yevsyukov, @armiol, FYI.
I really like it!
As discussed today with @alexander-yevsyukov, we need to extend this feature to Kotlin and Java.
The idea is that the code generated out of such Proto definitions would answer to the question, to which Bounded Context an Entity (in whichever language it is defined) belongs.
In particular:
- The generated code should be accessible at compile-time to allow
model-assembler
andmodel-verifier
function. - The generated code should be accessible at run-time, so that we could determine the Bounded Context for a given Entity state when, for instance, determining if an event is external.
To do (in no particular order):
- [ ] Generate the code based on
_context.proto
file into "model" sources which would satisfy pp. 1 and 2 above. - [ ] Keep
@BoundedContext
annotation, use it in the generated code, but prohibit its usage with the package. This is needed to allow compile-time access. - [ ] Detect the Bounded Context not basing on the Java/Kotlin package of the corresponding server-side implementation (e.g.
MyProjection : Projection
), but judging upon the package of the Entity state type.
@alexander-yevsyukov @dmdashenkov FYI.
P.S. As discussed, this feature will be implemented after we fully migrate to ProtoData-based generation from protoc
-based implemenatation.