2、MVC+IOC容器+ORM结合
1、常规写法,难道我们每次都new一个服务,如下面的userservice和companyservice然后调用服务的find方法去操作,为什么我们不让userservice和companyservice服务注入进来呢?
public actionresult index() { dbcontext context = new dbcontext(); iuserservice userservice = new userservice(context); user user = userservice.find<user>(2);
return view(user);
}
public actionresult index2() { dbcontext context = new dbcontext(); icompanyservice companyservice = new companyservice(context);
company company = companyservice.find<company>(2);
return view(company);
}
2、我们想要的实际效果是mvc请求进来的时候,实例化控制器的时候,就把userservice和companyservice服务注入进来,但是下面的带参数的控制器构造函数testcontroller(iuserservice userservice, icompanyservice companyservice)mvc是实例化不了的,mvc只能实例化不带参数的构造函数testcontroller()
public class testcontroller : controller { private iuserservice _iuserservice = null; private icompanyservice _icompanyservice = null;
/// <summary> /// 构造函数注入---控制器得是由容器来实例化 /// </summary> /// <param name="userservice"></param> /// <param name="companyservice"></param> public testcontroller(iuserservice userservice, icompanyservice companyservice) { this._iuserservice = userservice; this._icompanyservice = companyservice; } }
public actionresult index() { dbcontext context = new dbcontext(); iuserservice userservice = this._iuserservice; user user = userservice.find<user>(2); return view(user); }
public actionresult index2() { dbcontext context = new dbcontext(); icompanyservice companyservice = this._icompanyservice;
company company = companyservice.find<company>(2);
return view(company);
}
3、在“1、看源码mvc如何实例化控制器”中提到mvc实例化控制器使用的是controllerbuilder.getcontrollerfactory()得到一个defaultcontrollerfactory工厂,然后用工厂的createcontroller方法利用反射去创建controller实例,那么我们可以把工厂换成自己实现的自定义工厂不就可以了吗,controllerbuilder还有个setcontrollerfactory方法,自定义的控制器工厂customcontrollerfactory继承defaultcontrollerfactory,微软的特点是virtual和abstract都是让我们来扩展的,那么我们复写getcontrollerinstance方法,在getcontrollerinstance方法里去构建容器
public class customcontrollerfactory : defaultcontrollerfactory { protected override icontroller getcontrollerinstance(requestcontext requestcontext, type controllertype) { iunitycontainer container = difactory.getcontainer(); //return base.getcontrollerinstance(requestcontext, controllertype); return (icontroller)container.resolve(controllertype); } }
4、在application_start中指定自定义的控制器工厂
public class mvcapplication : system.web.httpapplication {protected void application_start() { arearegistration.registerallareas();//注册区域 filterconfig.registerglobalfilters(globalfilters.filters);//注册全局的filter routeconfig.registerroutes(routetable.routes);//注册路由 bundleconfig.registerbundles(bundletable.bundles);//合并压缩 ,打包工具 combres controllerbuilder.current.setcontrollerfactory(new customcontrollerfactory());//设置自定义的控制器工厂 } }
5、依赖注入
public class difactory { public static iunitycontainer getcontainer() { iunitycontainer container = null; //container.registertype execonfigurationfilemap filemap = new execonfigurationfilemap(); filemap.execonfigfilename = path.combine(appdomain.currentdomain.basedirectory + "cfgfiles\\unity.config"); configuration configuration = configurationmanager.openmappedexeconfiguration(filemap, configurationuserlevel.none); unityconfigurationsection section = (unityconfigurationsection)configuration.getsection(unityconfigurationsection.sectionname); container = new unitycontainer(); section.configure(container, "mycontainer"); return container; } }
6、进行unity配置
<configuration> <configsections> <section name="unity" type="microsoft.practices.unity.configuration.unityconfigurationsection, unity.configuration"/> </configsections> <unity> <sectionextension type="microsoft.practices.unity.interceptionextension.configuration.interceptionconfigurationextension, unity.interception.configuration"/> <containers> <container name="mycontainer"> <extension type="interception"/> <register type="system.data.entity.dbcontext, entityframework" mapto="schoolmanager.ef.model.schooldbentities, schoolmanager.ef.model"/> <register type="schoolmanager.bussiness.interface.icompanyservice,schoolmanager.bussiness.interface" mapto="schoolmanager.bussiness.service.companyservice, schoolmanager.bussiness.service"/> <register type="schoolmanager.bussiness.interface.iuserservice,schoolmanager.bussiness.interface" mapto="schoolmanager.bussiness.service.userservice, schoolmanager.bussiness.service"/> </container> </containers> </unity> </configuration>
下一篇: 你妈骗你呢!
推荐阅读