zoukankan      html  css  js  c++  java
  • Generic Entity Framework 4.0 Base Repository

    代码
    public interface IRepository<T> where T : class
    {
        IQueryable
    <T> GetQuery();

        IEnumerable
    <T> GetAll();

        IEnumerable
    <T> Find(Expression<Func<T, bool>> where);

        T Single(Expression
    <Func<T, bool>> where);

        
    void Delete(T entity);

        
    void Add(T entity);
    }


    public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
       
    readonly IObjectContext _objectContext = null;
       
    readonly IObjectSet<TEntity> _objectSet = null;

       
    public BaseRepository(IObjectContext objectContext)
       {
           
    if (objectContext == null)
              
    throw new ArgumentNullException("objectContext");

           _objectContext 
    = objectContext;
           _objectSet 
    = _objectContext.CreateObjectSet<TEntity>();
       }

       
    public IQueryable<TEntity> GetQuery()
       {
           
    return _objectSet;
       }

       
    public IEnumerable<TEntity> GetAll()
       {
          
    return _objectSet.ToList();
       }

       
    public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> where)
       {
          
    return _objectSet.Where(where);
       }

       
    public TEntity Single(Expression<Func<TEntity, bool>> where)
       {
          
    return _objectSet.SingleOrDefault(where);
       }

       
    public void Delete(TEntity entity)
       {
          _objectSet.DeleteObject(entity);
       }

       
    public void Add(TEntity entity)
       {
          _objectSet.AddObject(entity);
       }
    }

  • 相关阅读:
    多语言资源文件制作工具
    window service 插件服务插件开发
    .net 中读取自定义Config文件
    Asp.net 主题中CSS文件的缓存问题
    Asp .net 4.0 中ViewStatus 使用
    Linq通用分页数据查询方法
    EF中查询出现死锁的处理
    Windows Live Writer 分享到插件
    Windows Resx资源文件编辑工具
    插件式服务架构
  • 原文地址:https://www.cnblogs.com/jes_shaw/p/1813460.html
Copyright © 2011-2022 走看看