locked
How can I impliment , IQueryable to return the list of records, Please help me with suggested code RRS feed

  • Question

  • User-1355965324 posted

    I have the following model

    string MeterNo 
    long  Reading
    DateTime ReadDate
    

    I am using  the following Interface repository class

    namespace MyApp.GO.WebAPI.Repository
    {
        public interface IDepotAssetRepository
        {
            IEnumerable<SelectListItem> GetMeterCode(int DepotNo);
            MeterReadingModel CreateMeterReading(MeterReadingModel readingmodel);
        }
    }

    My Repository class is 

    namespace MyApp.GO.WebAPI.Repository
    {
        public class DepotAssetRepository : IDepotAssetRepository
        {
            private readonly GoContext ctx;
    
            public DepotAssetRepository(GoContext db)
            {
                ctx = db;
            }
            public IEnumerable<SelectListItem> GetMeterCode(int DepotNo)   // For Dropdown
            {
                IEnumerable<SelectListItem> MeterCodes = (from a in ctx.goMeter
                                                          where a.DepotNo == DepotNo
                                                          select new SelectListItem
                                                             {
                                                                 Value = a.MeterCode,
                                                                 Text = a.MeterCode
                                                             }).ToList();
                return MeterCodes;
            }
    
            public MeterReadingModel CreateMeterReading(MeterReadingModel readingmodel)
            {
                ctx.goMeterReading.Add(readingmodel);
                ctx.SaveChanges();
                return readingmodel;
            }
    
    // Here I wanto impilment a new method to  get list of record between two dates like given below and I want to customise it  to get the meter reading between two dates
    
     public virtual IEnumerable<TEntity> GetMeterReading(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties)
            {
                IQueryable<TEntity> query = dbSet;
    
                if (filter != null)
                {
                    query = query.Where(filter);
                }
    
                foreach (var includeProperty in includeProperties)
                {
                    query = query.Include(includeProperty);
                }
    
                if (orderBy != null)
                {
                    return orderBy(query).ToList();
                }
                else
                {
                    return query.ToList();
                }
            }
    
    
    
    
    
        }
    }
    

    From My controller class I would like to call the method

    namespace MyApp.GO.UI.Controllers
    {
        public class DepotAssetsController : Controller
        {
    
            
    
            private readonly IHostingEnvironment hostingEnvironment;
            private readonly IDepotAssetRepository _readingRepo;
            
            
    
            public DepotAssetsController(IHostingEnvironment hostingEnvironment,
               IDepotAssetRepository readingRepo )
            {
                
                this.hostingEnvironment = hostingEnvironment;
                _readingRepo = readingRepo;
               
                
            }
    
             public IActionResult MeterReading(MeterReadingModel readingmodel)
            {
                    List<ReadingModel> Readlist = new List<ReadingModel>();
                    Readlist = _readingRepo.GetMeterReading(pass the filter variable here for the where condition using two dates)
            }

    I am new in repository concept so I am asking stupid question please forgive.

    Thursday, November 7, 2019 10:05 PM

Answers

  • User303363814 posted

    I would suggest that you look at the reason for adding the extra repository layer of code.  What does it do for you?  Without a repository getting in the way then the code would be

    var readings = ctx.goMeterReading.Where(mr => mr.ReadDate >= startDate && mr.ReadDate <= endDate);

    Pretty simple and easy to understand.

    The entity framework context object implements a unit of work pattern.  It also abstracts away the underlying data provider.  This is all that repository does.  Entity Framework gives it to you for free.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2019 1:39 AM
  • User711641945 posted

    Hi polachan,

    Could you share your models?

    Here is a simple demo about how to implement a Generic Repository:

    1.Model:

    public class MeterReadingModel
    {
        public DateTime DateFrom { get; set; }
        public DateTime DateTo { get; set; }
    }
    public class ReadingModel
    {
        [Key]
        public string MeterNo { get; set; }
        public long Reading { get; set; }
        public DateTime ReadDate { get; set; }
    }

    2.IDepotAssetRepository:

    public interface IDepotAssetRepository<TEntity>
    {
            IEnumerable<TEntity> GetMeterReading(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties);
    }

    3.DepotAssetRepository:

    public class DepotAssetRepository<TEntity> : IDepotAssetRepository<TEntity> where TEntity : class
    {
        private readonly MVCSO1101Context ctx;
        internal DbSet<TEntity> dbSet;
    
        public DepotAssetRepository(MVCSO1101Context db)
        {
            ctx = db;
            this.dbSet = db.Set<TEntity>();
        }
        // Here I wanto impilment a new method to  get list of record between two dates like given below and I want to customise it  to get the meter reading between two dates
        public IEnumerable<TEntity> GetMeterReading(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties)
        {
            IQueryable<TEntity> query = dbSet;
    
            if (filter != null)
            {
                query = query.Where(filter);
            }
    
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
    
            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }
    
    }

    4.Controller:

    public class GoUserDepotLinksController : Controller
    {
        private readonly IDepotAssetRepository<ReadingModel> _readingRepo;
    
        public GoUserDepotLinksController(IDepotAssetRepository<ReadingModel> readingRepo)
        {
            _readingRepo = readingRepo;
         }
    
        // GET: GoUserDepotLinks
        public async Task<IActionResult> Index(MeterReadingModel readingmodel)
        {
               //for easy testing,i manually set the value to readingmodel
                readingmodel = new MeterReadingModel() { 
                DateFrom= Convert.ToDateTime("2019-11-10"),
                DateTo= Convert.ToDateTime("2019-11-13")
            };
                List<ReadingModel> Readlist = new List<ReadingModel>();
                Readlist = _readingRepo.GetMeterReading(filter: r => r.ReadDate >= readingmodel.DateFrom && r.ReadDate <= readingmodel.DateTo).ToList();
    
            return View();
        }

    5.Startup.cs:

    services.AddScoped<IDepotAssetRepository<ReadingModel>, DepotAssetRepository<ReadingModel>>();
    

    6.Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 11, 2019 9:37 AM

All replies

  • User303363814 posted

    I would suggest that you look at the reason for adding the extra repository layer of code.  What does it do for you?  Without a repository getting in the way then the code would be

    var readings = ctx.goMeterReading.Where(mr => mr.ReadDate >= startDate && mr.ReadDate <= endDate);

    Pretty simple and easy to understand.

    The entity framework context object implements a unit of work pattern.  It also abstracts away the underlying data provider.  This is all that repository does.  Entity Framework gives it to you for free.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2019 1:39 AM
  • User711641945 posted

    Hi polachan,

    Could you share your models?

    Here is a simple demo about how to implement a Generic Repository:

    1.Model:

    public class MeterReadingModel
    {
        public DateTime DateFrom { get; set; }
        public DateTime DateTo { get; set; }
    }
    public class ReadingModel
    {
        [Key]
        public string MeterNo { get; set; }
        public long Reading { get; set; }
        public DateTime ReadDate { get; set; }
    }

    2.IDepotAssetRepository:

    public interface IDepotAssetRepository<TEntity>
    {
            IEnumerable<TEntity> GetMeterReading(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties);
    }

    3.DepotAssetRepository:

    public class DepotAssetRepository<TEntity> : IDepotAssetRepository<TEntity> where TEntity : class
    {
        private readonly MVCSO1101Context ctx;
        internal DbSet<TEntity> dbSet;
    
        public DepotAssetRepository(MVCSO1101Context db)
        {
            ctx = db;
            this.dbSet = db.Set<TEntity>();
        }
        // Here I wanto impilment a new method to  get list of record between two dates like given below and I want to customise it  to get the meter reading between two dates
        public IEnumerable<TEntity> GetMeterReading(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties)
        {
            IQueryable<TEntity> query = dbSet;
    
            if (filter != null)
            {
                query = query.Where(filter);
            }
    
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
    
            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }
    
    }

    4.Controller:

    public class GoUserDepotLinksController : Controller
    {
        private readonly IDepotAssetRepository<ReadingModel> _readingRepo;
    
        public GoUserDepotLinksController(IDepotAssetRepository<ReadingModel> readingRepo)
        {
            _readingRepo = readingRepo;
         }
    
        // GET: GoUserDepotLinks
        public async Task<IActionResult> Index(MeterReadingModel readingmodel)
        {
               //for easy testing,i manually set the value to readingmodel
                readingmodel = new MeterReadingModel() { 
                DateFrom= Convert.ToDateTime("2019-11-10"),
                DateTo= Convert.ToDateTime("2019-11-13")
            };
                List<ReadingModel> Readlist = new List<ReadingModel>();
                Readlist = _readingRepo.GetMeterReading(filter: r => r.ReadDate >= readingmodel.DateFrom && r.ReadDate <= readingmodel.DateTo).ToList();
    
            return View();
        }

    5.Startup.cs:

    services.AddScoped<IDepotAssetRepository<ReadingModel>, DepotAssetRepository<ReadingModel>>();
    

    6.Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 11, 2019 9:37 AM