Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a Repository Implementation using ObjectContext vs DbContext on EF 4.1

What could be the better implementation for STE, I heard about that DbContext is the simplest way to implement a Repo with EF, personally I take advantage of the EntityState, but there is any member on ObjectContext that could deliver more functionallity for my CRUD operations using Repo? at today I'm using a GenericRepository like this one :

public class GenericRepository<TEntity> where TEntity : class
    {
        internal DbContext context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(DbContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }

        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);
        }

        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }

        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }

        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }

        public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
        {
            return dbSet.SqlQuery(query, parameters).ToList();
        }
    }

I forgot to mention that also I'm using Unity, so the calls to Repository are like this way :

[Dependency]
        public IUnityContainer Container { get; set; }

        public List<Case> GetAll()
        {
            using (var context = Container.Resolve<ClaimEntities>())
            {
                var qry = (from c in context.Cases
                           select c).ToList();
                return qry;
            }
        }
like image 246
Jonathan Escobedo Avatar asked May 11 '11 20:05

Jonathan Escobedo


People also ask

Which interface you have to implement to get the reference of ObjectContext from DbContext?

Interface implemented by objects that can provide an ObjectContext instance. The DbContext class implements this interface to provide access to the underlying ObjectContext.

What is DbContext and DbSet in Entity Framework?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table.

What is the use of entity context object?

You can use a DbContext associated to a model to: Write and execute queries. Materialize query results as entity objects. Track changes that are made to those objects.


1 Answers

Self tracking entities are feature of ObjectContext - they are not supported in DbContext. If you want STEs you need to swap to ObjectContext API and use STEs T4 template to generate entities instead of your current POCOs.

like image 107
Ladislav Mrnka Avatar answered Oct 21 '22 22:10

Ladislav Mrnka