Note that there are some explanatory texts on larger screens.

plurals
  1. POCode re-use with Linq-to-Sql - Creating 'generic' look-up tables
    primarykey
    data
    text
    <p>I'm working on an application at the moment in ASP.NET MVC which has a number of look-up tables, all of the form</p> <pre><code>LookUp { Id Text } </code></pre> <p>As you can see, this just maps the Id to a textual value. These are used for things such as Colours. I now have a number of these, currently 6 and probably soon to be more.</p> <p>I'm trying to put together an API that can be used via AJAX to allow the user to add/list/remove values from these lookup tables, so for example I could have something like:</p> <p><a href="http://example.com/Attributes/Colours/[List/Add/Delete]" rel="nofollow noreferrer">http://example.com/Attributes/Colours/[List/Add/Delete]</a></p> <p>My current problem is that clearly, regardless of which lookup table I'm using, <em>everything</em> else happens exactly the same. So really there should be no repetition of code whatsoever.</p> <p>I currently have a custom route which points to an 'AttributeController', which figures out the attribute/look-up table in question based upon the URL (ie <a href="http://example.com/Attributes/Colours/List" rel="nofollow noreferrer">http://example.com/Attributes/Colours/List</a> would want the 'Colours' table). I pass the attribute (Colours - a string) and the operation (List/Add/Delete), as well as any other parameters required (say "Red" if I want to add red to the list) back to my repository where the actual work is performed.</p> <p>Things start getting messy here, as at the moment I've resorted to doing a switch/case on the attribute string, which can then grab the Linq-to-Sql entity corresponding to the particular lookup table. I find this pretty dirty though as I find myself having to write the same operations on each of the look-up entities, ugh!</p> <p>What I'd really like to do is have some sort of mapping, which I could simply pass in the attribute name and get out some form of generic lookup object, which I could perform the desired operations on without having to care about type.</p> <p>Is there some way to do this to my Linq-To-Sql entities? I've tried making them implement a basic interface (IAttribute), which simply specifies the Id/Text properties, however doing things like this fails:</p> <pre><code>System.Data.Linq.Table&lt;IAttribute&gt; table = GetAttribute("Colours"); </code></pre> <p>As I cannot convert <code>System.Data.Linq.Table&lt;Colour&gt;</code> to <code>System.Data.Linq.Table&lt;IAttribute&gt;</code>.</p> <p>Is there a way to make these look-up tables 'generic'?</p> <p>Apologies that this is a bit of a brain-dump. There's surely imformation missing here, so just let me know if you'd like any further details. Cheers!</p>
    singulars
    1. This table or related slice is empty.
    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. 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