views:

63

answers:

2

Hi, I'm trying to get Figure 3 Fake Database from IRepository using the example here http://msdn.microsoft.com/en-us/magazine/dd263069.aspx

public class InMemoryRepository : IRepository
{
    private readonly Cache<Type, object> _types;
    private MockUnitOfWork _lastUnitOfWork;

    public InMemoryRepository()
    {
        _types = new Cache<Type, object>(type =>
        {
            Type listType = typeof(List<>).MakeGenericType(type);
            return Activator.CreateInstance(listType);
        });
    }

    private IList<T> listFor<T>()
    {
        return (IList<T>)_types.Get(typeof(T));
    }

    public T Find<T>(long id) where T : Entity
    {
        return listFor<T>().FirstOrDefault(t => t.Id == id);
    }

    public void Delete<T>(T target)
    {
        listFor<T>().Remove(target);
    }

    public T[] Query<T>(Expression<Func<T, bool>> where)
    {
        var query = from item in listFor<T>() select item;
        return query.Where(where.Compile()).ToArray();
    }

    public void Save<T>(T target)
    {
        listFor<T>().Add(target);
    }
}

I'm getting 'Cannot resolve symbol MockUnitOfWork. I have NUnit/Moq/Rhino.Mock installed/referenced but I cannot find any reference to MockUnitOfWork. Any help appreciated.

A: 

You can just remove the MockUnitOfWork, because it is never used in the code.

I think it is a remnant left over after a refactoring.

Jay
Thanks for spotting that!Unfortunately now the code is moaning about the private readonly Cache<Type, object> _types; saying that cache does not have type parameters :(
Hunt
From what assembly is VS getting `Cache`? I'm pretty sure this is also a type defined by the author. The only `Cache` class with which I am familiar is for ASP.NET. The `Cache<Type,object>` class appears to be delegate like `Func<T1,TResult>()` that takes a type parameter and a `Func<TResult>` and resolves and returns an `object` of the specified type (which must be cast). In this case, the author passes a `Func<TResult>` that will be executed when a user requests a `List<>` and will use the `Activator` to instantiate the collection and then return it.
Jay
I will look for fully documented example code. Thanks.
Hunt
A: 

The article doesn't explicitly say anything about what MockUnitOfWork is, but since it is an explicitly declared type, it must be a hand-rolled Mock.

Despite its semantic equivalence, it has nothing to do with Moq or RhinoMocks.

If you can download the source code for the article, I'm pretty sure you will find a class called MockUnitOfWork in the test project.

Mark Seemann
Unfortunately there is no source code with this example code ;(Thanks for your interest.
Hunt