ASP.NET MVC 4 – Dependency Injection ( Unity.Mvc4 ) + AutoMapper + Entity Framework 5 ( Get Record ) 方法
Add ( Unity.Mvc4 & AutoMapper ) NuGet Package on MVC Web Application Project
( Project : ProductServiceLibrary ) IProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProductServiceLibrary { public interface IProductRepository { IEnumerable GetAll(); IEnumerable GetAll(int rowcount); ProductEntity Get(int id); } } |
( Project : ProductServiceLibrary ) ProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProductServiceLibrary { public class ProductRepository : IProductRepository { private List<ProductEntity> products; private BIProductEntities entity; public ProductRepository() { entity = new BIProductEntities(); products = entity.ProductEntities.OrderBy(x => new { x.cat, x.subcat, x.model }).ToList(); } public IEnumerable<ProductEntity> GetAll() { return products; } public IEnumerable<ProductEntity> GetAll(int rowcount) { return products.Take(rowcount).ToList(); } public ProductEntity Get(int id) { return products.Find(p => p.id == id); } } } |
( Project : MVCApplication ) \Models\Product.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication.Models { public class Product { public int id { get; set; } public string cat { get; set; } public string subcat { get; set; } public string model { get; set; } public Nullable<long> price { get; set; } } } |
( Project : MVCApplication ) AutoMapperConfig.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using AutoMapper; using ProductServiceLibrary; namespace MvcApplication { public class AutoMapperConfig { public static void Configure() { Mapper.CreateMap<ProductServiceLibrary.ProductEntity, MvcApplication.Models.Product>(); Mapper.AssertConfigurationIsValid(); } } } |
( 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 ProductServiceLibrary; 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<IProductRepository, ProductRepository>(); RegisterTypes(container); return container; } public static void RegisterTypes(IUnityContainer container) { } } } |
( Project : MVCApplication ) \Controllers\HomeController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ProductServiceLibrary; using AutoMapper; namespace MvcApplication.Controllers { public class HomeController : Controller { readonly IProductRepository repository; public HomeController(IProductRepository repository) { this.repository = repository; } … … public ActionResult AllProduct(int? id) { if (id.HasValue) { var data = Mapper.Map(repository.GetAll(id.Value).ToList(), new List<MvcApplication.Models.Product>()); return View(data); } else { var data = Mapper.Map(repository.GetAll(30).ToList(), new List<MvcApplication.Models.Product>()); return View(data); } } public ActionResult Product(int? id) { if (id.HasValue) { var data = Mapper.Map(repository.Get(id.Value), new MvcApplication.Models.Product()); return View(data); } else { var data = Mapper.Map(repository.Get(0), new MvcApplication.Models.Product()); return View(data); } } } } |
( Project : MVCApplication ) \Views\AllProduct.cshtml |
@model List<MvcApplication.Models.Product> <table border="0" cellpadding="10" cellspacing="0"> @foreach (var item in Model) { <tr> <td>@(item.cat)</td> <td>@(item.subcat)</td> <td>@(item.model)</td> </tr> } </table> |
( Project : MVCApplication ) \Views\Product.cshtml |
@model MvcApplication.Models.Product @if (Model != null) { <table border="0" cellpadding="10" cellspacing="0"> <tr> <td>@(Model.cat)</td> <td>@(Model.subcat)</td> <td>@(Model.model)</td> </tr> </table> } |