//Global.asax.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using Microsoft.Practices.Unity;
public class MvcApplication : UnityMvcApplication
{
protected override void Application_Start( )
{
base.Application_Start( );
}
protected override void RegisterDependency( )
{
Container.RegisterType<IRDict, DictRepository>( );
Container.RegisterType<IRStudent, StudentRepository>( );
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using Microsoft.Practices.Unity;
public class MvcApplication : UnityMvcApplication
{
protected override void Application_Start( )
{
base.Application_Start( );
}
protected override void RegisterDependency( )
{
Container.RegisterType<IRDict, DictRepository>( );
Container.RegisterType<IRStudent, StudentRepository>( );
}
}
//使用 DemoController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using 你的Respository;
using Microsoft.Practices.Unity;
public class DemoController : BaseController
{
private IRDict _dictRepository;
private IRStudent _studentRepository;
public DemoController( IRDict aDictRespository, IRStudent aStudentRespository )
{
_dictRepository = aDictRespository;
_studentRepository = aStudentRespository;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using 你的Respository;
using Microsoft.Practices.Unity;
public class DemoController : BaseController
{
private IRDict _dictRepository;
private IRStudent _studentRepository;
public DemoController( IRDict aDictRespository, IRStudent aStudentRespository )
{
_dictRepository = aDictRespository;
_studentRepository = aStudentRespository;
}
}
上面的代码都是从公司封装的框架中提取出来,仅供大家分享。
由于 Unity 已经包含在微软 Enterprise Library 中,因此,只要下载安装Enterprise Library安装包即可。
其实我个人比较喜欢 NInject 又小、又轻便,但由于特殊原因,实在是没有办法,上面这些代码很容易就改成 NInject。
Unity的地址: http://entlib.codeplex.com/
NInject的地址:http://ninject.org/download