c# - Generic AutoMapper Mapping for all inheriting from a specific class -


the majority of view models inherit base view model called encryptedbaseviewmodel. method encrypts id user not see database sensitive information.

i create automapper mapping handles mappings between entity maps encryptedbaseviewmodel. source id value , pass destinations setid method.


the viewmodel class

class encryptedbaseviewmodel {     private string _encryptedid;     public int id {get; set; }  // after new mapping method developed.      public void setid(int id)     {         _encryptedid = encrypted(id);      }      public string getid()     {         return _encryptedid;      } } 

auto mapper example

i have forged hacked example, passes value after mapping not sure of approach. suggestions welcome here.

createmap<anysource, encryptedbaseviewmodel>().formember(vm => nameof(vm.id), opt => opt.ignore()).aftermap((src,dest) => dest.setid(src.id)); 

the questions

  1. is possible make generic mapper. if so, put in anysource specified?
  2. will mapping run other specific mappings? - want to.

i'm trying avoid having write same mapping every entity, lead situations 1 forgets it.

as @davidg said, need base class or interface source. can map object, not terribly useful, because still need access source id somehow. , why aftermap? that's hack. can write resolver inline or resolver class. , second point, need include if want both mappings run (base , derived). docs.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -