views:

187

answers:

4

Hello!

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

+4  A: 

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.

Jon Skeet
+1 - I thought about this non-generic `IRepository` interface, but wasn't sure about how to bring it. Excellent Jon! =)
Will Marcouiller
Hi again! I have repositories in the way described here: http://olgatherer.wordpress.com/2010/08/18/repositoriesrus/ And I created DbManager to manage database and create repositories for each of tables (books, comics, poststamps, so on). Now I would like that currentRepository in DbManager could be BookRepository, StampsRepository, whatever - I thought that this thing with interfaces would be OK, but unfortunately isn't, because IRepoItem (as I realized today) should have all different properties then... :(
skrzeczowas
A: 

In .NET 3.5 there isn't any relation between IRepository and IRepository in .NET 4, you could achieve something like this with covariance and contravariance support (but it depends on the IRepository interface declaration)

A would recommand using a non generic IRepository interface and doing the cast yourself. I know, it's not wonderful, but the fonctionality you describe requires covariance/contravariance support.

Maupertuis
Thank you for this answer, I used this one below and realized that this doesn't fully suits me :( I don't know what to do now... IRepoItem couldn't have all properties that books, comics, stamps consits of :(
skrzeczowas
+1  A: 

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;
Grozz
Hi! This is great and very elegant, I'm very gladful that you posted this. Unfortunately, as I described in the comment to above post, I realized that this doesn't fully suits me. IRepoItem is an interface that would be implemented in Books, Stamps, Comics, so then it shuld have all properties for different items. This isn't smart and I have to find another way to achieve what I want - that means that I want currentRepo to be BookRepository or StampsRepository or whatever other repository... Maybe some help, please?
skrzeczowas
IRepoItem being an interface for other stuff should only result in it consolidating the things they have in common, not "all properties". For example IMoveable should only expose the Move method, not every thing that all classes derived (like Cheetah, Car, Airplane) from it have.
Grozz
But then, if I, let say, will have currentRepo = StampsRepo();, I will be able to use only methods that IRepository consists of. I won't be able to use IStampsRepository methods :( I believe that I can then ask what type this currentRepo is: if(currentRepo is IBookRepository)do something else if(currentRepo is IStampsRepository) do something ... But this isn't good object programming, I think. I have to reconsider all assumptions I made, because maybe what I want is stupid and not achieveable...
skrzeczowas
Good luck refactoring if that's the case ;)
Grozz
A: 

Hi again,

Thank you all for answers. I exactly described my another problem here: http://olgatherer.wordpress.com/2010/10/07/0013-problems-with-repositories/

In short, I can’t put in IRepoItem all properties that Comics, Books, Stamps and other things have. On the other hand, if I won’t put them, I won’t be able to use them in DbManager - and I don't know how to overrun this. Maybe you could help me?

Thank you in advance, skrzeczowas

skrzeczowas