.net 为实体框架应用程序构建通用存储库

.net 为实体框架应用程序构建通用存储库,.net,entity-framework,repository,.net,Entity Framework,Repository,我正在尝试为基于实体框架的应用程序编写一个通用存储库。以下是我的原型代码: 接口 public interface IDomainRepository { T GetById<T>(int id, Expression<Action<T>> idx) } 我现在可以这样称呼它: var g = repository.GetById<tbl_Client>(23, c => c.cl_id); var g=r

我正在尝试为基于实体框架的应用程序编写一个通用存储库。以下是我的原型代码:

接口

public interface IDomainRepository
    {
        T GetById<T>(int id, Expression<Action<T>> idx)
    }
我现在可以这样称呼它:

var g = repository.GetById<tbl_Client>(23, c => c.cl_id);
var g=repository.GetById(23,c=>c.cl\u id);
但我不知道如何使用idx并根据传递的id检查其值:

 public T GetById<T>(int id, Expression<Func<T, object>> idx)
        {
            //var col = idx.Compile().Invoke(T);
           // How do I check if the column passed to "idx" is equal to id?

            return default(T);
        }
public T GetById(int-id,表达式idx)
{
//var col=idx.Compile().Invoke(T);
//如何检查传递给“idx”的列是否等于id?
返回默认值(T);
}
编辑: 我想我现在开始工作了。以下是我的全部代码和测试:

public interface IDomainRepository
    {
        T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class;

        IEnumerable<T> GetAll<T>() where T : class;
        IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class;
        IEnumerable<T> Query<T>(ISpecification<T> filter) where T : class;

        void Add<T>(T entity) where T : class;
        void Delete<T>(T entity) where T : class;
        Table<T> GetTable<T>() where T : class;
    }

public class DomainRepository : IDomainRepository
    {
        private readonly DatabaseDataContext _ctx;

        public DomainRepository(DatabaseDataContext ctx)
        {
            _ctx = ctx;
        }

        public T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class
        {
            return (from i in GetAll<T>()
                    let h = idx.Compile().Invoke(i)
                    where Convert.ToInt32(h) == id
                    select i).SingleOrDefault();
        }

        public IEnumerable<T> GetAll<T>() where T : class
        {
            return GetTable<T>().ToList();
        }

        public IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class
        {
            return GetTable<T>().Where(filter);
        }

        public IEnumerable<T> Query<T> (ISpecification<T> filter) where T : class
        {
            return GetTable<T>().Where(filter.Predicate);
        }

        public void Add<T> (T entity) where T : class
        {
            GetTable<T>().InsertOnSubmit(entity);
        }

        public void Delete<T> (T entity) where T : class
        {
            GetTable<T>().DeleteOnSubmit(entity);
        }

        public Table<T> GetTable<T>() where T : class
        {
            return _ctx.GetTable(typeof(T)) as Table<T>;
        }
    }


var repository = new DomainRepository(_ctx);

var g = repository.GetById<tbl_Client>(1, c => c.cl_id);
公共接口存储库
{
T GetById(int-id,表达式idx),其中T:class;
IEnumerable GetAll(),其中T:class;
IEnumerable查询(表达式过滤器),其中T:class;
IEnumerable查询(ISpecification筛选器),其中T:class;
无效添加(T实体),其中T:类别;
作废删除(T实体),其中T:类别;
表GetTable(),其中T:class;
}
公共类DomainRepository:IDomainRepository
{
专用只读数据库DatabaseDataContext\u ctx;
公共域存储库(DatabaseDataContext ctx)
{
_ctx=ctx;
}
公共T GetById(int-id,表达式idx),其中T:class
{
返回(来自GetAll()中的i)
设h=idx.Compile().Invoke(i)
其中Convert.ToInt32(h)=id
选择i).SingleOrDefault();
}
public IEnumerable GetAll(),其中T:class
{
返回GetTable().ToList();
}
公共IEnumerable查询(表达式筛选器),其中T:class
{
返回GetTable(),其中(filter);
}
公共IEnumerable查询(ISpecification筛选器),其中T:class
{
返回GetTable().Where(filter.Predicate);
}
公共无效添加(T实体),其中T:class
{
GetTable().InsertOnSubmit(实体);
}
公共作废删除(T实体),其中T:class
{
GetTable().DeleteOnSubmit(实体);
}
公共表GetTable(),其中T:class
{
将_ctx.GetTable(typeof(T))作为表返回;
}
}
var repository=newdomainrepository(_ctx);
var g=repository.GetById(1,c=>c.cl\u id);
我会继续测试,看看是否正常

干杯。
Jas。

您可以通过层将IQueryable暴露出来

IQueryable用户{get;}

因此,您可以在ui代码中执行以下操作:

其中(c=>c.Username==“foo”)


BLLContext将有一个公开IQueryable的基础DAL上下文。

您可以通过层公开IQueryable

IQueryable用户{get;}

因此,您可以在ui代码中执行以下操作:

其中(c=>c.Username==“foo”)


BLLContext将有一个公开IQueryable的基础DAL上下文。

好的,我想我已经有了最终版本,不过需要进行更多的测试:

public T GetById<T>(int id, Func<T, int> idx) where T : class
        {
            return (from i in GetAll<T>()
                    where idx(i) == id
                    select i).SingleOrDefault();
        }
public T GetById(int-id,Func-idx),其中T:class
{
返回(来自GetAll()中的i)
其中idx(i)=id
选择i).SingleOrDefault();
}

好的,我想我已经有了最终版本,不过还需要一些测试:

public T GetById<T>(int id, Func<T, int> idx) where T : class
        {
            return (from i in GetAll<T>()
                    where idx(i) == id
                    select i).SingleOrDefault();
        }
public T GetById(int-id,Func-idx),其中T:class
{
返回(来自GetAll()中的i)
其中idx(i)=id
选择i).SingleOrDefault();
}

谢谢您的建议,但我希望存储库是通用的,因此它无法具体了解“用户”。无论如何,干杯。谢谢你的建议,但我希望存储库是通用的,因此它不能以具体的方式了解“用户”。干杯。请检查此方法生成的SQL。据我所知,这可能会导致实现整个表并将Linq应用于对象。请检查由此方法生成的SQL。据我所知,这可能导致实现整个表并将Linq应用于对象。
public interface IDomainRepository
    {
        T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class;

        IEnumerable<T> GetAll<T>() where T : class;
        IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class;
        IEnumerable<T> Query<T>(ISpecification<T> filter) where T : class;

        void Add<T>(T entity) where T : class;
        void Delete<T>(T entity) where T : class;
        Table<T> GetTable<T>() where T : class;
    }

public class DomainRepository : IDomainRepository
    {
        private readonly DatabaseDataContext _ctx;

        public DomainRepository(DatabaseDataContext ctx)
        {
            _ctx = ctx;
        }

        public T GetById<T>(int id, Expression<Func<T, object>> idx) where T : class
        {
            return (from i in GetAll<T>()
                    let h = idx.Compile().Invoke(i)
                    where Convert.ToInt32(h) == id
                    select i).SingleOrDefault();
        }

        public IEnumerable<T> GetAll<T>() where T : class
        {
            return GetTable<T>().ToList();
        }

        public IEnumerable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class
        {
            return GetTable<T>().Where(filter);
        }

        public IEnumerable<T> Query<T> (ISpecification<T> filter) where T : class
        {
            return GetTable<T>().Where(filter.Predicate);
        }

        public void Add<T> (T entity) where T : class
        {
            GetTable<T>().InsertOnSubmit(entity);
        }

        public void Delete<T> (T entity) where T : class
        {
            GetTable<T>().DeleteOnSubmit(entity);
        }

        public Table<T> GetTable<T>() where T : class
        {
            return _ctx.GetTable(typeof(T)) as Table<T>;
        }
    }


var repository = new DomainRepository(_ctx);

var g = repository.GetById<tbl_Client>(1, c => c.cl_id);
public T GetById<T>(int id, Func<T, int> idx) where T : class
        {
            return (from i in GetAll<T>()
                    where idx(i) == id
                    select i).SingleOrDefault();
        }