Thursday, 15 January 2015

c# - What are the disadvantages to inheriting Persistence Models from Domain Models to save mapping effort using Entity Framework? -


I'm thinking of adding my Pristine Model Model (PM) to my first domain model (DM) classes Properties that are important for persistence logic. In this way I will not have to deal with many mapping codes.

For the sake of the case of a simple example, here is a DM that represents a credit which can be used by the user or organization to order.

  Namespace Foo.Domain {// Domain Model Public Square Credit: IDomainEntity, IAggregateRoot {public credits (int? UserId, int? Organization ID, date expiration date) {if (userId = = Null & amp; amp; Organization; id == faucet) New Invalid Operation exception throw ("credit must be assigned to user or organization."); If a new invalid operation (userId! = Null & amp; Organization ID! = Null) throws exceptions ("credit can not be assigned to user and organization."); UserId = userId; OrganizationId = Organization ID; End date = expiration date; } Public Virtual Ent ID {get; Private set; } Public Virtual Date Expiration Date {Received; Private set; } // External IAggregateRoot object reference is connected to public virtual int? UserId {get; Private set; } Public Virtual Int? Organization ID {Get; Private set; } Public Virtual Int? Order ID {Receive; Private set; } Public Zero AttachToOrder (Intra Order ID) {If (Expired> Date = DateTime.UtcNow) New Invalid Operation Exception ("The credit has expired."); If (order id! = Null) new invalid operation throw exception ("credit is unavailable."); OrderId = orderId; } Public Zero DetachFromOrder () {If (OrderId == blank) New InvalidOperationException Throw ("Credit is Available."); Order income = zero; }}}  

Say I want to continue this model with a foreign key barrier. Therefore I can not directly benefit domain model and from the barrier check of foreign keys in a relational database.

The Prime Minister's successor is the successor to add to my needs as required Navigation Properties Since EF requires a parasailable constructor, unfortunately I have to add DM, though it will not be fortunate to have an effect on its inert .

  Namespace Foo.Domain {// Adjusted Domain Model Public Sector Credit: IDomainEntity, IAggregateRoot {Secured credit} () {} Public credit (int? UserId, organization? Id, date expiration Date): This (remaining) remainder of the remaining class}}}}} name: Foo.Infrastructure {/ Public Virgin Credit}: Base () {} Public credit (int? UserId, int? Organization ID, date expiration date): Base UserId, organizationId, expiry date) {} Public Virtual User User {Received; Private set; } Public Virtual Organization Organization {get; Private set; } Public Virtual Order Order {get; Private set; }}}  

Now I think I can use it in Fluent API like this:

  modelbuilder.Entity < Credit & gt; (). Useful & lt; Order & gt; (C => c.Order) // So I need a navigation property. Multiple () .asForeignKey (c => c.OrderId);  

.. and define my DbContext like this:

  Namespace Foo.Infrastructure {Public Interface IFooDbContext {IDbSet & lt; Credit & gt; Credit {get; } Work & lt; Int & gt; SaveChangesAsync (); }}  

What is your view on this approach?

For me, the main loss is converting from the domain unit into the stability unit. When you get to the repository - it's okay, you can only be inserted from Foo.Infrastructure.Credit to Foo.Domain.Credit without problems (due to legacy) . But when you have to save the new Foo.Domain.Credit , then you have to create a new object Foo.Infrastructure.Credit and then transfer the properties from one to the other, You can not call new Foo.Domain.Credit , you need to use a factory, and be aware of this code for Foo.Infrastructure.Credit Should not, and do not think so good. Unfortunately, the unit framework does not allow us to enter our domain as "pure", is really unaware, so we have to deal with it.


No comments:

Post a Comment