0

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.

Asagohan
  • 583
  • 5
  • 19
  • 1
    Mind to use `Expression>` ? – Fendy May 30 '13 at 09:31
  • I suppose you would like to use StockDTO as query parameter to match items in the data source. **First question**: are you sure EVERY StockDTO's property has a "null value" you can use to mark it (="don't care about this property value in the search")? **Second question**: can you consider to use a fake QueryStockDTO class to use it as _query object_ for StockDTO? You may even use a micromapper to exchange properties between them. – Adriano Repetti May 30 '13 at 09:35
  • It doesn't have to be a DTO object at all actually. I am actually thinking that it may not be a good idea to use the DTO now that I think about it. Yes I could use some sort of Filter object which is not a DTO and is specifically for querying. I changed my question to use a StockFilter object instead of StockDTO – Asagohan May 30 '13 at 09:43
  • So how would my query look if I had a filter object with potential properties such as stockFilter.stockCode, stockFilter.maxPrice, stockFilter.minPrice, stockFilter.lastSaleDate? But any of those could be null. – Asagohan May 30 '13 at 09:50

1 Answers1

0

I found this - Creating dynamic queries with entity framework.

I believe this would solve my problem for finding a stock item. However I can see my method becoming huge if I have to check every single property that I might want to filter on and have to do this for each entities DAL.

If there is a better way, please let me know.

Expression<Func<T, bool>> 

is the Generic Repository Pattern right? Would this be better than the solution in the given link in terms of best practice and code decoupling/maintainability?

Community
  • 1
  • 1
Asagohan
  • 583
  • 5
  • 19
  • Just a question: if you use a raw expression then you "expose" the query method to the client and this may not be the best to let optimize the query itself by LINQ. – Adriano Repetti May 30 '13 at 11:46