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);
       }
    }

  • 相关阅读:
    P3899 [湖南集训]谈笑风生
    bzoj3252: 攻略
    批量创建用户20个和密码
    创建100个目录dir1-dir100一键完成
    SVM的优缺点
    Python zip() 函数
    经典博客4
    python的空格和tab混用报错问题
    Python的functools.reduce用法
    matplotlib显示AttributeError: 'module' object has no attribute 'verbose'
  • 原文地址:https://www.cnblogs.com/jes_shaw/p/1813460.html
Copyright © 2011-2022 走看看