c# - AutoMapper - What's difference between Condition and PreCondition -


suppose mapping using automapper bellow:

mapitem.formember(to => to.someproperty, => {     from.condition(x => ((fromtype)x.sourcevalue).otherproperty == "something");     from.mapfrom(x => x.myproperty); }); 

what's difference of substitute condition precondition:

    from.precondition(x => ((fromtype)x.sourcevalue).otherproperty == "something"); 

what's practical difference between 2 methods?

the diference precondition executed before acessing source value , condition predicate, in case, before value myproperty precondition predicate run, , value property evaluated , condition executed.

in following code can see this

class program {     static void main(string[] args)     {         mapper.initialize(cfg =>         {             cfg.createmap<person, personviewmodel>()                 .formember(p => p.name, c =>                 {                     c.condition(new func<person, bool>(person =>                     {                         console.writeline("condition");                         return true;                     }));                     c.precondition(new func<person, bool>(person =>                     {                         console.writeline("precondition");                         return true;                     }));                     c.mapfrom(p => p.name);                 });         });          mapper.instance.map<personviewmodel>(new person() { name = "alberto" });     } }  class person {     public long id { get; set; }     private string _name;      public string name     {                 {             console.writeline("getting value");             return _name;         }         set { _name = value; }     } }  class personviewmodel {     public string name { get; set; } } 

the output program is:

precondition getting value condition 

because condition method contains overload receives resolutioncontext instance, have property called sourcevalue, condition evaluate property value source, set sourcevalue property on resolutioncontext object.

attention:

this behavior work until version <= 4.2.1 , >= 5.2.0.

the versions between 5.1.1 , 5.0.2, behavior not working anymore.

the output in versions is:

condition precondition getting value 

Comments

Popular posts from this blog

javascript - D3 (v3) resetting a previously animated circle after clicking another one -

python - ImportError: Missing required dependencies ['numpy'] -