locked
Interface, Generic Repository, IOC. Please, need advice. RRS feed

  • Question

  • Hello,

    I am developing a generic repository for EF4. I am using IOC on this project. 

    For sake of simplicity I am leaving out a few methods. Context is the EF4 model class:

    public partial class Context : ObjectContext { // Generated Code }
    

    The IRepository interface is as follows:

     public interface IRepository<T> : IDisposable where T : class {
      void Add(T entity);
      T Get(Func<T, Boolean> predicate);  
      void SaveChanges();
      void SaveChanges(SaveOptions options);
     } // IRepository
    

    And the generic  Repository class is:

     public class Repository<T> : IRepository<T> where T : class {
    
      private ObjectContext _context;
      private IObjectSet<T> _set;
    
      public Repository()
       : this(new Context()) {
      } // Repository
    
      public Repository(ObjectContext context) {
       _context = context;
       _set = _context.CreateObjectSet<T>();
      } // Repository
    
      public void Add(T entity) {
       if (entity == null)
        throw new ArgumentNullException("entity");
       _set.AddObject(entity);
      } // Add
    
      public T Get(Func<T, Boolean> predicate) {
       return _set.Where(predicate).FirstOrDefault<T>();
      } // Get
    
      public void SaveChanges() {
       _context.SaveChanges();
      } // Save
    
      public void SaveChanges(SaveOptions options) {
       _context.SaveChanges(options);
      } // SaveChanges
    
      public void Dispose() {
       Dispose(true);
       GC.SuppressFinalize(this);
      } // Dispose
    
      protected virtual void Dispose(Boolean disposing) {
       if (disposing) {
        if (_context != null) {
         _context.Dispose();
         _context = null;
        }
       }
      } // Dispose
    
     } // Repository

    The problem/question:

      In Repository class constructor a ObjectContext context is expected. So I pass an instance of EF4 Context class.

      But should I not have an interface as the constructor argument? I think for IOC it would be the objective.

    So I created the following interface (Not sure if I should call it IContext, should I?):

     public interface ISession {  
      void Save();
      void Save(SaveOptions options);
      void Free();
     } // ISession
    

    And then the partial Context class that "extends" the previous Context class (Note that EF4 Context is a partial class):

     public partial class Context : ObjectContext, ISession {
    
      public void Save() {
       base.SaveChanges();
      } // Save
    
      public void Save(SaveOptions options) {
       base.SaveChanges(options);
      } // Save
    
      public void Free() {
       base.Dispose();
      } // Free
    
     } // Context
    

    Does this make sense? Now I think I should have a ISession type as argument of the Repository class parameter but in that case I am not sure how to define the ObjectContext.

    Anyway, I am a little bit lost about this ...

    Could someone, please, advice me on this?

    Note: Any other suggestion to improve this is welcome.

    Thank You,

    Miguel

     

     

     

     

    Monday, August 30, 2010 2:30 PM

Answers