Using AutoMapper with F#

前端 未结 2 682
小蘑菇
小蘑菇 2021-02-15 05:31

I\'m trying to use AutoMapper from F#, but I\'m having trouble setting it up due to AutoMapper\'s heavy use of LINQ Expressions.

Specifically, the AutoMapper type

2条回答
  •  旧巷少年郎
    2021-02-15 05:51

    I'm not quite sure how to fix the generated expression tree (that's doable by post-processing it, but it is pain to find out what AutoMapper expects). However, there are two alternatives:

    As a first option - the expressions that you need to translate are fairly simple. They are mostly just method calls, property getters and uses of a variable. This means that it should be possible to write your own quotation to expression trees translator that produces exactly the code you want (then you can also add your own handling of obj, perhaps by calling Expression.Convert to build the expression tree). I wrote a simple quotation tranlsator as a sample, which should handle most of the stuff in your sample.

    As a second option - if AutoMapper provides an option to specify just a property name - you could just use quotations of the form <@ x.FooBar @>. These should be quite easy to deconstruct using the Patterns.PropertyGet pattern. The API should maybe look like this:

    Mapper.CreateMap(fun post summary mapper ->
      mapper |> mapMember <@ post.Slug @> // not sure what the second argument should be?
             |> ignoreMember <@ post.Author @> )
    

    Or, in fact, you could use this style of API even in the first case, because you don't need to write lambda expressions repeatedly for every single mapping, so maybe it is a bit nicer :-)

提交回复
热议问题