locked
Generic Repository for Entity Framework RRS feed

  • Question

  • User-666539819 posted

    How to build common generic repository for entity framework in asp.net c# web application.

    Wednesday, June 12, 2013 5:23 AM

Answers

  • User-666539819 posted

    generic repository for entity framework in asp.net c# web application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2013 5:37 AM
  • User1482461450 posted
    public interface IRepository<TEntity> : IDisposable where TEntity : class
    {
            IQueryable<TEntity> GetAll { get; }
            IEntity GetById(int id) { get; }
    }
    
    
    
    
    
    public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        private IContext context;
        private IObjectSet<TEntity> objectSet;
    
    
    
    private IObjectSet<TEntity> ObjectSet
    {
            get
            {
                if (this.objectSet == null)
                {
                    var entitySetProperty = this.Context.GetType().GetProperties().Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>).MakeGenericType(typeof(TEntity)).IsAssignableFrom(p.PropertyType));
    
                    this.objectSet = (IObjectSet<TEntity>)entitySetProperty.GetValue(this.Context, null);
                }
    
                return this.objectSet;
            }
    }
    
        public IQueryable<TEntity> GetAll
            {
                get
                {
                    return this.ObjectSet;
                }
            }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2013 11:40 AM
  • User-666539819 posted

    Can anybody post full details about Generic Repository for Entity Framework

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 19, 2013 6:17 AM

All replies

  • User-666539819 posted

    If anybody can post detail repository class for the same

    Wednesday, June 12, 2013 5:24 AM
  • User-666539819 posted

    does generic repository will break the application core logic

    Wednesday, June 12, 2013 5:24 AM
  • User-666539819 posted

    ok

    Wednesday, June 12, 2013 5:27 AM
  • User-666539819 posted

    generic repository for entity framework in asp.net c# web application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2013 5:37 AM
  • User1482461450 posted
    public interface IRepository<TEntity> : IDisposable where TEntity : class
    {
            IQueryable<TEntity> GetAll { get; }
            IEntity GetById(int id) { get; }
    }
    
    
    
    
    
    public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        private IContext context;
        private IObjectSet<TEntity> objectSet;
    
    
    
    private IObjectSet<TEntity> ObjectSet
    {
            get
            {
                if (this.objectSet == null)
                {
                    var entitySetProperty = this.Context.GetType().GetProperties().Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>).MakeGenericType(typeof(TEntity)).IsAssignableFrom(p.PropertyType));
    
                    this.objectSet = (IObjectSet<TEntity>)entitySetProperty.GetValue(this.Context, null);
                }
    
                return this.objectSet;
            }
    }
    
        public IQueryable<TEntity> GetAll
            {
                get
                {
                    return this.ObjectSet;
                }
            }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2013 11:40 AM
  • User-666539819 posted

    Can anybody post full details about Generic Repository for Entity Framework

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 19, 2013 6:17 AM