Note that there are some explanatory texts on larger screens.

plurals
  1. POC# syntax for declaring a variable of an abstract generic type
    primarykey
    data
    text
    <p>I have a class defined as follows;</p> <pre><code>public abstract class Repository&lt;TEntity, TDataContext&gt; : DisposableBaseClass where TEntity : class where TDataContext : DataContext, new() {...contains Linq to SQL related functionality </code></pre> <p>In the concrete sub-class I define the types as so;</p> <pre><code>public class ConcreteRepo : Repository&lt;LSTableClass, LSDataContext&gt; </code></pre> <p>At the next tier up, I have business objects which hold the Repository object as a private variable.</p> <p>This was fine;</p> <pre><code>private ConcreteRepo _repository; </code></pre> <p>However, I then refactored this out into a parent class for all the business objects - this parent class holds the repository/ implements Dispose pattern to dispose of repository etc.</p> <p>My problem is that I just can't get the syntax right for the declaration of the variable.</p> <p>The closest I have come is;</p> <pre><code>protected Repository&lt;Object, DataContext&gt; _repository; </code></pre> <p>but this gives the compile error:</p> <p><strong>"Error 1 'System.Data.Linq.DataContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TDataContext' in the generic type or method '....Repository'..."</strong></p> <p>I've tried various other things but hit other problems.</p> <p>In the business layer object inheriting this abstract class I am creating and using the _repository variable with a cast;</p> <pre><code>(Repository&lt;LSTableClass, LSDataContext&gt;)_repository = new ConcreteRepo(); </code></pre> <ul> <li>and I think this will be fine, assuming I can get this declaration right in the parent.</li> </ul> <p>If I can't get this to work I have to declare the _repository in each business object, with the full/ concrete type details, and implement the dispose pattern in each one to clear up. Not the end of the world, but I'd like to not have to.</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.
 

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