Edition 0 Updated to asp. Net core 0


Data Annotations versus Fluent API



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə220/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   216   217   218   219   220   221   222   223   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

Data Annotations versus Fluent API 
There are many additional EF Core conventions, and most of them can be changed by using either 
data annotations or Fluent API, implemented within the OnModelCreating method. 
Data annotations must be used on the entity model classes themselves, which is a more intrusive way 
from a DDD point of view. This is because you are contaminating your model with data annotations 
related to the infrastructure database. On the other hand, Fluent API is a convenient way to change 
most conventions and mappings within your data persistence infrastructure layer, so the entity model 
will be clean and decoupled from the persistence infrastructure. 
Fluent API and the OnModelCreating method 
As mentioned, in order to change conventions and mappings, you can use the OnModelCreating 
method in the DbContext class. 
The ordering microservice in eShopOnContainers implements explicit mapping and configuration
when needed, as shown in the following code. 
// At OrderingContext.cs from eShopOnContainers
protected
override
void
OnModelCreating
(ModelBuilder modelBuilder) 

// ...
modelBuilder.
ApplyConfiguration
(
new
OrderEntityTypeConfiguration
()); 
// Other entities' configuration ...

// At OrderEntityTypeConfiguration.cs from eShopOnContainers
class
OrderEntityTypeConfiguration : IEntityTypeConfiguration 

public
void
Configure
(EntityTypeBuilder orderConfiguration) 

orderConfiguration.
ToTable
(
"orders"
, OrderingContext.
DEFAULT_SCHEMA
); 
orderConfiguration.
HasKey
(o => o.
Id
); 
orderConfiguration.
Ignore
(b => b.
DomainEvents
); 
orderConfiguration.
Property
(o => o.
Id

.
UseHiLo
(
"orderseq"
, OrderingContext.
DEFAULT_SCHEMA
); 


252 
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns 
//Address value object persisted as owned entity type supported since EF Core 2.0
orderConfiguration 
.
OwnsOne
(o => o.
Address
, a => 

a.
WithOwner
(); 
}); 
orderConfiguration 
.
Property
<
int
?>(
"_buyerId"

.
UsePropertyAccessMode
(PropertyAccessMode.
Field

.
HasColumnName
(
"BuyerId"

.
IsRequired
(
false
); 
orderConfiguration 
.
Property
(
"_orderDate"

.
UsePropertyAccessMode
(PropertyAccessMode.
Field

.
HasColumnName
(
"OrderDate"

.
IsRequired
(); 
orderConfiguration 
.
Property
<
int
>(
"_orderStatusId"

.
UsePropertyAccessMode
(PropertyAccessMode.
Field

.
HasColumnName
(
"OrderStatusId"

.
IsRequired
(); 
orderConfiguration 
.
Property
<
int
?>(
"_paymentMethodId"

.
UsePropertyAccessMode
(PropertyAccessMode.
Field

.
HasColumnName
(
"PaymentMethodId"

.
IsRequired
(

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   216   217   218   219   220   221   222   223   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin