0

我正在设计一个应用程序的对象模型,并且对象和表格之间存在一定程度的阻抗失配。例如我有:

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


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

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

(我没有说明查找表:状态、描述类型、产品类型、市场)


我想要一个DOMAIN CLASS

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

使用 LLblGen pro 或实体框架:

 - 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();
    }

非常感谢您的阅读。

4

1 回答 1

0

我认为您不能将这些表映射到一个类(至少不能在 EF 中)。您的模型传达了 Product 和其他表之间的一对多关系。如果他们共享相同的主键(真正的 1:1),您可以将其映射到一个实体中。

至于你的其他问题:

  • 是通过域类来处理这个问题的最佳方法,还是更好地将域类与 ORM 生成的类隔离开来?

使用您选择的 ORM 工具中的类,不要开始在您的 ORM 类之上手动创建另一个模型(如果您首先执行此代码,它们无论如何都应该是 POCO)

  • 如果我手动将 ORM 类数据映射到我的域类,那么当我持久化我的域类时(想象它们是 POCO 自我跟踪),我如何编写可管理的、精心设计的代码?

你不能

我猜这是一个遗留应用程序,你不能接触数据库,所以我的建议是将你的表映射到多个类中,然后通过 Product 类将你请求的功能作为包装器公开。示例:Product.MarketAvailability.Rank 成为产品的包装属性:Product.Rank。

于 2012-01-31T19:45:00.897 回答