ASP.NET MVC 4 – Model + JAX-WS + Unity.Mvc4 + AutoMapper 方法

ASP.NET MVC 4 – Model + JAX-WS + Unity.Mvc4 + AutoMapper 方法

   ( Project : JAXWSLibrary ) IPlayerRepository.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using JAXWSLibrary.Entities;
 
   namespace JAXWSLibrary
   {
      public interface IPlayerRepository
      {
         IEnumerable<Entities.Player> GetAll();
      }
   }
 
   ( Project : JAXWSLibrary ) PlayerRepository.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.ServiceModel;
 
   namespace JAXWSLibrary
   {
      public class PlayerRepository : IPlayerRepository
      {
         public IEnumerable<Entities.Player> GetAll()
         {
 
            List<Entities.Player> list = new List<Entities.Player>();
            Entities.Player player;
 
            try
            {
 
               BasicHttpBinding Binding = new BasicHttpBinding();
 
               EndpointAddress Endpoint = new EndpointAddress("http://192.168.1.83:8080/SoccerJAXWS/PlayerSOAPService?wsdl");
 
               SoccerJAXWSReference.PlayerSOAPClient SoapClient = new SoccerJAXWSReference.PlayerSOAPClient(Binding, Endpoint);
 
               SoapClient.Open();
 
               int i = 0;
 
               if(SoapClient.State == System.ServiceModel.CommunicationState.Opened){
 
                  foreach (SoccerJAXWSReference.player item in SoapClient.getPlayerList())
                  {
 
                     if(i<20){
 
                        player = new Entities.Player();
                        player.PLAYER_ID = item.PLAYER_ID;
                        player.PLAYER_NAME = item.PLAYER_NAME;
                        player.PLAYER_NATION = item.PLAYER_NATION;
                        player.PLAYER_POSITION = item.PLAYER_POSITION;
                        player.PLAYER_TEAM = item.PLAYER_TEAM;
 
                        list.Add(player);
 
                     }
 
                     i++;
                  }
 
                  if (SoapClient != null && SoapClient.State == System.ServiceModel.CommunicationState.Opened) SoapClient.Close();
 
               }
 
            }catch (Exception ex){}
 
            return list;
         }
      }
   }
 
   ( Project : JAXWSLibrary ) \Entities\Player.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
 
   namespace JAXWSLibrary.Entities
   {
      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; }
      }
   }
 
   ( Project : MvcApplication ) Global.asax.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Http;
   using System.Web.Mvc;
   using System.Web.Optimization;
   using System.Web.Routing;
 
   namespace MvcApplication
   {
      public class MvcApplication : System.Web.HttpApplication
      {
         protected void Application_Start()
         {
            AreaRegistration.RegisterAllAreas();
 
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
 
            Bootstrapper.Initialise();
 
            AutoMapperConfig.Configure();
         }
      }
   }
 
   ( Project : MvcApplication ) Bootstrapper.cs
 
   using System.Web.Mvc;
   using Microsoft.Practices.Unity;
   using Unity.Mvc4;
   using JAXWSLibrary;
 
   namespace MvcApplication
   {
      public static class Bootstrapper
      {
         public static IUnityContainer Initialise()
         {
            var container = BuildUnityContainer();
 
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
 
            return container;
         }
 
         private static IUnityContainer BuildUnityContainer()
         {
            var container = new UnityContainer();
 
            … …
            container.RegisterType();
 
            RegisterTypes(container);
 
            return container;
         }
 
         public static void RegisterTypes(IUnityContainer container)
         {
 
         }
      }
   }
 
   ( Project : MvcApplication ) AutoMapperConfig.cs
 
   using System;
   using System.Collections.Generic;
   using AutoMapper;
 
   namespace MvcApplication
   {
      public class AutoMapperConfig
      {
         public static void Configure()
         {
            … …
            Mapper.CreateMap<JAXWSLibrary.Entities.Player, MvcApplication.Models.Player>().ReverseMap();
            … …
 
            Mapper.AssertConfigurationIsValid();
         }
      }
   }
 
   ( Project : MvcApplication ) \Models\Player.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
 
   namespace MvcApplication.Models
   {
      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; }
      }
   }
 
   ( Project : MvcApplication ) \Controllers\PlayerControllers.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   using AutoMapper;
   using MvcApplication.Models;
   using JAXWSLibrary;
 
   namespace MvcApplication.Controllers
   {
      public class PlayerController : Controller
      {
         readonly IPlayerRepository repository;
 
         public PlayerController(IPlayerRepository repository)
         {
            this.repository = repository;
         }
 
         public ActionResult Index()
         {
            var data = Mapper.Map(repository.GetAll().ToList(), new List());
            return View(data);
         }
      }
   }
 
   ( Project : MvcApplication ) \Views\Player\index.cshtml
 
   @model List<Models.Player>
 
   @if (Model != null)
   {
      foreach (Models.Player item in Model)
      {
         <div>  </div>
         <div>Player ID : @(item.PLAYER_ID)</div>
         <div>Player Name : @(item.PLAYER_NAME)</div>
         <div>Player Position : @(item.PLAYER_NATION)</div>
         <div>Player Nation : @(item.PLAYER_POSITION)</div>
         <div>Player Team : @(item.PLAYER_TEAM)</div>
      }
   }