Querying Mapping Engine At Runtime

21 views
Skip to first unread message

David McNee

unread,
Apr 20, 2015, 9:26:14 AM4/20/15
to automapp...@googlegroups.com
Hi Guys,
            could anyone give me a little direction with my current issue as i feel this has to be a solved problem but am struggling to find how to do it.

Essentially, I have a web API project which exposes a series of DTO classes and these are mapped to EF entities in the data layer. The data layer contains a repository object which exposes the DTO's, so the service layer knows nothing about the underlying entities in the data layer. I have automapper configured with mappings from entity => dto and can either map entities manually in the repository or use the .Project() extension to convert these as required. So far, so good.

My issue comes from the fact that I now want to add querying and filtering to the controller in a general, re-usable way. My requirements are similar to a simplified set of the OData spec - I want to include pagination, ordering and standard filters of [propertyName][=/</>][FieldValue]. I have created a library which can apply string filters to in-memory collections, however I am struggling to translate the expression to the underlying entity fields. Ultimately, I want to take a query string intended for the DTO collection (using its property names) and translate this to a query / expression tree which I can throw at EF (using the entity property names). I have everything in place, except translating the property names and values.

For Example:

//Entity class created by EF
public class FooEntity
{
     public string FooValue { get; set;}
}

//DTO class exposed by the controller
public class FooDto
{
     public string SomeValue { get; set;}
}

if, using the above classes, I map FooDTO.SomeValue to FooEntity.FooValue, a query string which can be applied uses the FooDTO field names such as '?SomeValue=Something&Order=SomeValue desc'.

Is there any way to query the automapper engine at run-time to translate these values and property names so the expression can be executed against an collection of the underlying entities? Initially, I had thought it was just a case of swapping out the property names but I would like this to handle more complex mapping expression so I guess I need to translate the value as well. 

Sorry for the somewhat rambling quertion, if I need to clarify or expand on anything here, please just let me know. 

Thanks =)


David McNee

unread,
Apr 20, 2015, 10:06:19 AM4/20/15
to automapp...@googlegroups.com
Hmm, i may have found a way forward with this post: https://groups.google.com/forum/#!topic/automapper-users/DrOqCjmG2Gs

I'll post back if I have additional questions. Thanks 
Reply all
Reply to author
Forward
0 new messages