[英]Repository Pattern where method takes too much RAM and loads unnecessary data
I have an image class.我有一个图像类。
public int Id{get; set;}
public string ImageName{get; set;}
public string CreatedDate{get; set;}
public string ModifiedDate{get; set;}
public string Url{get; set;}
......... Many more properties
}
I have 100,000 Images in db.我在数据库中有 100,000 张图像。 When i use repository Pattern to get images it takes to much memory.当我使用存储库模式获取图像时,它需要大量内存。
imageRepostery.GetAll().Where(x=> x.Id == 1).ToList();
This line gets all images from database, creates objects, and stores to RAM, then apply where condition and give me 1 image out of 100,000, then again destroy other object 99,999 objects.这一行从数据库中获取所有图像,创建对象并存储到 RAM,然后应用 where 条件并给我 100,000 个图像中的 1 个图像,然后再次销毁其他对象 99,999 个对象。
How can i use where condition with repository Pattern without getting all data from database.如何在不从数据库中获取所有数据的情况下将 where 条件与存储库模式一起使用。
Edit: And i want to use where condition Like this.编辑:我想像这样使用 where 条件。 List<MyImage> images = imageRepostery.GetAll().Where(x=> x.Id > 500 || x.Id < 1000 ).ToList();
Without implementation of your repository class, it will be all guesses..如果没有实现您的存储库类,这将是所有猜测..
Probably your generic implementation of IRepository.GetAll()
returns List<T>
instead of IEnumerable<T>
and basically you are retrieving all your entries every time.可能您的IRepository.GetAll()
的通用实现返回List<T>
而不是IEnumerable<T>
并且基本上您每次都在检索所有条目。
If you like to have single result by Id better refactor your repository and have method like T GetById(int Id)
and in its implementation return .SingleOrDefault(x=> x.Id == Id)
如果您希望通过 Id 获得单个结果,则最好重构您的存储库并使用T GetById(int Id)
类的方法,并在其实现中返回.SingleOrDefault(x=> x.Id == Id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.