This project has moved and is read-only. For the latest updates, please go here.

Automapper for converting DTO to Business

Feb 9, 2015 at 11:34 AM

first of all congratulations on Remote.Linq. This is something I needed for ages. I also managed to implement it with success.

Unfortunately passing back the DTOs from e.g. EF is not very handy. So my idea was to convert the expression to business and the resulting DTOs back to business.
        public IEnumerable<DynamicObject> ExecuteQuery(Expression queryExpression)
            var efContext = new EFContext();
            // Map Expresson from Business to DTO
            var result = queryExpression.Execute(queryableProvider: type => efContext.Set(type));
            // Map Back
            return result;
I went with something like
IEnumerable<DynamicObject> result = queryExpression.Execute(queryableProvider: type => Mapper.CreateMap<OrderItemBusiness, OrderItem>().ConstructUsing((Func<OrderItemBusiness, OrderItem>)(c => efContext.Set(type))));
Any help or hint would be greatly appreciated!

Feb 14, 2015 at 9:37 PM
When it comes to converting the types returned by EF you're not only required to match the expected structure of the result but also make sure the client's query may be applied. The query expression passed from client to server is targeting a well know source structure.

Applying a query to an ORM like EF means translating the query into SQL. Unfortunately conversions done by Automapper cannot be translated into SQL but may only happen once the query has been executed and the resulting records are loaded into memory.

Therefore I would suggest you pick one of two alternatives which are:
  • Type conversion using type resolver (if the structure of the types match)
  • Projection of the source queryables to match the client types
There are two sample applications demonstrating the ideas: RemoteQueryable_CustomTypeResolver and RemoteQueryable_ServerDefinedProjection

Please make sure to upgrade to the lates version (4.0.0-beta002)