Strongly-Typed ids with Marten #97
oskardudycz
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Strongly typed ids (or, in general, a proper type system) can make your code more predictable. It reduces the chance of trivial mistakes, like accidentally changing parameters order of the same primitive type.
So for such code:
the compiler won't catch if you switch
reservationId
withseatId
.If you use strongly typed ids, then compile will catch that issue:
They're not ideal, as they're usually not playing well with the storage engines. Typical issues are: serialisation, Linq queries, etc. For some cases they may be just overkill. You need to pick your poison.
To reduce tedious, copy/paste code, it's worth defining a strongly-typed id base class, like:
Then you can define specific id class as:
You can even add additional rules:
The base class working with Marten, can be defined as:
Marten requires the id with public setter and getter of
string
orGuid
. We used the trick and addedAggregateId
with a strongly-typed backing field. We also informed Marten of the Identity attribute to use this field in its internals.Example aggregate can look like:
See the full sample here.
Read more in the article:
This discussion was created from the release v9.4.0.
Beta Was this translation helpful? Give feedback.
All reactions