通用存储库模式:尝试将EntityFramework实现为存储库

时间:2014-01-06 20:00:35

标签: c# .net entity-framework generics repository-pattern

我正在使用实体框架6,CA_OIG是SQL Server DB中的一个表,并尝试在ExclusionListSQLRepository类中构建实现IRepository的存储库,然后尝试在单元测试中实例化ExclusionListSQLRepository。

我有以下IRepository(编译没有错误),

namespace N
{
    public interface IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>, new()
        where TKey : struct
    {
        IEnumerable<TEntity> GetAll();
        TEntity Get(TKey key);

        IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> filter);
        //IQueryable<TEntity> Query { get; }

        void Add(TEntity entity);
        void Add(IEnumerable<TEntity> entities);

        void Delete(TEntity entity);
        void Delete(IEnumerable<TEntity> entities);

        void Update(TEntity entity);
        void Update(IEnumerable<TEntity> entities);
    }

    public interface IEntity<TKey> 
        where TKey : struct
    {
        TKey Id { get; set; }
    }
}

以下是IRepository的实现(没有错误编译),

namespace N
{
    public class ExclusionListSQLRepository<TEntity, TKey> : IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>, new()
        where TKey : struct
    {
        #region Members

        private readonly DbContext _context;

        #endregion

        #region Ctor

        public ExclusionListSQLRepository(DbContext entityContext)
        {
            _context = entityContext;
        }

        #endregion

        public IEnumerable<TEntity> GetAll()
        {
            return _context.Set<TEntity>().Select(row => row);
        }

        public TEntity Get(TKey key)
        {
            return _context.Set<TEntity>().Find(key);
        }

        public IQueryable<TEntity> Query(System.Linq.Expressions.Expression<Func<TEntity, bool>> filter)
        {
            throw new NotImplementedException();
        }

        public void Add(TEntity entity)
        {
            throw new NotImplementedException();
        }

        public void Add(IEnumerable<TEntity> entities)
        {
            throw new NotImplementedException();
        }

        public void Delete(TEntity entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(IEnumerable<TEntity> entities)
        {
            throw new NotImplementedException();
        }

        public void Update(TEntity entity)
        {
            throw new NotImplementedException();
        }

        public void Update(IEnumerable<TEntity> entities)
        {
            throw new NotImplementedException();
        }
    }
}

以下是我的单元测试,

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        DbContext _context = new ExclusionListEntities();

        var a = new ExclusionListSQLRepository<CA_OIG, int>(_context); // compiler error here when trying to use CA_OIG
        var x = a.Get(1);
    }
}

在编译时执行单元测试时出现错误,

  

类型'N.CA_OIG'不能用作类型参数'TEntity'   泛型类型或方法'N.ExclusionListSQLRepository'。   没有从'N.CA_OIG'到...的隐式引用转换   'N.IEntity'。

感谢任何帮助,谢谢!


在@Wiktor Zychla的帮助下

以下是界面,(删除IEntity所以我不必实现它)

namespace N
{
    public interface IRepository<TEntity, TKey>
        where TEntity : class, new()
    {
        IEnumerable<TEntity> Get();
        TEntity Get(TKey key);

        IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> filter);
        //IQueryable<TEntity> Query { get; }

        void Add(TEntity entity);
        void Add(IEnumerable<TEntity> entities);

        void Delete(TEntity entity);
        void Delete(IEnumerable<TEntity> entities);

        void Update(TEntity entity);
        void Update(IEnumerable<TEntity> entities);
    }
}

并对其他提及的课程进行了必要的修改,这些都是一种魅力!

0 个答案:

没有答案