Skip to content

0013: Problems with repositories



Today I have to make it quick 🙂
I’ve created DbManager class that consists of field called currentRepository. I want this member to store repository that user works with at the moment. This could be BookRepository, ComicRepository and so on.
Of course for now, when I’m writing only Books functionality this isn’t necessary and I would simple write this:

private BookRepository currentRepository;

But when another repos will be added, this won’t work anymore. What to do? First, I figured out that I could write chain of interfeces, something like this:

IBookRepository : IRepository<Books>
BookRepository : IBookRepository

Then add new interface IRepoItem that Books, Comics, Stamps will be implemented. And next add field in DbManager:

private IRepository<IRepoItem> currentRepo;

This will be great, I thought naive. After this:

IRepository<IRepoItem> currentRepo = new BookRepository();

VisualStudio gave me nasty error:
Cannot implicitly convert type ‘BookRepository’ to ‘IRepository’. An explicit conversion exists (are you missing a cast?)

I’ve read some time ago about covariance in .Net 4.0 so I suspected that this is related with this thing. As I couldn’t figure it out by myself, I asked a question on stackoverflow. Smart guys answered and gave beautiful solution:

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; 

I did what it states, but then I realized that THIS WON’T WORK ANYWAY!!! 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 😦 I don’t know how to overrun this problem, so I will try to get answers from smarter than me – then, be sure, I will post it here.

Another, not related with above, thing is that I implemented configuration files handling and logger. I work on VS2008 with CodeRush Express due to its great capabilities to reduce automatic, boring work like adding properties and other. Maybe I will introduce this tool next time.

Best regards,

  1. Traffic Mayhem Review- the articles on this article is really a single of the most effective substance that I?ve actually are available throughout. I really like your article, I will are available again to verify for new posts.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: