#眉標=Enterprise Library、MVC、MVP #副標=設計模型套件系列(18) #大標=UAB與MVC設計模型的整合運用 #作者=文/圖 王寧疆 ============= public class CategoryController : Controller { ICategoryService _categoryService = null; //宣告ICategoryService類別的變數 public CategoryController(ICategoryService categoryService) //指定當建構函式被呼叫時要利用相依性的 { //關係自動建立實作ICategoryService介面的物件 _categoryService=categoryService;//將建立好的CategoryService物件存放到_categoryService變數中 } [AcceptVerbs(HttpVerbs.Get)] public ActionResult List(int? page) //宣告Controller可以執行的List動作 { var categories = _categoryService.GetCategories(); //呼叫_categoryService變數的GetCategories方法查詢資料庫 return View(categories); //將查詢的結果交給View顯示 } } ================ ===<反灰>============= using MyMVCWebSite.Models; ================ ===<反灰>============= public interface ICategoryService { List GetCategories(); //宣告實作介面的類別必須製作的方法 } public class CategoryService : ICategoryService //製作實作ICategoryService的類別 { ICategoryRepository _repository = null; //宣告型態為ICategoryRepository的變數 public CategoryService(ICategoryRepository repository) //指定當建構函式被呼叫時要利用相依性的 { //關係自動建立實作ICategoryRepository介面的物件 _repository = repository; //將建立好的CategoryRepository物件存放到_repository變數中 if (_repository == null) //如果_repository變數沒有內容值 throw new InvalidOperationException( "Repository cannot be null"); //引發InvalidOperationException型態的例外 } public List GetCategories() //負責查詢Categories資料表記錄的方法 { return _repository.GetCategories().ToList(); //呼叫_repository變數的GetCategories方法 //將自Categories資料表查詢得到記錄準備成元素型態為Category類別的List集合並傳回 } } ================ ===<反灰>============= public interface ICategoryRepository { IQueryable GetCategories(); //宣告實作介面的類別必須製作的方法 } ================ ===<反灰>============= public class CategoryRepository : ICategoryRepository { myFinanceData _db; //宣告扮演Model角色的myFinanceData類別型態的變數 public CategoryRepository(myFinanceData data) //指定當建構函式被呼叫時要利用相依性的 { //關係自動建立實作myFinanceData類別的物件 _db = data; //將myFinanceData類別的物件存放到_db變數 } public IQueryable GetCategories() //製作查詢Categories資料表記錄的方法 { return from c in _db.DataContext.Categories select c; //利用LINQ語法查詢Categories資料表的記錄 } } ================ ===<反灰>============= public class myFinanceData { private NorthwindDataClassesDataContext dataContext; //NorthwindDataClassesDataContext型態的變數 public NorthwindDataClassesDataContext DataContext { get { if (dataContext == null) //如果dataContext變數沒有內容值 { //建立NorthwindDataClassesDataContext型態的變數 dataContext = new NorthwindDataClassesDataContext(); } return dataContext; //傳回dataContext變數的內容值 } } } ================ ===<反灰>============= using Microsoft.Practices.Unity; ================ ===<反灰>============= public class UnityControllerFactory : DefaultControllerFactory //繼承自DefaultControllerFactory類別 { IUnityContainer container; //宣告IUnityContainer型態的變數 public UnityControllerFactory(IUnityContainer container) //建構函式 { this.container = container; //將container變數的內容值設定成參數的內容值 } protected override IController GetControllerInstance(Type controllerType) { try { if (controllerType == null) //如果controllerType參數沒有內容值 throw new ArgumentNullException("controllerType");//引發ArgumentNullException型態的例外 if (!typeof(IController).IsAssignableFrom(controllerType)) //如果controllerType型態的變數未 //實作IController介面的功能 throw new ArgumentException(string.Format( "Type requested is not a controller: {0}", controllerType.Name), "controllerType"); //引發ArgumentNullException型態的例外 return container.Resolve(controllerType) as IController; //從container變數取出controllerType型態的物件 } catch { return null; } //處理例外 } } ================ ===<反灰>============= protected void Application_Start() { RegisterRoutes(RouteTable.Routes); IUnityContainer container = new UnityContainer();//建立存放相依性的容器 container.RegisterType(); //註冊myFinanceData類別型態到存放相依性的容器中 container.RegisterType(); //註冊ICategoryRepository介面型態到存放相依性的容器中 container.RegisterType(); //註冊ICategoryService介面型態到存放相依性的容器中 ControllerBuilder.Current.SetControllerFactory(new MyMVCWebSite.Controllers.UnityControllerFactory( container)); //指定欲使用的ControllerFactory型態 } ================ ===<反灰>============= routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Category", action = "List", id = "" } // Parameter defaults ); ================ ============= 相關參考資源 ●若要在開發網頁或應用程式時套用Unity Application Block的功能,讀者可下載Enterprise Library 4.1-October 2008: http://www.microsoft.com/downloads/details.aspx?familyid=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en。 ●請注意2008年5月發表的Enterprise Library 4.0只支援Visual Studio 2008程式開發工具,使用Visual Studio 2005開發者必須另外下載Unit Application 1.2版:http://msdn.microsoft.com/en-us/library/dd203104.aspx。 ●Composite UI Application Block C#版下載網址: http://www.microsoft.com/downloads/details.aspx?FamilyId=7B9BA1A7-DD6D-4144-8AC6-DF88223AEE19&displaylang=en。 ●Composite UI Application Block Visual Basic .NET版下載網址: http://www.microsoft.com/downloads/details.aspx?FamilyId=B619276A-2E9E-47C6-8893-F8E5F88FD8DD&displaylang=en。 ●ASP.NET MVC Framework RC1下載網址:http://www.microsoft.com/downloads/details.aspx?FamilyID=f4e4ee26-4bc5-41ed-80c9-261336b2a5b6&displaylang=en。 ================