ASP.NET MVC 4 – Dependency Injection ( Unity.Mvc4 ) 方法
Add ( Unity.Mvc4 ) NuGet Package on MVC Web Application Project
( Project : ServiceLibrary ) Product.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceLibrary { public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } } } |
( Project : ServiceLibrary ) IProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceLibrary { public interface IProductRepository { IEnumerable<Product> GetAll(); Product Get(int id); Product Add(Product item); bool Update(Product item); bool Delete(int id); } } |
( Project : ServiceLibrary ) ProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServiceLibrary; namespace ServiceLibrary { public class ProductRepository : IProductRepository { private List<Product> products = new List<Product>(); private int _nextId = 1; public ProductRepository() { Add(new Product { Name = "Computer", Category = "Electronics", Price = 23.54M }); Add(new Product { Name = "Laptop", Category = "Electronics", Price = 33.75M }); Add(new Product { Name = "iPhone4", Category = "Phone", Price = 16.99M }); } public IEnumerable<Product> GetAll() { return products; } public Product Get(int id) { return products.Find(p => p.Id == id); } public Product Add(Product item) { if (item == null) { throw new ArgumentNullException("item"); } item.Id = _nextId++; products.Add(item); return item; } public bool Update(Product item) { if (item == null) { throw new ArgumentNullException("item"); } int index = products.FindIndex(p => p.Id == item.Id); if (index == -1) { return false; } products.RemoveAt(index); products.Add(item); return true; } public bool Delete(int id) { products.RemoveAll(p => p.Id == id); return true; } } } |
( 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 ServiceLibrary; 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 ServiceLibrary; namespace MvcApplication.Controllers { public class HomeController : Controller { readonly IProductRepository repository; public HomeController(IProductRepository repository) { this.repository = repository; } … … public ActionResult Unity() { var model = repository.GetAll(); return View(model); } } } |
( Project : MVCApplication ) \Views\Unity.cshtml |
@model List<ServiceLibrary.Product> @foreach(var item in Model) { <div>@(item.Name)</div> } |