Conversion of Linq expressions from business to domain types? RRS feed

  • Question

  • Hello,

    I am trying to figure out how to convert a Linq Expression between two types: One in my Business Repository and its related one in the Domain Repository. Both types have the same data properties that will be used in the queries. In my business layer, I have the following method that will perform a query based on a business entity:

    BUSINESS REPOSITORY: Domain entities converted to business entities

    public IQueryable<CustomerVendorProduct> Query(Expression<Func<CustomerVendorProduct,bool>> filterInBusinessRepo)

    So, a consumer could do something like the following:

    var list = BusinessRepository.Query(c => c.CustomerId == 77);

    They would get a list of CustomerVendorProduct business objects with CustomerId == 77.

    Within the Query() method of the Business Repository, I need to invoke the following method in the Domain Repository. It will retrieve the raw, encrypted domain entities from the database. Note that its query requires the entity type used in the Domain Repository. It is related to our business entity, but simply is involved in CRUD operations.

    DOMAIN REPOSITORY: Raw, encrypted entities from the database

    public IQueryable<CustomerVendorProductEntity> Query(Expression<Func<CustomerVendorProductEntity, bool>> filter)

    So, the Query() method in the Business Repository would do something like the following:

    var domainList = DomainRepository.Query(filterInBusinessRepo);

    The result would be a list of CustomerVendorProductEntity objects. The business repository then translates them into CustomerVendorBusiness entities and returns them to the caller.

    So, my question is how can I convert the Linq Expression from the Business Repository Entity Type: CustomerVendorProduct to the Domain Repository Entity Type: CustomerVendorProductEntity? I think that I must create a conversion method... Something like the following... But, I am lost as to how to effect the conversion. I would really appreciate your help:

         // Convert the Linq Expression from our Business Layer to our Domain Layer.
        private Expression<Func<CustomerVendorProductEntity, bool>> CVPToCVPE(Expression<Func<CustomerVendorProduct, bool>> filter)
            var parms = System.Linq.Expressions.Expression.Parameter(typeof(CustomerVendorProductEntity), "destination");

            var result = System.Linq.Expressions.Expression.Lambda<Func<CustomerVendorProductEntity, bool>>(filter, parms);

            return result;

    Thank you for your time and suggestions!


    Thursday, November 1, 2012 6:20 PM


All replies

  • Wow, no replies?  I would have thought that a lot of other developers have encountered this issue, especially within an MVC application...  Still hoping for help...
    Friday, November 2, 2012 6:07 PM
    • Marked as answer by A Bit of Help Monday, November 5, 2012 5:26 AM
    Monday, November 5, 2012 5:09 AM
  • Hi Dorado999,

    Thank you for the link... It was helpful and confirmed my suspicions...  At the bottom of the posting, there is a good summary of the options by antlersoft.  My solution was to remove the filter from the Business layer, so our Controller Actions have a limited signature for retrieving data.  However, in our Domain layer, I implemented the filter so we have the power to filter and retrieve less data from the database for processing.  The alternative, for us, would be to walk the expression try and do our own conversion, but, well.. Yuck!  :P

    Thank you for your help!


    Monday, November 5, 2012 5:26 AM