I'm trying to overrun quite big (for me) problem that I came across while writing my application. Look at this, please (I will try to shorten the code for simplicity):
I have root interface called IRepository<T>
.
Next, IBookRepository : IRepository<Book>
Next, concrete class that implements it: BookRepository : IBookRepository
In the RepositoryManager class I declared private IRepository<IRepoItem> currentRepo;
IRepoItem
is an interface that is implemented by Book class.
Now, when I try to do something like this:
currentRepo = new BookRepository();
VisualStudio gives error message:
Cannot implicitly convert type 'BookRepository' to 'IRepository'. An explicit conversion exists (are you missing a cast?)
I tried to cast explicitly, but runtime exception is thrown...
I know (almost for sure) that it is something called covariance and this is (probably) solved in .Net 4.0. Unfortunately I'm writing using framework version 3.5, and I cannot change this. Please give me some advices what to do - how to overrun this problem? I'd like to get currentRepo from RepoFactory that would produce few kinds of repositories depends on user needs. I don't know whether links are allowed here, but I write some kind of blog on http://olgatherer.wordpress.com/ where I describe application creation. My English isn't good, but I hope that it's enough to understand me. Thank you in advance for answer.
Best regards, skrzeczowas