Domain Driven Design with ORM

447 views Asked by At

i am designing the object model an application and there is certaint level of impedance mismtach between objects and tables. For example I have :

Product
-----------
ProductId,
ProductTypeCode,
StatusCode,
SKUNumber


ProductMarketAvailability
--------------------------
ProductMarketAvailabilityId,
ProductId,
MarketId,
Rank

ProductDescription
------------------
ProductDescriptionId,
ProductId,
MarketId,
StatusId,
DescriptionTypeId,
Description

(i didnt illustrate lookup tables:Status,DescriptionType,ProductType,Market)


I want to have a DOMAIN CLASS:

Product
--------------
ProductId,
SkuNumber,
MarketId,
MarketName,
StatusCode,
Status,
Title ,
Description,
Caption,
Rank

With LLblGen pro or Entity Framework :

 - how i can map these tables  my domain class?
 - Is it a best way to deal with this through Domain classes or better to 
   isolate domain classes from ORM generated classes
  - If I manually map ORM classes data to my domain classes then when i 
    persisting my Domain classes (imagine they are POCO Self tracking), 
    how can i write managable , well crafted code ?

i dont want to write , it doesnt look so right to me:

   if (myProduct.TitleisDirty)
    {
      ProductDescription p= myRepository.GetDescriptionById 
       (myProduct.ProductDescriptionIdForTitle);
      p.Description=myProduct.Title;
      p.SubmitChanges();
    }
   if (myProduct.RankisDirty)
    {
      ProductMarketAvailability pma= myRepository.GetMarketById
      (myProduct.ProductMarketAvailabilityId);
      pma.Rank=myProduct.Rank;
      pma.SubmitChanges();
    }

Thank you very much for reading.

1

There are 1 answers

0
Marius On

I don't think you can map these tables to one class (at least not in EF). Your model conveys a one to many relationship between Product and the other tables. If they shared the same primary key (a true 1:1) you could map it into one entity.

As for your other questions:

  • Is it a best way to deal with this through Domain classes or better to isolate domain classes from ORM generated classes?

Use the classes from your ORM tool of choice, don't start to manually create another model ontop of your ORM-classes (if you do this code-first they should be POCOs anyway)

  • If I manually map ORM classes data to my domain classes then when i persisting my Domain classes (imagine they are POCO Self tracking), how can i write managable , well crafted code?

You can't

I am guessing that this is a legacy app and that you can't touch the database so my suggestion is to map your tables into multiple classes and then expose the functionality you request as wrappers through the Product class. Example: Product.MarketAvailability.Rank becomes a wrapper property on product: Product.Rank.