ASP.NET MVC 4 – Dependency Injection ( Unity.Mvc4 ) + Entity Framework 5 方法
Add ( Unity.Mvc4 ) 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 ) 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() { … … Bootstrapper.Initialise(); } } } |
( 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; namespace MvcApplication.Controllers { public class HomeController : Controller { readonly IProductRepository repository; public HomeController(IProductRepository repository) { this.repository = repository; } … … public ActionResult AllProduct(int? id) { List<ProductEntity> data; if (id.HasValue) { data = repository.GetAll(id.Value).ToList(); } else { data = repository.GetAll(30).ToList(); } return View(data); } public ActionResult Product(int? id) { ProductEntity data; if (id.HasValue) { data = repository.Get(id.Value); } else { data = repository.Get(0); } return View(data); } } } |
( Project : MVCApplication ) \Views\AllProduct.cshtml |
@model List<ProductServiceLibrary.ProductEntity> <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 ProductServiceLibrary.ProductEntity @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> } |