views:

310

answers:

1

I am an intermediate user of EF in .net 3.5 and have recently moved to working with .net 4.

One thing i keep coming across when reading various tutorials is the use of ObjectSets instead of ObjectQuerys and that they are a great new feature.

What is so great about them?

Please enlighten me.

Kind regards, Kohan

+2  A: 

Not sure if that's what you mean in your question, but what's really cool about ObjectSet is that it implements an interface IObjectSet which means you can fake it very easily and test the code down until the data access layer.

What's even cooler, is that since the ObjectSet use generic types (IObjectSet< T >), you can have a generic repository and implement the Unit Of Work pattern.

public interface IRepository<T> where T : class
{
     IQueryable<T> GetQuery();
     IEnumerable<T> GetAll();
     IEnumerable<T> Find(Func<T, bool> where);
     T Single(Func<T, bool> where);
     T First(Func<T, bool> where);

     void Delete(T entity);
     void Add(T entity);
     void Attach(T entity);
     void SaveChanges();
}

complete article here :

http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/

Stephane
Okay that makes sence since i was looking at tutorials on making a generic repository when i saw this "hype". Cheers.
Kohan
Just out of interest and i will make a new question if needed but whats the difference between adding a Product through an objectquery, objectset, or on the objectcontext like i have shown above?
Kohan
I actually don't know the answer to that question :-/My first guess is that it has something to do with self-tracking entities vs Persistence agnostic entities when using POCO. But not so sure about that, sorry.
Stephane
No probs. Thanks for the help i have added a new question for this and edited the question here for clarity sake. http://stackoverflow.com/questions/2810988/3-methods-for-adding-a-product-through-entity-framework-whats-the-difference
Kohan