253
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
the example. Especially for large models, it is advisable to have separate configuration classes for
configuring different entity types.
The code in the example shows a few explicit declarations and mapping. However,
EF Core
conventions do many of those mappings automatically, so the actual code you would need in your
case might be smaller.
The Hi/Lo algorithm in EF Core
An interesting aspect of code in the preceding example is that it uses the
Hi/Lo algorithm
as the key
generation strategy.
The Hi/Lo algorithm is useful when you need unique keys before committing changes.
As a summary,
the Hi-Lo algorithm assigns unique identifiers to table rows while not depending on storing the row in
the database immediately. This lets you start using the identifiers right away, as happens with regular
sequential database IDs.
The Hi/Lo algorithm describes a mechanism for getting a batch of unique IDs from a
related database
sequence. These IDs are safe to use because the database guarantees the uniqueness, so there will be
no collisions between users. This algorithm is interesting for these reasons:
•
It does not break the Unit of Work pattern.
•
It gets sequence IDs in batches, to minimize round trips to the database.
•
It generates
a human readable identifier, unlike techniques that use GUIDs.
EF Core supports
HiLo
with the
UseHiLo
method, as shown in the preceding example.
Map fields instead of properties
With this feature, available since EF Core 1.1, you can directly map columns to fields. It is possible to
not use properties
in the entity class, and just to map columns from a table to fields. A common use
for that would be private fields for any internal state that do not need to be accessed from outside the
entity.
You can do this with single fields
or also with collections, like a
List<>
field. This point was mentioned
earlier when we discussed modeling
the domain model classes, but here you can see how that
mapping is performed with the
PropertyAccessMode.Field
configuration highlighted in the previous
code.
Dostları ilə paylaş: