This project has moved. For the latest updates, please go here.

Need Help Creating Mock Repositority

Aug 18, 2013 at 10:20 PM
Hello,
Ive created a Mock Repositority posted below - I'm having trouble implementing IQueriable - could you please provide some help on how I can get the Mock Reporstority to impliment IQueriable.

Thanks

using MongoDB.Driver;
using MongoRepository;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace LearnMongoDB.Tests
{
public class MockMongoRepositority<T> : IRepository<T> where T:IEntity
{
    public MockMongoRepositority(List<T> baseCollection)
    {
        this._collection = baseCollection;
    }

    private List<T> _collection { get; set; }
    public MongoCollection<T> Collection { get; private set; }


    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public IEnumerator<T> GetEnumerator()
    {
        return this._collection.GetEnumerator();
    }

    public Expression Expression { get; private set; }
    public Type ElementType { get; private set; }
    public IQueryProvider Provider {
        get { return this._collection.AsQueryable().Provider;}
    }
    public T GetById(string id)
    {
        return this._collection.First(x => x.Id == id);
    }

    public T GetSingle(Expression<Func<T, bool>> criteria)
    {
        return this._collection.AsQueryable().FirstOrDefault(criteria);
    }

    public T Add(T entity)
    {
        entity.Id = Guid.NewGuid().ToString();

        this._collection.Add(entity);
        return entity;
    }

    public void Add(IEnumerable<T> entities)
    {
        foreach (var entity in entities)
        {
            Add(entity);
        }
    }

    public T Update(T entity)
    {
        var i = this._collection.Select(x=>x.Id).ToList().IndexOf(entity.Id);
        this._collection.ToArray()[i] = entity;
        return entity;
    }

    public void Update(IEnumerable<T> entities)
    {
        foreach (var entity in entities)
        {
            Update(entity);
        }
    }

    public void Delete(string id)
    {
        var i = this._collection.Select(x => x.Id).ToList().IndexOf(id);
        this._collection.RemoveAt(i);
    }

    public void Delete(T entity)
    {
        var i = this._collection.Select(x => x.Id).ToList().IndexOf(entity.Id);
        this._collection.RemoveAt(i);
    }

    public void Delete(Expression<Func<T, bool>> criteria)
    {
        var entitiesToDelete = this._collection.AsQueryable().Where(criteria);
        foreach (var entity in entitiesToDelete)
        {
            Delete(entity);
        }
    }

    public void DeleteAll()
    {
        this._collection = new List<T>();
    }

    public long Count()
    {
        return this._collection.Count();
    }

    public bool Exists(Expression<Func<T, bool>> criteria)
    {
        return this._collection.AsQueryable().Any(criteria);
    }

    public IDisposable RequestStart()
    {
        throw new NotImplementedException();
    }

    public void RequestDone()
    {
        throw new NotImplementedException();
    }


}
}
Coordinator
Aug 19, 2013 at 8:16 AM
It would help if you would explain what your exact problem is?
Aug 19, 2013 at 4:03 PM
I need to know how to implement :
public Expression Expression { get; private set; }
public Type ElementType { get; private set; }
public IQueryProvider Provider {
    get { return this._collection.AsQueryable().Provider;}
}
as the standard IQueriable aspects such as
Repo.Any(x=>x...)
Repo.Where(x=>x...)
Repo.First(x=>x...)

etc...
do not work

Thanks
Coordinator
Aug 19, 2013 at 11:54 PM
Edited Aug 20, 2013 at 12:00 AM
I might be missing the point, but does this help you?
public Type ElementType
{
    get { return typeof(T); }
}

public System.Linq.Expressions.Expression Expression
{
    get { return _collection.AsQueryable<T>().Expression; }
}

public IQueryProvider Provider
{
    get { return _collection.AsQueryable<T>().Provider; }
}
What do you mean when you say "as the standard IQueriable aspects such as ...<snip>... etc... do not work"?
(I assume you have referenced System.Linq in the file where you're trying to use the MockRepository? If you haven't, and that's what you mean by "do not work" then consider this: Linq is, essentially, a crapload of extension methods on IEnumerable/IQueryable and without referencing System.Linq the .Net runtime has no idea what you're trying to do (neither has intellisense by the way, which might also be considered "does not work"))

This should be a complete MockRepository implementation similar to yours.