Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to point to generic abstract class?
    text
    copied!<p>I haven't got problems with code since my last problem with interface covariance. Today I took another approach to the OLGAtherer architecture and I found huge (for me) obstacle. I hope that its huge only for me and I will find answer here :) OK, please check this out:</p> <p>I have abstract generic class:</p> <pre><code>abstract class Repository&lt;T&gt; where T : Entity </code></pre> <p>this class contain signatures of four CRUD methods (add, insert, etc.) and two protected fields. Any repository that I will create must inherit from this superclass. I have repositories like, for example:</p> <pre><code>class BookRepository : Repository&lt;Book&gt; </code></pre> <p>Now I have class DB that is responsible for operations on DB file. And there is my problem. Look at this method placed in DB class:</p> <pre><code> void Add(List&lt;Entity&gt; enitites, Type entityType) { Repository&lt;entityType&gt; repo = EntitiesFactory(entityType); repo.Add(entities); } </code></pre> <p>I know that above method won't work, but this visualize my problem (hopefully) - I would like to dynamically create repository of the specified type, but I have no idea how to do this. This has been always my main OOP problem - I could code inheritance (or implementation of interfaces) but I could not use these inherited classes. Please clarify this to me. Thank you in advance.</p> <p>Paweł</p> <hr> <p>I used hints that you provided and this is what I have now:</p> <pre><code>public void Add&lt;T&gt;(List&lt;T&gt; entities, string repoName) where T : Entity { Repository&lt;T&gt; repo = RepoFactory&lt;T&gt;(repoName, typeof(T)); repo.Add(entities); } private Repository&lt;T&gt; RepoFactory&lt;T&gt;(string repoName, Type t) where T : Entity { if (t == typeof(Book)) { Repository&lt;T&gt; repo = (Repository&lt;T&gt;)((object)(new BookRepository(this.ConnectionString, repoName))); } return null; </code></pre> <p>}</p> <p>RepoFactory is now in DB class (which is, by the way, some sort of Bridge here) - it should be moved elsewhere probably, but it's not a main problem here. </p> <p>First, I don't know whether RepoFactory body is implemented in the proper way - I guess not. </p> <p>Second - I assume that I will call DB methods from other class (let's say - WindowsForm, for simplicity). I will call Add with parameter T dependent on user's choice. This is situation that I have problem and fix it temporarily, because now DB is OK, but if I would have to implement WindowsForm code, I will encounter it another time (I think) - in the future I will try to figure out answer to the same question, but on the higher level (above DB) - I hope you understand me... I basically mean, that I will not know how to dynamically call Add method, because T will be dependent on user's choice (OLGAtherer has to be Repository Manager that would support Books and other kind of collections). And if user will try to work with his book collection, I will have to call Add() method. If with Comics collection, Add(). I don't want to write class above for each kind of collection, but rather use one call of Add to do this.</p> <p>If you understand what I mean, you're really good :) My English sucks, but I'm working on it. Thank you for previous answers and thanks in advance for further ones. Paweł</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