views:

140

answers:

2

I set up an IDataContext and when i create a data class for an entity, i inherit it from IDataContext.

IDataContext interface has 4 methods.

IQueryable<T> GetAll();
T GetById(long id);
void Add(T entity);
void Delete(T entity);
void Save(T entity);

As you know Delete and Save methods have this structure;

FooEntities db = new FooEntities();

db.DeleteObject(Foo entity);
// or save changes method
db.SaveChanges();

I meant these two methods could be generalized or something...

My question is how and where do you use these two methods.

  • Inside of the each data class for an entity
  • or another way of using.
A: 

DeleteObject is used to delete an object from the store. Calling DeleteObject as you have above marks the object as deleted. The call to SaveChanges() actually commits that change to the database. Both are necessary to delete an object from the store.

Dave Swersky
Thank you for your answer but this is not what i am asking for.Please take a look at the Aidan's answer
Barbaros Alp
Sorry, misunderstood your question. Aidan is right, the Repository pattern is a good method for wrapping and consolidating EF. Here is another good article on that: http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
Dave Swersky
+1  A: 

You can abstract away the details of the framework with the Repository pattern. Here is an example implementation of IRepository for the Entity Framework. As for where to invoke the actual deleting and saving, your Controller/Presenter is a likely candidate.

Aidan Ryan
Thank you very much, i am going to read it now
Barbaros Alp