Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching large number of entities in WPF
    primarykey
    data
    text
    <p>I am writing an application in WPF. I am using Entity Framework 5 and was wondering if you can give me advice about how to handle the following situation.</p> <p>I have basically only three tables:</p> <p>Item {ID, Name}</p> <p>Attribute {ID, Name, Type}</p> <p>AttributeValue {ItemID, AttributeID, Value}</p> <p>Our client wants to add attributes to his items and this seems to work quite well. Now he can add attributes and assign them with values to his items. First question: To you think this is good design? </p> <p>The question is now how to display the items in a WPF DataGrid and show all the attributes as columns. This is what we have now:</p> <p>We generate the columns programmatically: </p> <pre><code>foreach (var attribute in db.Attributes) { datagrid.Columns.Add(new DataGridTextColumn { Header = attribute.Name, Binding = new Binding { Path = new PropertyPath(string.Format("[{0}]", attribute.ID)) } }); } </code></pre> <p>This works fine. The question now is how to implement the indexer in the Items-Class. We have one option which gets quite slow with large amount of data (20.000 items, 400 Attributes, every item has 100 values):</p> <pre><code>public AttributeValue this[int i] { get { return AttributeValues.FirstOrDefault(aa =&gt; aa.AttributID == i); } } </code></pre> <p>Like i said, this works but it gets slow. Instead of always querying for the attributevalues i thought of caching everything before showing like this:</p> <pre><code>var items = db.Items.AsNoTracking().ToArray(); CachedValues.Values = new Dictionary&lt;int, Dictionary&lt;int, AttributeValue&gt;&gt;(items.Length); foreach (var item in items) { var attributevalues = db.AttributeValue.AsNoTracking().Where(w =&gt; w.ArtikelID == item.ID).ToArray(); CachedValues.Values[item.ID] = new Dictionary&lt;int, AttributeValue&gt;(attributevalues.Length); foreach (var value in attributevalues) { CachedValues.Values[item.ID][value.AttributeID] = value; } } </code></pre> <p>I use a static class as the Cache: </p> <pre><code>public static class CachedValues { public static Dictionary&lt;int, Dictionary&lt;int, ArtikelAttribut&gt;&gt; Values; } </code></pre> <p>And in the Items-Class I can then access the cache:</p> <pre><code>public AttributeValue this[int i] { get { AttributeValue val = null; CachedValues.Values[ID].TryGetValue(i, out val); return val; } } </code></pre> <p>Obviously it takes some time (15s) to initialize the cache but then it's a lot faster. Sorting the Items by a attribute in the datagrids only takes a second. With the other approach it tooks ages.</p> <p>I'm not satisfied with the solution, do you have any suggestions? I would apprecciate any kind of criticism (I know both are not good solutions).</p> <p>Thanks,</p> <p>Thomas</p> <p><strong>EDIT</strong></p> <p>To make the first question more clear an small example:</p> <p>Items: Item1,Item2,Item3,... Attributes: Width, Height, Speed, ... AttributeValues: (Item1, Width, 100), (Item1,Height,200), (Item2,Width,100), (Item3, Height, 200), (Item3,Speed,40)</p> <p>So it's a classic many-to-many relationship. A attribute might appear in 0-many Items and and Item might have 0-many attributes.</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