1

I am relatively new to Entity Framework. Below is the code that I'm using for generic CRUD functionality.

internal class clsRepository<T>  where T : EntityObject
{

private ObjectContext objCtxt;              
private ObjectSet<T> objSet;

public clsRepository()
{
    objCtxt = new DAL.Entities();
    objSet = objCtxt.CreateObjectSet<T>();            
}

public clsRepository(ObjectContext context)
{
    objCtxt = context;
    objSet = objCtxt.CreateObjectSet<T>();            
}

public T AddEntity(T entity)
{
    objSet.AddObject(entity);
    objCtxt.SaveChanges();
    return entity;
}
public void UpdateEntity(T entity)
{
    object ent;
    if (!objCtxt.TryGetObjectByKey(entity.EntityKey, out ent))
    {
        objSet.Attach(entity);
        objCtxt.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
    }
    else
    {
        objCtxt.ApplyCurrentValues<T>(entity.EntityKey.EntitySetName, entity);
    }            
    objCtxt.SaveChanges();
}
public void DeleteEntity(T entity)
{
    object ent;
    if (!objCtxt.TryGetObjectByKey(entity.EntityKey, out ent))
    {
        objSet.Attach(entity);
        objCtxt.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Deleted);   
    }                      
    objSet.DeleteObject(entity);
    objCtxt.SaveChanges();            
}        
}

1) Is this an efficient way of working with entity model?

2) objCtxt.CreateObjectSet<T>(): I assume this line of code loads all the entities in the db to the object context.Will this lead to any performance issue?

Can anyone suggest a better method? Thank you.

w.brian
  • 16,296
  • 14
  • 69
  • 118
sjj
  • 29
  • 8

1 Answers1

2

Your second question first. objCtxt.CreateObjectSet<T>(); does not load all entities from the database, it just creates an ObjectSet. This is an IQueryable so it will load entities when you start querying it.

Now the first question. Once you decide to use generic repositories this is a way to do it. Some remarks:

  • entity.EntityKey can be null. So you have to check for that too.
  • You have a parameterless constructor in which an ObjectContext is created. I would always create the context outside the repository (or inject it through an IoC container), because in jobs that involve multiple repositories you want to have one context instance and you certainly don't want to mix repos with a shared context and ones with their own context.
  • that also means that you better remove the SaveChanges() calls. The pattern would be:

    using (var ctx = new DAL.Entities();
    {
         var repo1 = new clsRepository<A>(ctx);
         .... 
         var repo2 = new clsRepository<B>(ctx);
         .... 
         if (// no error )
             ctx.SaveChanges();
    }
    

I said "once you decide to use generic repositories" for a reason. This is an often cited post that will most certainly give you second thoughts.

Your repository methods might just as well be extension methods on ObjectSet: you do exactly the same thing, but you just work with contexts without having to create all these repositories all the time.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291