I'm trying to find the best way to work with objects in entity framework. I don't want my forms to know anything about ObjectContext, so I put all the logic inside the entities (I write partial classes). I've been looking for others experiences a lot and haven't find this approach anywhere. So, how do you work? How do you get an object from ObjectContext and work with it, without loosing its entitystates and everything else? I've come to some solution but still wondering for others. Thanks.
this example shows how to abstract your model away from specific implementations like LINQ DataContexts: http://blog.wekeroad.com/mvc-storefront/
I'm currently using this approach and it's not bad.
Hi Following the DDD let's separate your entities from the logic that operates on them. Personally, I have used Repository pattern to create one generic repository as well as some specialized repositories that operate on my entities. The reposotory can operate on constructor-given ObjectContext or will create a new one (from configuration) when none is specified.
My example IRepository interface:
public interface IRepository<T> where T : class
{
/// <summary>
/// Return all instances of type T.
/// </summary>
/// <returns></returns>
IQueryable<T> All();
/// <summary>
/// Return all instances of type T that match the expression exp.
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
IEnumerable<T> Find(Func<T, bool> exp);
/// <summary>Returns the single entity matching the expression.
/// Throws an exception if there is not exactly one such entity.</summary>
/// <param name="exp"></param><returns></returns>
T Single(Func<T, bool> exp);
/// <summary>Returns the first element satisfying the condition.</summary>
/// <param name="exp"></param><returns></returns>
T First(Func<T, bool> exp);
/// <summary>
/// Mark an entity to be deleted when the context is saved.
/// </summary>
/// <param name="entity"></param>
void Delete(T entity);
/// <summary>
/// Create a new instance of type T.
/// </summary>
/// <returns></returns>
T CreateInstance();
/// <summary>Persist the data context.</summary>
void SaveAll();
}
Examples how to put entity framework 4 in n-tier arhitecture in order of complexity:
- http://devtalk.dk/2009/06/09/Entity+Framework+40+Beta+1+POCO+ObjectSet+Repository+And+UnitOfWork.aspx
- http://blog.keithpatton.com/2009/05/30/Entity+Framework+POCO+Repository+Using+Visual+Studio+2010+Net+40+Beta+1.aspx
- http://danielwertheim.files.wordpress.com/2009/12/putting-entity-framework-4-to-use-in-a-business-architecture-v2.pdf
- http://www.simonsegal.net/blog/2010/01/11/entity-framework-repositories-fetching-strategies-specification-and-mapping-using-nfetchspec-for-role-driven-development-parts-1-4
By the way if you'd preffer to implement interface posted by @twk use IEnumerable<T> Find(Expression<Func<T, bool>> exp);
syntax for all query operation. Implementing IEnumerable<T> Find(Func<T, bool> exp);
will result in materializing entire table and in-memory filtering.