Morshed Anwar’s article “Implementing Repository Pattern with Entity Framework”
starts off with this at the top of his repository:
public class Repository<E, C> : IRepository<E, C>, IDisposable
where E : EntityObject
where C : ObjectContext
{ ... }
Say you want to use ASP.NET POCO Entity Generator, you could change EntityObject to class. However, in order to get all the properties of the EntityObject, you would need to use reflection to determine that the class does in fact have all the properties and methods of an EntityObject.
Is there a better constraint other than class we could substitute for EntityObject using ASP.NET POCO Entity Generator to expose the EntityObject properties and methods normally available using where E : EntityObject?
First of all, that Generic Repository implementation is ugly (CodeProject, in general, has been a disappointing source for good examples in my opinion). There’s no two ways around it.
When using POCO Objects, there’s really no reason to need to reflect. Take advantage of the
EntityContext.CreateObjectSet<T>method and let Entity Framework worry about the details.If you want to take a look at a much cleaner Generic Repository implementation, check out:
Elegant Code >> Entity Framework POCO (EF4): Generic Repository and Unit of Work