您的位置:首页 > 移动开发 > Unity3D

偶得--Unity在asp.net mvc上的基本应用

2010-01-31 22:27 363 查看
最近在研究Oxite,发现在这个程序中,大量采用了Unity这个微软的IOC框架。过去我对于IOC的了解,还只停留在“城堡”阶段。所以最近对Unity这个框架进行了下小小的补课,现在就将一个简单的应用拿出来跟大家分享。

首先,我有个主页HOME

public class HomeController : Controller

在它下面有个Index,在这个页面上有个主页信息,需要从数据库上获取,我们首先先创建一个获取这个信息的接口

public interface IGetHomeInfo
{
string GetHomeTitle();
}

然后,实现它的一种方式

public class GetHome : IGetHomeInfo
{

#region IGetHomeInfo 成员

public string GetHomeTitle()
{
DataService ds = new DataService();
return ds.GetHomeTitle();
}

#endregion
}

然后,我们先在index页面的Controller中,加入这个接口

public class HomeController : Controller
{

[Dependency]
public IGetHomeInfo getHomeTitle { get; set; }

[LoggerFilter()]
[ExceptionFilter()]
public ActionResult Index(int? id)
{
DataService ds = new DataService();
ViewData["HomeTitle"] = getHomeTitle.GetHomeTitle();
ViewData["Id"] = id.ToString();
return View();
}

好了,现在我们在Index(int? id)中放入了一个IGetHomeInfo 类型的注入接口,下面我们就把GetHome 注入到这个位置

public interface IContainerAccessor
{
IUnityContainer Container { get; }
}

public class MvcApplication : System.Web.HttpApplication, IContainerAccessor

{
private static UnityContainer _container;

public static IUnityContainer Container
{
get { return _container; }
}

IUnityContainer IContainerAccessor.Container
{
get { return Container; }
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Test", action = "Index", id = "" } // Parameter defaults
);

}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

if (_container == null)
_container = new UnityContainer();


IControllerFactory controllerFactory =
new UnityControllerFactory(_container);


ControllerBuilder.Current.SetControllerFactory(controllerFactory);

_container.RegisterType<IGetHomeInfo, GetHome> //在这里完成了具体的接口-类型绑定
(new ContainerControlledLifetimeManager());


    _container.RegisterType<IActionInvoker, BYSJControllerActionInvoker>();
}
}

最后,我们还需要完成UnityControllerFactory它继承自DefaultControllerFactory,代码如下:

public class UnityControllerFactory : DefaultControllerFactory
{
IUnityContainer _container;

public UnityControllerFactory(IUnityContainer container)
{
_container = container;
}

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)
{
if (controllerType == null)
throw new ArgumentNullException("controllerType");

if (!typeof(IController).IsAssignableFrom(controllerType))
throw new ArgumentException(string.Format(
"Type requested is not a controller: {0}", controllerType.Name),
"controllerType");

IController ic = _container.Resolve(controllerType) as IController;
if (typeof(Controller).IsAssignableFrom(controllerType))
{
Controller controller = ic as Controller;

if (controller != null)
controller.ActionInvoker = _container.Resolve<IActionInvoker>();

return ic;
}

return ic;
}
}

这样,一个简单的IOC注入就完成了,至于程序中的IActionInvoker是怎么回事,我们留到下回再说
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: