Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Generic Inheritance Issue with Base Library
    primarykey
    data
    text
    <p>Given the following classes/interfaces defined in a base library:</p> <pre><code>public interface IWorkContext { T Node&lt;T&gt;() where T : class, IHierachy&lt;T&gt;; IHierachyItem Node(); } public interface IHierachyItem { int Id { get; set; } string Title { get; set; } } public interface IHierachy&lt;T&gt; : IHierachyItem where T : IHierachy&lt;T&gt; { T Parent { get; set; } IList&lt;T&gt; Children { get; set; } } public class WorkContext { public static IWorkContext Current { get { return DependencyResolver.Current.GetService&lt;IWorkContext&gt;(); } } } </code></pre> <p>Which has the following implentation inside another library (that references the base library above):</p> <pre><code>public class DefaultWorkContext : IWorkContext { // Nhibernate session private readonly ISession _session; public DefaultWorkContext(ISession session) { _session = session; } public T Node&lt;T&gt;() where T : class, IHierachy&lt;T&gt; { return _session.Get&lt;T&gt;(2); } public IHierachyItem Node() { return Node&lt;SiteMapNode&gt;(); } } </code></pre> <p>Where SiteMapNode exists in the same library (and is mapped to a database table):</p> <pre><code>public class SiteMapNode : IHierachy&lt;SiteMapNode&gt; { public virtual int Id { get; set; } public virtual SiteMapNode Parent { get; set; } public virtual string Title { get; set; } public virtual IList&lt;SiteMapNode&gt; Children { get; set; } public SiteMapNode() { Children = new List&lt;SiteMapNode&gt;(); } } </code></pre> <p>I can say the following to access a node and get the parent:</p> <pre><code>var node = WorkContext.Current.Node(); var parentNode = ((IHierachy&lt;SiteMapNode&gt;)node).Parent; var node2 = WorkContext.Current.Node&lt;SiteMapNode&gt;(); var parentNode2 = node2.Parent; </code></pre> <p>However i don't like either approach as option 1 requires a case and option 2 requires me to pass a default type.</p> <p>Is it possible to refactor this sample so that i can access the Parent and Child the same way I get the Id and Title.</p> <p>I hope i've explained the problem clear enough. I'd appreciate the help. Thanks</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