ASP.NET MVC 整合 Spring.net(1)- Controller进容器
?
ProcessRequest ????????protected?internal?virtual?void?ProcessRequest(HttpContextBase?httpContext)?{????????????AddVersionHeader(httpContext);
????????????//?Get?the?controller?type
????????????string?controllerName?=?RequestContext.RouteData.GetRequiredString("controller");
????????????//?Instantiate?the?controller?and?call?Execute
????????????IControllerFactory?factory?=?ControllerBuilder.GetControllerFactory();
????????????IController?controller?=?factory.CreateController(RequestContext,?controllerName);
????????????if?(controller?==?null)?{
????????????????throw?new?InvalidOperationException(
????????????????????String.Format(
????????????????????????CultureInfo.CurrentUICulture,
????????????????????????MvcResources.ControllerBuilder_FactoryReturnedNull,
????????????????????????factory.GetType(),
????????????????????????controllerName));
????????????}
????????????try?{
????????????????controller.Execute(RequestContext);
????????????}
????????????finally?{
????????????????factory.ReleaseController(controller);
????????????}
????????}
?
?
我們可以認為MVC中Controller都是基于工廠模式來創建對象的。 但是要利用Spring.net實現IOC和AOP,就必須管理這個框架中具體對象的創建工作。 即通過配置文件的方式決定一個具體對象的生或死! Objects <objects?xmlns="http://www.springframework.net/">??
??<object?id="Demos.Domain.Object">
????<property?name="Target">
??????<object?type="Demos.Domain.Object,Demos.Domain">
????????<property?name="Manager"?ref="Demos.Domain.Manager"/>
??????</object>
????</property>
??</object>
</objects>
?
? 但是MVC框架自己的運行機制決定了Controller的創建不會經過Spring。 幸好,MVC留下了一個叫做SetControllerFactory的擴展。 即,程序運行之初我們就可以用自己的ControllerFactory替換默認的ControllerFactory.??????? protected void Application_Start()
??????? {
??????????? //用自定義的構造工廠替換默認的
??????????? ControllerBuilder.Current.SetControllerFactory(typeof(Demo.Core.ControllerFactory));
??????????? RegisterRoutes(RouteTable.Routes);
??????? }
?
這樣我們自己的工廠進行替換默認的Controller工廠后,就可以控制Controller的創建了。我們自己的Controller工廠的代碼
ControllerFactory using?System.Web.Mvc;using?System.Web.Routing;
using?Spring.Context;
namespace?Demo.Core
{
????public?class?ControllerFactory?:?IControllerFactory
????{
????????///?<summary>
????????///?Default?ControllerFactory
????????///?</summary>
????????private?static?DefaultControllerFactory?defalutf?=?null;
????????public?IController?CreateController(RequestContext?requestContext,?string?controllerName)
????????{
????????????string?controller?=?controllerName?+?"Controller";
????????????IApplicationContext?ctx?=?Container.GetContext();
????????????if?(ctx.ContainsObject(controller))
????????????{
????????????????object?controllerf?=?ctx.GetObject(controller);
????????????????return?(IController)controllerf;
????????????}
????????????else
????????????{
????????????????if?(defalutf?==?null)
????????????????{
????????????????????defalutf?=?new?DefaultControllerFactory();
????????????????}
????????????????????
????????????????return?defalutf.CreateController(requestContext,?controllerName);
????????????}
????????}
????????public?void?ReleaseController(IController?controller)
????????{
????????????IApplicationContext?ctx?=?Container.GetContext();
????????????if?(!ctx.ContainsObject(controller.GetType().Name))
????????????{
????????????????if?(defalutf?==?null)
????????????????{
????????????????????defalutf?=?new?DefaultControllerFactory();
????????????????}
????????????????defalutf.ReleaseController(controller);
????????????}
????????}
????}
}
?
?
?Spring.net 容器包裝的代碼
?
Container using?System;using?System.Collections;
using?System.Collections.Generic;
using?Spring.Context;
using?Spring.Context.Support;
namespace?Demo.Core
{
????public?class?Container
????{
????????///?<summary>
????????///?獲取應用程序上下文.
????????///?</summary>
????????///?<returns><see?cref="IApplicationContext"/>應用程序上下文.</returns>
????????public?static?IApplicationContext?GetContext()
????????{
????????????if?(FApplicationContext?==?null)
????????????{
????????????????FApplicationContext?=?ContextRegistry.GetContext();
????????????}
????????????return?FApplicationContext;
????????}
????????///?<summary>
????????///?獲取應用程序上下文.
????????///?</summary>
????????///?<param?name="name"><see?cref="IApplicationContext"/>應用程序上下文名稱.</param>
????????///?<returns><see?cref="IApplicationContext"/>應用程序上下文.</returns>
????????public?static?IApplicationContext?GetContext(string?name)
????????{
????????????return?ContextRegistry.GetContext(name);
????????}
????????///?<summary>
????????///?獲取對象.
????????///?</summary>
????????///?<typeparam?name="T">對象的類型.</typeparam>
????????///?<param?name="id">標識.</param>
????????///?<returns></returns>
????????public?static?T?GetObject<T>(string?id)
????????{
????????????return?(T)GetContext().GetObject(id);
????????}
????????///?<summary>
????????///?獲取對象類表.
????????///?</summary>
????????///?<typeparam?name="T">對象的類型.</typeparam>
????????///?<returns></returns>
????????public?static?IList<T>?GetObjects<T>()
????????{
????????????IEnumerable?items?=?GetContext().GetObjectsOfType(typeof(T));
????????????IList<T>?objects?=?new?List<T>();
????????????foreach?(DictionaryEntry?item?in?items)
????????????{
????????????????objects.Add((T)item.Value);
????????????}
????????????return?objects;
????????}
????????[ThreadStatic]
????????private?static?IApplicationContext?FApplicationContext;
????}
}
?
?
然后我們再新建個Demos.Controllers的項目。
using System.Web.Mvc;
namespace Demo.Controllers
{
??? public class DemoController : Controller
??? {
??????? public ViewResult Index()
??????? {
??????????? ViewData["Message"] = "Welcome to IOC MVC";
??????????? return View();
??????? }
??? }
}
?
? 相應Dll的配置文件 <?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net/">
? <object id="DemoController" type="Demo.Controllers.DemoController, Demo.Controllers" singleton="false" >
? </object>
</objects>
?
并把配置文件設置為嵌入資源
然后再在Web.config中添加相應配置
?
Web.config ??<sectionGroup?name="spring">??????<section?name="context"?type="Spring.Context.Support.WebContextHandler,?Spring.Web"?/>
??????<section?name="objects"?type="Spring.Context.Support.DefaultSectionHandler,?Spring.Core"?/>
??</sectionGroup>
??<spring>
????<context>
??????<resource?uri="config://spring/objects"?/>
??????<resource?uri="assembly://Demo.Controllers/Demo.Controllers/Controllers.xml"?/>
????</context>
????<objects?xmlns="http://www.springframework.net"?/>
??</spring>
?
?
測試運行,我們可以發現這個DemoController已在Spring的容器中。轉載于:https://www.cnblogs.com/moshe/archive/2010/02/07/1665344.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC 整合 Spring.net(1)- Controller进容器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2005中解决方案管理器中看不到解决
- 下一篇: 地铁上怎么那么多钢管女郎?