none
How to implement a specific repository pattern RRS feed

  • Question

  • public interface IRepository<TEntity> : IDisposable
                where TEntity : class        
        {
            bool IsDisposed { get; }
        
            TEntity Find(params object[] keyValues);
    
            TEntity Find(Expression<Func<TEntity, bool>> filter, 
                         Expression<Func<TEntity, object>>[] include = null);
        
            Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> filter,
                                    Expression<Func<TEntity, object>>[] include = null);
        
            IQueryable<TEntity> Query(Expression<Func<TEntity, object>>[] include = null);
        
            TEntity Create();
        
            TEntity Create(TEntity value);        
        
            bool Update(TEntity value);
        
            void Delete(TEntity value);
        
            void Delete(IEnumerable<TEntity>entities);
        
            void Reset(TEntity value);
        
            Task ResetAsync(TEntity value);
          
        }
    
    
    public interface IUnitOfWork : IDisposable
        {
     
            IRepository<TEntity> GetRepository<TEntity>()
                where TEntity : class;
                      
            int SaveChanges();
            
            Task<int> SaveChangesAsync();
            
            IEnumerable<TEntity> Invoke<TEntity>(string procedure, IDictionary<string, object> parameters);
            
            IEnumerable<TEntity> Invoke<TEntity>(string procedure, object parameters);
            
            int Invoke(string procedure, IDictionary<string, object> parameters);
            
            int Invoke(string procedure, object parameters);
        }

    i would like to use EF6 with repository pattern and UoW.

    Wednesday, July 12, 2017 2:35 PM

Answers

  • Hi Mou_kolkata,

    >>How to implement a specific repository pattern

    The following code for your reference.

    public class ContactsRepository : IRepository<Contact>
    {
        private SampleDbContext entities = new SampleDbContext();
     
        public Contact Add(Contact entity)
        {
            entities.Contacts.Add(entity);
        }
      
        public void Delete(Contact entity)
        {
            entities.Contacts.Remove(entity);
        }
        //Other method.
        
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, July 17, 2017 12:13 PM
    Thursday, July 13, 2017 6:08 AM
    Moderator

All replies

  • Hi Mou_kolkata,

    >>How to implement a specific repository pattern

    The following code for your reference.

    public class ContactsRepository : IRepository<Contact>
    {
        private SampleDbContext entities = new SampleDbContext();
     
        public Contact Add(Contact entity)
        {
            entities.Contacts.Add(entity);
        }
      
        public void Delete(Contact entity)
        {
            entities.Contacts.Remove(entity);
        }
        //Other method.
        
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, July 17, 2017 12:13 PM
    Thursday, July 13, 2017 6:08 AM
    Moderator
  • you did not show how to use unit of work pattern with repository pattern.

    would you share a sample code just to show how to use it.

    Monday, July 17, 2017 12:14 PM