views:

60

answers:

1

Has anyone here have used LINQ to SQL to support the persistence of domain models?

I'm not planning to use the LINQ2SQL entity designer, just plain-old hand-coded XML mapping and I'm currently having roadblocks.

I'm attempting to use it in a DDD example I'm doing, since my audience only knows LINQ2SQL.

A: 

POCOs can be mapped so it can be used with a DataContext.

However, I find the mapping a bit leaky. Data model information is now leaking to the domain entity. Consider this example:

    <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>

The worse part is that I have to explicitly declare properties on my entities to match the data model's foreign keys. I didn't have to do this with NHibernate.

Is there any better way to do this without explicitly writing properties and their mapping to the data model?

leypascua