Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've found a pattern which I think works best--In my case, at least. <br /><br /><br /> I extend entity classes using partial classes. I use partial classes so the signature of the entity does not change (see the <code>DeleteOnSubmit</code> call in the <code>Delete</code> method).</p> <p>I've cooked up a a small example. Here's an image of the database and LINQ to SQL class setup:</p> <p><img src="https://i.stack.imgur.com/WqEHH.png" /> <br /><br /> And here's the partial class in which I implement business logic:</p> <pre><code>/// &lt;summary&gt; /// This class extends BusinessLogicDataContext.Products entity class /// &lt;/summary&gt; public partial class Product { /// &lt;summary&gt; /// New up a product by column: dbo.Products.ProductId in database /// &lt;/summary&gt; public Product(Int32 id) { var dc = new BusinessLogicDataContext(); // query database for the product var query = ( from p in dc.Products where p.ProductId == id select p ).FirstOrDefault(); // if database-entry does not exist in database, exit if (query == null) return; /* if product exists, populate self (this._ProductId and this._ProductName are both auto-generated private variables of the entity class which corresponds to the auto-generated public properties: ProductId and ProductName) */ this._ProductId = query.ProductId; this._ProductName = query.ProductName; } /// &lt;summary&gt; /// Delete product /// &lt;/summary&gt; public void Delete() { // if self is not poulated, exit if (this._ProductId == 0) return; var dc = new BusinessLogicDataContext(); // delete entry in database dc.Products.DeleteOnSubmit(this); dc.SubmitChanges(); // reset self (you could implement IDisposable here) this._ProductId = 0; this._ProductName = ""; } } </code></pre> <p>Using the implemented business logic:</p> <pre><code>// new up a product var p = new Product(1); // p.ProductId: 1, p.ProductName: "A car" // delete the product p.Delete(); // p.ProductId: 0, p.ProductName: "" </code></pre> <p>Furthermore: LINQ to SQL entity classes are very open in nature. This means that the property corresponding to the <em>dbo.Products.ProductId</em> column implements both a getter and a setter--this field should not be changeable.</p> <p>To my knowledge you can't override properties using partial classes, so what I usually do is implement a manager which narrows the object using an interface:</p> <pre><code>public interface IProduct { Int32 ProductId { get; } void Delete(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload