it:ad:automapper:howto:home

IT:AutoMapper:HowTo

Collections

Collections

  • When mapping Collections, remember that you create Maps for the Collection Items, not the Collections of Items

EF

  • EF has special requirements (see AutoMapperTips
  • The Linq syntax is based on: Destination, Options, Source

MVC ViewModels

  • Important: For ViewModels it is important that the Value Types (int, DateTime,etc.) are nullable.
  • Important: ViewModels:
    • ViewModels used in MVC have special requirements:
    • First of all, you'll definately need a custom ModelBinder that will ensure that empty strings returned from the UI are not mapped to null.
    • This is so that if the Model is returning 3 of 10 properties, (ie 7 nulls) that one can use the IsSourceValueNull conditional modifier effectively to not try to map them (avoiding crushing the Domain entity model with nulls)
    • That also means that optional Value properties (int, decimal, DateTime, bool) on ViewModels (ie properties that may be Displayed only, and not returned…) must* be marked as Nullable so that they are not turned into Default(T) (ie not null) and therefore get through the IsSourceValueNull conditional filter, and end up crushing the Domain entity values with Date.Min, 0, etc. * You can Ignore a member – but if you also add a Conditional, it ignores the Ignore… * Important: * SubProperties have to have set statement. Can be protected, but has to have a set (initializing the sub object in constructor or get is not enough). But protected makes it usable safely from EF) * “Its only concerned about properties on the destination object”
  • /home/skysigal/public_html/data/pages/it/ad/automapper/howto/home.txt
  • Last modified: 2023/11/04 03:38
  • by 127.0.0.1