When the model is legacy.


I started a new Asp.Net MVC project with an existing domain model and DAL logic. Unfortunately “the legacy domain model” was made for different data access logic (client-server) while I need to show to show information in a different way.
Searching an easy solution to adapt old logic (Building) to new logic (BuildingDTO) presentation I found Aftermap method of Automapper.
Below the code that I wrote:

namespace LegacyDomain
{
 public class Building
 {
   public int Id { get; set; }
   public IList Resources { get; set; }
   //.....
 }

 public class Resource
 {
   public int Id { get; set; }
   public byte[] Bytes { get; set; }
   public ResourceType RType { get; set; }
  }

  public enum ResourceType { SmallPhoto, BigPhoto }
}

namespace DTO
{
 public class BuildingDTO
 {
   public int Id { get; set; }
   public int SmallPhotoId { get; set; }
   public int BigPhotoId { get; set; }
 }
}

Mapper.CreateMap()
      .AfterMap( ( i, d ) => d.SmallPhotoId = i.Resources
         .Where( r => r.RType == ResourceType.SmallPhoto )
         .Select( r => r.Id ).SingleOrDefault() )
      .AfterMap( ( i, d ) => d.BigPhotoId = i.Resources
         .Where( r => r.RType == ResourceType.BigPhoto )
         .Select( r => r.Id ).SingleOrDefault() )

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...