Skip to content

Instantly share code, notes, and snippets.

@anujb
Created July 30, 2011 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anujb/1115216 to your computer and use it in GitHub Desktop.
Save anujb/1115216 to your computer and use it in GitHub Desktop.
DapperRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Linq.Expressions;
using Dapper;
using Dapper.Contrib.Extensions;
namespace Devnos.Core.DataAccess.Repository
{
public class DapperRepository<T> : IRepository<T> where T : class
{
public IDbConnection db { get; private set; }
public DapperRepository(IDbConnection connection)
{
db = connection;
}
public TIdentity Add<TIdentity>(T entity)
{
return (TIdentity)Convert.ChangeType(db.Insert(entity), typeof(TIdentity));
}
public IEnumerable<TIdentity> Add<TIdentity>(IEnumerable<T> entities)
{
foreach (T item in entities)
{
yield return (TIdentity)Convert.ChangeType(db.Insert(item), typeof(TIdentity));
}
}
public bool Update(T entity)
{
return db.Update<T>(entity);
}
public bool Delete(T entity)
{
return db.Delete<T>(entity);
}
public bool Delete(IEnumerable<T> entities)
{
foreach (T item in entities)
{
db.Delete<T>(item);
}
return true;
}
public IQueryable<T> All()
{
return db.Query<T>(string.Format("select * from {0}", typeof(T).Name)).AsQueryable<T>();
}
public T FindBy(int id)
{
throw new NotImplementedException();
}
public T FindBy(int id, string name = "id")
{
return db.Query<T>(string.Format("select * from {0} where {1} = @id", typeof(T).Name, id)).FirstOrDefault();
}
public T FindBy(Expression<Func<T, bool>> expression)
{
throw new NotImplementedException();
}
public IQueryable<T> FilterBy(Expression<Func<T, bool>> expression)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment