I am writing a data access layer in c# to find stock items in a database. I have a method like getStockItemByStockCode(string stockCode) which is easy enough, but how do you go about writing a method for finding a stockItem based on a combination of properties?
For example if I pass a StockFilter object to the method and set the properties that I want to filter on, how can I create an Entity Framework query to do this without it becoming convoluted? Or should I just get all the records and filter them in the Business Layer which does not sound right to me because that means I would be bringing back data that I don't need.
This is the method I am using to get my stock item by stock code:
public StockDTO getStockItemByStockCode(string stockCode)
{
StockDTO stockItem = null;
using (var db = new DbContext())
{
var aStock = (from stock in db.STK_MASTER
where stock.STOCK_CODE.Equals(stockCode)
select stock).Single();
Mapper.CreateMap<STK_MASTER, StockDTO>();
StockDTO stockDto = Mapper.Map<STK_MASTER, StockDTO>(aStock);
stockItem = stockDto;
}
return stockItem;
}
This is the method header that I would like to use to get my stock item based on a combination of properties:
public StockDTO getStockItem(StockFilter stockProperties)
Is there a pattern or something that I can use for this? The stock item properties might include things like the last sale date, quantity sold, price range etc.