Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces inheritance in C#

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

like image 949
skrzeczowas Avatar asked Oct 06 '10 13:10

skrzeczowas


2 Answers

In .NET 3.5 you definitely can't treat an IRepository<Book> as an IRepository<IRepoItem>.

We'd need to know more about what you're using the repository for in RepositoryManager to really know how to solve it... but could you create a non-generic IRepository interface which IRepository<T> extends? Make it include all the members which don't refer to T. Then you can declare currentRepo as just an IRepository.

like image 174
Jon Skeet Avatar answered Oct 15 '22 06:10

Jon Skeet


Try using the intermediate layer (IRep):

    interface IRepository<T>
    {
    }

    interface IRep<T> : IRepository<IRepoItem> where T : IRepoItem
    {
    }

    interface IBookRepository : IRep<Book>
    {
    }

    class BookRepository : IBookRepository
    {
    }

then you can do what you want:

        BookRepository br = new BookRepository();
        IRepository<IRepoItem> currentRepo = br;
like image 42
Grozz Avatar answered Oct 15 '22 07:10

Grozz