简体   繁体   中英

What is the advantage of using ObjectSet

As MSDN suggests we can use the following ObjectContext

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Add the new object to the context.
    context.Products.AddObject(newProduct);
}

However there is also another use of a similiar code with using ObjectSet<T>

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> pSet = context.CreateObjectSet<Product>();
    pSet.AddObject(newProduct);
}

The second paragraph in the article says:

In versions starting with .NET Framework version 4, you can use the following methods defined on ObjectSet instead of the equivalent ones defined on ObjectContext .

Is there a spesific reason to use ObjectSet instead of ObjectContext and how do we know which to use when?

ObjectContext and ObjectSet are legacy EF code, for which DbSet and DbContext have created as wrappers around ObjectContext model to make EF a much better experience.

Underneath DbSet and DbContext , EF is still using ObjectContext / ObjectSet .

Starting with EF 7, they got rid of all base code, and are re writing the whole EF ORM.

EDIT

DbContext = A collection of your entity models, connection to the database, logging, tracking, and glue, and probably a whole bunch of things i have missed. This usuall contains 1 or more DbSets<YourEntity>

DbSet is an object representing a collection of a specific entity. This contains information such as caching, inserting, updating, selecting for only a specific entity.

I like to think of these a

DbContext = Database

DbSet = Table

They are ALOT more than that, but conceptually that is how I visualise them, and don't necessarily map 1:1. Eg an Entity may be a subset of a table or it may even be a combination of multiple tables.

Regarding ObjectSet and ObjectContext I lack experience in how they work internally in order to tell you what the difference is exactly. I know how DbSet/Context works, but I don't know how much of it gets done by ObjectSet/Context and how much is additional.

Maybe an excercise for you to find out in the wild? :-P

There isn't much difference if you use ObjectSet directly.

However by using ObjectContext and OBjectSet together, you can develop reusable generic repository classes (CRUD). The code sample that you provided will only work for retrieving products for that application only, whereas a generic CRUD repository would define methods to Add, Read, Update and Delete which can work with any table (and in other databases).

eg You can define a IRepository interface

public interface IRepository<T> : IDisposable where T : class
{
    void Add(T entity);
    void Delete(T entity);
    void SaveChanges();
    ...
}

And a generic concrete class

public class DataRepository<C,T> : IRepository<T> where T : class where C : ObjectContext, new()
{
    private ObjectContext _context;
    private IObjectSet<T> _objectSet;
    public DataRepository() : this(new C()) { }
    public DataRepository(ObjectContext context)
    {
       _context = context;
       _objectSet = _context.CreateObjectSet<T>();
    }

    public void Add(T entity)
    {
       if(entity == null) throw new ArgumentNullException("entity");
       _objectSet.AddObject(entity);
    }

    public void Delete(Func<T, bool< predicate)
    {
        var records = from x in _objectSet.Where<T>(predicate) select x;
        foreach(T record in records)
           _objectSet.DeleteObject(record);
    }

    public void SaveChanges()
    {
        _context.SaveChanges();
    }

    // Other members

    // IDisposable members
}

The code above you could copy and paste to each application, or put in a separate assembly and reference in each application.

For your example, you would create this class in your application to retrieve products

public class ProductsRepo : DataRepository<AdventureWorksEntities, Product> {
    // You can add other specific methods not covered by the default CRUD methods here

}

And to add a new product

using(var repo = new ProductsRepo())
{
    repo.Add(newProduct);
    repo.SaveChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM