Note that there are some explanatory texts on larger screens.

plurals
  1. POExtend DataTable in C#
    text
    copied!<p>A static constructor for class <code>SourceManager</code> goes through all modules/classes and discovers all classes that implement <code>ISource</code>. It will instantiate each one of these and expose an <code>IEnumerable</code> of them as a static property called <code>IEnumerable&lt;ISource&gt; Sources</code>. For simplicity <code>ISource</code> has two properties, <code>DataTable Table { get; }</code> and <code>string UniqueName { get; }</code>. When instantiated each different <code>ISource</code> is responsible for populating its <code>Table</code> from SQL, MDX, etc. For all the <code>ISource</code>'s I've written thus far, loading the <code>Table</code> with all <code>DataRow</code>s when instantiated has been sufficient. However <strong>I now have a situation where I'd like to load the <code>Table</code> with <code>DataRow</code>'s lazily, rather than all up front. How do I do this?</strong> I will walk through an example. </p> <p><code>PermissionSource</code> implements <code>ISource</code>. Its <code>Table</code> property, that has a <code>private set</code> is given a value of <code>new PermissionDataTable()</code>. Its <code>UniqueName</code> is <code>"Permissions"</code>. As of right now there are no permissions loaded from the database into this <code>Table</code> property.</p> <pre><code>ISource permissionSource = SourceManager.Sources. Where(s =&gt; "Permission".Equals(s.UniqueName)).First(); </code></pre> <p>Now we've obtained the <code>PermissionSource</code>, but through an interface. Let's get a permission.</p> <pre><code>DataRow row = permissionSource.Table.Rows.Cast&lt;DataRow&gt;(). Where(r =&gt; r["PermissionName"].Equals("PermissionName")).First() </code></pre> <p>I've overriden the <code>Rows</code> property in <code>PermissionDataTable</code> so that the above, somehow, gets the value of the permissions associated with <code>"PermissionName"</code> in the database. Other permissions are not loaded.</p> <p>I don't have a choice in the permissions system and I don't have a choice to not use a <code>DataTable</code>.</p> <p><strong>EDIT:</strong></p> <p>In my example I would need to override the <code>Rows</code> property of <code>DataTable</code>. <code>Rows</code>, though, is a <a href="http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.aspx" rel="noreferrer">DataRowCollection</a> which is <code>sealed</code>. Thus there isn't much that can be done in terms of creating a minimal custom DataTable implementation like I want to do.</p>
 

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