2010-02-02 67 views
1

有没有人在这里使用LINQ to SQL来支持域 模型的持久性?LINQ2SQL,持久性无知和域模型

我不打算使用LINQ2SQL实体设计器,只是简单的手工编码的XML映射,我目前有障碍。

我正在尝试在我正在做的DDD示例中使用它,因为我的受众只知道LINQ2SQL。

回答

0

POCOs可以被映射,因此它可以与DataContext一起使用。

但是,我发现映射有点泄漏。数据模型信息现在泄露给域实体。考虑下面这个例子:

<Table Name="dbo.products"> 
    <Type Name="Ptc.Store.Core.Product"> 
     <Column Name="id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" CanBeNull="false" DbType="INT NOT NULL IDENTITY" /> 
     <Column Name="name" Member="Name" CanBeNull="false" DbType="VARCHAR(50) NOT NULL" /> 
    </Type> 
</Table> 

<Table Name="[dbo].branches"> 
    <Type Name="Ptc.Store.Core.Branch"> 
     <Column Name="id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" DbType="INT NOT NULL IDENTITY" /> 
     <Column Name="name" Member="Name" CanBeNull="false" DbType="VARCHAR(50) NOT NULL" /> 
    </Type> 
</Table> 

<Table Name="[dbo].sales"> 
    <Type Name="Ptc.Store.Core.Sale"> 
     <Column Name="id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" CanBeNull="false" DbType="INT NOT NULL IDENTITY" /> 
     <Column Name="transaction_date" Member="TransactionDate" CanBeNull="false" DbType="DATETIME" /> 
     <Column Name="amount" Member="Amount" CanBeNull="false" DbType="MONEY" /> 
     <Association Name="sales_item" Member="Item" IsForeignKey="true" ThisKey="ProductId" OtherKey="Id" /> 
     <Association Name="sales_location" Member="Location" IsForeignKey="true" ThisKey="BranchId" OtherKey="Id" /> 

     <!-- Why do I have to map foreign keys? Looks leaky to me.--> 
     <Column Name="product_id" Member="ProductId" CanBeNull="false" DbType="INT" /> 
     <Column Name="branch_id" Member="BranchId" CanBeNull="false" DbType="INT" /> 
    </Type> 
</Table> 

更坏的是,我要对我的实体显式声明的属性相匹配的数据模型的外键。我不需要用NHibernate来做到这一点。

有没有更好的方法来做到这一点,而不明确写入属性及其映射到数据模型?