Note that there are some explanatory texts on larger screens.

plurals
  1. POGenericising one class to handle multiple types
    primarykey
    data
    text
    <p>I have a series of about 30 lookup tables in my database schema, all with the same layout (and I would prefer to keep them as separate tables rather than one lookup table), and thus my Linq2SQL context has 30 entities for these lookup tables.</p> <p>I have a standard class that I would use for CRUD operations on each of these 30 entites, for example:</p> <pre><code>public class ExampleAttributes : IAttributeList { #region IAttributeList Members public bool AddItem(string Item, int SortOrder) { MyDataContext context = ContextHelper.GetContext(); ExampleAttribute a = new ExampleAttribute(); a.Name = Item; a.SortOrder = SortOrder; context.ExampleAttributes.InsertOnSubmit(a); try { context.SubmitChanges(); return true; } catch { return false; } } public bool DeleteItem(int Id) { MyDataContext context = ContextHelper.GetContext(); ExampleAttribute a = (from m in context.ExampleAttributes where m.Id == Id select m).FirstOrDefault(); if (a == null) return true; // Make sure nothing is using it int Count = (from m in context.Businesses where m.ExampleAttributeId == a.Id select m).Count(); if (Count &gt; 0) return false; // Delete the item context.ExampleAttributes.DeleteOnSubmit(a); try { context.SubmitChanges(); return true; } catch { return false; } } public bool UpdateItem(int Id, string Item, int SortOrder) { MyDataContext context = ContextHelper.GetContext(); ExampleAttribute a = (from m in context.ExampleAttributes where m.Id == Id select m).FirstOrDefault(); a.Name = Item; a.SortOrder = SortOrder; try { context.SubmitChanges(); return true; } catch { return false; } } public String GetItem(int Id) { MyDataContext context = ContextHelper.GetContext(); var Attribute = (from a in context.ExampleAttributes where a.Id == Id select a).FirstOrDefault(); return Attribute.Name; } public Dictionary&lt;int, string&gt; GetItems() { Dictionary&lt;int, string&gt; Attributes = new Dictionary&lt;int, string&gt;(); MyDataContext context = ContextHelper.GetContext(); context.ObjectTrackingEnabled = false; Attributes = (from o in context.ExampleAttributes orderby o.Name select new { o.Id, o.Name }).AsEnumerable().ToDictionary(k =&gt; k.Id, v =&gt; v.Name); return Attributes; } #endregion } </code></pre> <p>I could replicate this class 30 times with very minor changes for each lookup entity, but that seems messy somehow - so can this class be genericised so I can also pass it the type I want, and have it handle internally the type differences in the linq queries? That way, I have one class to make additions to, one class to bug fix et al - seems the way that it should be done.</p> <p><strong>UPDATE:</strong></p> <p>Andrews answer below gave me the option that I was really looking at while thinking about the question (passing the type in) but I need more clarification on how to genericise the linq queries. Can anyone clarify this?</p> <p>Cheers</p> <p>Moo</p>
    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.
 

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