views:

1738

answers:

2

I'm considering one of two IRepository interfaces, one that is a descendant of IQueryable and one that contains IQueryable.

Like this:

public interface IRepository<T> : IQueryable<T>
{
    T Save(T entity);
    void Delete(T entity);
}

Or this:

public interface IRepository<T>
{
    T Save(T entity);
    void Delete(T entity);
    IQueryable<T> Query();
}

LINQ usage would be:

from dos
in ServiceLocator.Current.GetInstance<IRepository<DomainObject>>()
where dos.Id == id
select dos

Or...

from dos
in ServiceLocator.Current.GetInstance<IRepository<DomainObject>>().Query
where dos.Id == id
select dos

I kinda like the first one, but it's problematic to mock. How have other people implemented LINQable, mockable repositories?

+8  A: 

Depends on if you want a Has-A or an Is-A relationship.

The first one is an Is-A relationship. The IRepository interface is a IQueryable interface. The second is a has-a. The IRepository has an IQueryable interface. In the process of writing this, I actually like the second better then the first, simply because when use your second IRepository, I can give the Query() method ANYTHING that returns IQueryable. To me, that is more flexible then the first implementation.

MagicKat
This is what I ended up doing.. Mocking Example 1 is just too painful, with Moq anyway.
Andy S
Related question, I'm using Moq to mock a repository like Example 2, but my query contains mapping logic in the "select new { ... }" that never gets tested because execution is deferred until IRepository.Save(), which is mocked and never performs the mapping. Any ideas?
flipdoubt
A: 

You could always quick write stuff against List, it's not mocking using a mock framework, but it sure works great.

Kim Johansson