Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created November 27, 2009 02:26
Show Gist options
  • Save jfromaniello/243787 to your computer and use it in GitHub Desktop.
Save jfromaniello/243787 to your computer and use it in GitHub Desktop.
public class DaoStub<T> : IDao<T>
{
public DaoStub()
{
Repository = new List<T>();
Mock = new Mock<IDao<T>>();
}
protected List<T> Repository { get; private set; }
public DaoStub<T> PushEntity(T entity)
{
Repository.Add(entity);
return this;
}
public DaoStub<T> PushEntities(IEnumerable<T> entities)
{
Repository.AddRange(entities);
return this;
}
public DaoStub<T> PushEntities(params T[] entities)
{
Repository.AddRange(entities);
return this;
}
public static DaoStub<T> New()
{
return new DaoStub<T>();
}
#region IDao<T> Members
public T Get(object id)
{
return Repository.FirstOrDefault(t => t.To<BaseEntity>().Id == id.To<int>());
}
public T GetProxy(object id)
{
return Repository.First(t => t.To<BaseEntity>().Id == id.To<int>());
}
public void Refresh(T entity)
{
Mock.Object.Refresh(entity);
}
public virtual T MakePersistent(T entity)
{
return Mock.Object.MakePersistent(entity);
}
public virtual void MakeTransient(T entity)
{
Mock.Object.MakeTransient(entity);
}
public virtual IEnumerable<T> RetrieveAll()
{
return Repository;
}
public IEnumerable<T> RetrieveAll(params string[] properties)
{
return RetrieveAll();
}
public virtual IEnumerable<T> Retrieve(Expression<Func<T, bool>> predicate)
{
return Repository.Where(predicate.Compile());
}
public IEnumerable<T> Retrieve(Expression<Func<T, bool>> predicate, params string[] properties)
{
return Repository.Where(predicate.Compile());
}
public virtual int Count(Expression<Func<T, bool>> predicate)
{
return Repository.Count(predicate.Compile());
}
public virtual Mock<IDao<T>> Mock { get; private set; }
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment