I have this generic repository.
/// <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);
}
Here the specific implementation...
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();
}
}
When i create a specific repository.. it say that Tkey must be a reference type but why ? Is there a way to make this generic repository work ? TKey was used in order to make the method SelectByKey accept a key type.
Edit #1:
If i remove the constrain then i have another problem... TKey cannot be compared with TKey using == as lambda expression in my SelectByKey method.
Edit #2:
Tried to use Equals and the syntax seem to be ok.
Edit #3:
Equals crash at runtime.. saying Tkey (which seem to be a System.Object) can't use equals which doesnt seem logic since object have the equal method. I currently doesn't have access to the real code but i did some test with this code below..
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>
{
}
And it seem to work pretty well. So i will try later tonight.
Edit #4
Alright the exception i get is Canoot create a constant value of type "System.Object". Only primary types such int32, string and guid are supported by this context.