我有这个通用的存储库。为什么我不能使这个通用的知识库工作?什么不对?
/// <summary>
/// Implémentation de base d'un dépositoire pour Entity Framework.
/// </summary>
/// <remarks>Entity Framework 4.1</remarks>
/// <typeparam name="TEntite">Entité spécifique.</typeparam>
public abstract class RepositoryBase<TEntity, TKey> : IRepository<TEntity>, IDisposable
where TEntity : EntityBase<TKey>
where TKey : class
{
private readonly IContext _context;
private ObjectContext _objectContext;
private IObjectSet<TEntity> _objectSet;
protected RepositoryBase(IContext context)
{
_context = context;
_objectContext = _context.GetObjectContext();
_objectSet = _objectContext.CreateObjectSet<TEntity>();
}
/// <see cref="IRepository.cs"/>
public IEnumerable<TEntity> GetAll(Expression<Func<TEntity, object>> sortExpression)
{
if (sortExpression == null)
sortExpression = x => x.Id;
return _objectSet.OrderBy(sortExpression).AsEnumerable();
}
/// <see cref="IRepository.cs"/>
public IEnumerable<TEntity> GetAll(int maximumRows, int startRowIndex, Expression<Func<TEntity, object>> sortExpression)
{
if (sortExpression == null)
sortExpression = x => x.Id;
return _objectSet.OrderBy(sortExpression).Skip(startRowIndex).Take(maximumRows).AsEnumerable();
}
/// <see cref="IRepository.cs"/>
public TEntity SelectByKey(TKey key)
{
if (key == null)
throw new ArgumentNullException("La clé était NULL! Une clé est nécessaire pour récupérer un entité.");
return _objectSet.SingleOrDefault(x => x.Id == key);
}
这里的具体实施...
public class ProductRepository : RepositoryBase<Product, int>, IProductRepository { public ProductRepository(IContext context) : base(context) { }
/// <see cref="IProductRepository.cs"/>
public Product GetByName(string name)
{
return base.First(x => x.Name == name);
}
/// <see cref="IProductRepository.cs"/>
public IEnumerable<Product> FindProduct(Specification<Product> specification)
{
throw new System.NotImplementedException();
}
}
当我创建特定的资源库..它说,TKEY必须是引用类型,但是为什么呢?有没有办法使这个通用的知识库工作?使用TKey是为了使方法SelectByKey接受键类型。
编辑#1:
如果我删除了约束的话,我有一个问题...... TKEY的不能使用== TKEY的在我的SelectByKey方法lambda表达式进行比较。
编辑#2:
尝试使用Equals和语法似乎是好。
编辑#3:
在运行时的Equals碰撞..说TKEY(这似乎是一个System.Object)不能使用,其似乎不逻辑由于对象具有相等的方法Equals。我目前没有获得真正的代码,但我做了下面这段代码的一些测试..
class Program
{
static void Main(string[] args)
{
Test<TestEntity, int> t = new Test<TestEntity, int>();
t.TestMethod(5);
}
}
class Test<TEntity, TKey>
where TEntity : Entity<TKey>
{
public Test()
{ }
public TestEntity TestMethod(TKey id)
{
List<TestEntity> testEntity = new List<TestEntity>();
testEntity.Add(new TestEntity(5));
return testEntity.SingleOrDefault(x => x.Id.Equals(id));
}
}
class Entity<TKey>
{
public TKey Id { get; set; }
}
class TestEntity : Entity
{
public TestEntity(int id)
{
Id = id;
}
}
class Entity : Entity<int>
{
}
,它似乎工作得很好。所以我会在今晚稍后尝试。
编辑#4
好吧例外我得到的是Canoot创造型的恒定值 “System.Object的”。此上下文仅支持主要类型,如int32,string和guid。
然后我得到一个错误,说TKey不能有操作== – Rushino 2012-01-12 00:37:46
通过使用Equals来解决这个问题。谢谢! – Rushino 2012-01-12 01:08:54
umm nope does not work。所以相反,我删除了SelectByKey方法..不幸的。 – Rushino 2012-01-12 02:08:58