c# - AutoMapper converters fail for null values -


Note: This question is now outdated and only applies to older versions of Automaker has gone.


Problem:

I have an automaker converter which is a nullable / bool? and a string . I apply this profile globally to my profile, and it works for true and false , but not for null

I have this in my automaker profile:

  CreateMap & lt; Bool?, String & gt; (). Convert Using & lt; NullableBoolToLabel & gt; ();  

And here is the converter class:

  Public class NullableBoolToLabel: ITypeConverter & lt; Bool?, String & gt; {Public string convert (boole? Source) {if (source.HasValue) {if (source.Value) returned "yes"; And return "no"; } And return "(N / A)"; }}  

Example which shows the problem

  Public square fu {public boole? IFooBrread {get; Set; }} Public class FooViewModel {public string IsFooBarred {get; Set; }} Public Square TryIt {Public TryIt () {Mapper.CreateMap & lt; Bool?, String & gt; (). ConvertUsing & lt; NullableBoolToLabel & gt; (); Mapper Createmap & lt; Foo, FooViewModel & gt; (); // true (successful) var foo1 = new foo {IsFooBarred = true}; Var fooViewModel1 = Mapper Map & lt; Foo, FooViewModel & gt; (Foo1); Debug Print ("[{0}]", fooViewModel1.IsFooBarred); // print: [yes] // false (successful) var foo2 = new fu {isfu berried = false}; Var fooViewModel2 = Mapper Map & lt; Foo, FooViewModel & gt; (Foo2); Debug Print ("[{0}]", fooViewModel2.IsFooBarred); // print: [no] // tap (unsuccessful) var foo3 = new foo {IsFooBarred = null}; Var fooViewModel3 = Mapper Map & lt; Foo, FooViewModel & gt; (Foo3); Debug Print ("[{0}]", Fu controversyModel 3. ISFU Berad); // print: [] / / print: [(n / a)]}}  

Question:

  1. Is this a bug or a design?
  2. If this is by design, then what is the logic behind working in this manner?
  3. Can you recommend an alternate solution?

Do you need to specify convert usage for the map? Otherwise, I'm not sure how to use the members of the Foo Class member of ISFU. But I'm not familiar with Mapper, and maybe it can understand (it appears in the first two cases).

If you put breakpoints in convert, then it gets hit in Debugger) in any case?


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -