ASP.NET MVC 5 – AutoMapper + Data Access Layer / Entity Layer

ASP.NET MVC 5 – AutoMapper + Data Access Layer / Entity Layer
 
Remarks :
 
For Presentation Layer, the Entity Model is for mapping Entity and Page View. For CRM example, the Properties under Customer Entity Model are List<Menu> and List<Customer> etc. It is possible to have the method under Business Logic Layer for collecting the related Data Record. The Controller under Presentation Layer would map the result of Business Logic Layer Method and the Presentation Layer Model Properties. Then the Data Model with value under Presentation Layer would be binded on Page View.
 
For Best Practice, it is not recommended to use AutoMapper to cause the repeated Data Entity Model under Entity Layer & Presentation Layer.
 
Following is the sample of AutoMapper which would cause the repeated Data Entity Model under Entity Layer & Presentation Layer.

   ( Entity Framework – Entity Layer ( SoccerDAO ) ) PLAYER.cs
 
   namespace SoccerDAO
   {
      using System;
      using System.Collections.Generic;
 
      public partial class PLAYER
      {
         public PLAYER()
         {
            … …
         }
 
         public int PLAYER_ID { get; set; }
         public string PLAYER_NAME { get; set; }
         public int PLAYER_POSITION { get; set; }
         public int PLAYER_NATION { get; set; }
         public int PLAYER_TEAM { get; set; }
         public Nullable PLAYER_BIRTHDAY { get; set; }
         public bool PLAYER_ISLOAN { get; set; }
         public int PLAYER_RATING { get; set; }
 
         … …
 
      }
   }
 
   ( Entity Framework – Entity Layer ( SoccerDAO ) ) TEAM.cs
 
   namespace SoccerDAO
   {
      using System;
      using System.Collections.Generic;
 
      public partial class TEAM
      {
         public TEAM()
         {
            … …
         }
 
         public int TEAM_ID { get; set; }
         public string TEAM_NAME { get; set; }
         public int TEAM_LEAGUE { get; set; }
         public int TEAM_CITY { get; set; }
         public int TEAM_STADIUM { get; set; }
 
         … …
 
      }
   }
 
   ( Entity Framework – Entity Layer ( SoccerDAO ) ) NATION.cs
 
   namespace SoccerDAO
   {
      using System;
      using System.Collections.Generic;
 
      public partial class NATION
      {
         public NATION()
         {
            … …
         }
 
         public int NATION_ID { get; set; }
         public string NATION_NAME { get; set; }
         public int NATION_REGION { get; set; }
 
         … …
 
      }
   }
 
   ( Business Logic Layer ( SoccerDAO ) ) DataService.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
 
   namespace SoccerDAO
   {
      public class DataService
      {
         public static List<PLAYER> getPlayer()
         {
            SoccerEntities entity = new SoccerEntities();
            return entity.PLAYERs.ToList();
         }
 
         public static List<TEAM> getTeam()
         {
            SoccerEntities entity = new SoccerEntities();
            return entity.TEAMs.ToList();
         }
 
         public static List<NATION> getNation()
         {
            SoccerEntities entity = new SoccerEntities();
            return entity.NATIONs.ToList();
         }
      }
   }
 
   ( Presentation Layer ( SoccerWeb.Models ) ) PlayerView.cs
 
   using System;
   using System.Collections.Generic;
 
   namespace SoccerWeb.Models
   {
 
      public class PlayerView
      {
         public List<SoccerWeb.Models.Player> plist;
         public List<SoccerWeb.Models.Team> tlist;
         public List<SoccerWeb.Models.Nation> nlist;
      }
 
      public class Player
      {
         //public int PLAYER_ID { get; set; }
         public string PLAYER_NAME { get; set; }
         //public int PLAYER_POSITION { get; set; }
         //public int PLAYER_NATION { get; set; }
         //public int PLAYER_TEAM { get; set; }
         //public Nullable<System.DateTime> PLAYER_BIRTHDAY { get; set; }
         //public bool PLAYER_ISLOAN { get; set; }
         //public int PLAYER_RATING { get; set; }
      }
 
      public class Team
      {
         //public int TEAM_ID { get; set; }
         public string TEAM_NAME { get; set; }
         //public int TEAM_LEAGUE { get; set; }
         //public int TEAM_CITY { get; set; }
         //public int TEAM_STADIUM { get; set; }
      }
 
      public class Nation
      {
         //public int NATION_ID { get; set; }
         public string NATION_NAME { get; set; }
         //public int NATION_REGION { get; set; }
      }
 
   }
 
   ( Presentation Layer ( SoccerWeb ) ) AutoMapperConfig.cs
 
   using System;
   using AutoMapper;
 
   namespace SoccerWeb
   {
      public class AutoMapperConfig
      {
         public static void Configure()
         {
            Mapper.CreateMap<SoccerDAO.PLAYER, SoccerWeb.Models.Player>();
            Mapper.CreateMap<SoccerDAO.TEAM, SoccerWeb.Models.Team>();
            Mapper.CreateMap<SoccerDAO.NATION, SoccerWeb.Models.Nation>();
 
            Mapper.AssertConfigurationIsValid();
         }
      }
   }
 
   ( Presentation Layer ( SoccerWeb ) ) Global.asax.cs
 
   using System;
   using System.Web.Mvc;
   using System.Web.Optimization;
   using System.Web.Routing;
 
   namespace SoccerWeb
   {
      public class MvcApplication : System.Web.HttpApplication
      {
         protected void Application_Start()
         {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AutoMapperConfig.Configure();
         }
      }
   }
 
   ( Presentation Layer ( SoccerWeb.Library ) ) ViewMapper.cs
 
   using System;
   using System.Collections.Generic;
   using SoccerWeb.Models;
 
   namespace SoccerWeb.Library
   {
      public class ViewMapper
      {
         public static PlayerView getPlayerView()
         {
            SoccerWeb.Models.PlayerView obj = new SoccerWeb.Models.PlayerView();
            obj.plist = AutoMapper.Mapper.Map(SoccerDAO.DataService.getPlayer(), new List<SoccerWeb.Models.Player>());
            obj.tlist = AutoMapper.Mapper.Map(SoccerDAO.DataService.getTeam(), new List<SoccerWeb.Models.Team>());
            obj.nlist = AutoMapper.Mapper.Map(SoccerDAO.DataService.getNation(), new List<SoccerWeb.Models.Nation>());
 
            return obj;
         }
      }
   }
 
   ( Presentation Layer ( SoccerWeb.Controllers ) ) HomeController.cs
 
   using System;
   using System.Web.Mvc;
   using AutoMapper;
 
   namespace SoccerWeb.Controllers
   {
 
      public class HomeController : Controller
      {
 
         … …
 
         public ActionResult Player()
         {
            return View(Library.ViewMapper.getPlayerView());
         }
 
      }
   }
 
   ( Presentation Layer ( SoccerWeb.View ) ) Player.cshtml
 
   @model SoccerWeb.Models.PlayerView
 
   … …
 
      @{
         foreach(SoccerWeb.Models.Player item in Model.plist){
            <div>@(item.PLAYER_NAME)</div>
         }
      }
 
   … …
 
      @{
         foreach (SoccerWeb.Models.Team item in Model.tlist)
         {
            <div>@(item.TEAM_NAME)</div>
         }
      }
 
   … …
 
      @{
         foreach (SoccerWeb.Models.Nation item in Model.nlist)
         {
            <div>@(item.NATION_NAME)</div>
         }
      }
 
   … …