Asp.net MVC + EF + Spring.Net 项目实践(四)

这篇写一写如何使用Spring.net来解耦各个项目

1. 在接口层添加IStudentBLL文件,里面有GetStudent和GetAllStudents两个方法;然后在StudentBLL类里实现这两个方法。此外还要在StudentManageSystem.ViewModel层添加StudentViewModel。

注意,此处使用了属性StudentRepository,后期会用Spring.net将些属性注入

  

2. 添加单元测试工作,为BLL层写单元测试。此处引用了NSubstitute做为Mock工具,为BLL层隔离Repository层。具体的使用方法,可自行百度,此处不做解释。

  

3. 引用Spring.net里面的Spring.Core,为了能够实现注入,需要重新实现IControllerFactory接口,如下

public class SpringControllerFactory : IControllerFactory
    {
        /// <summary>
        /// Default ControllerFactory
        /// </summary>
        private static DefaultControllerFactory defalutf = null;

        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            //get spring context
            //WebApplicationContext ctx = ContextRegistry.GetContext() as WebApplicationContext;

            var ctx = ContextRegistry.GetContext();

            string controller = controllerName + "Controller";
            //查找是否配置该Controller
            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)
        {
            //get spring context
            IApplicationContext ctx = ContextRegistry.GetContext();
            if (!ctx.ContainsObject(controller.GetType().Name))
            {
                if (defalutf == null)
                {
                    defalutf = new DefaultControllerFactory();
                }

                defalutf.ReleaseController(controller);
            }
        }



        public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }
    }
View Code

在Global.cs的Application_Start方法中设置控制器工厂,如下

4. 在web.config中配置Spring.net文件

首先在configSections里加入节点<sectionGroup>,内容如下,这个节点必须是排在第一位的

然后在与configSections并列的层级添加spring context,可以使用外部文件(1),也可以使用内容节点(2)方式,我这里是把配置节点放到了App_Data里面了

objects文件内容如下

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id="HomeController" type="StudentManageSystem.Web.Controllers.HomeController, StudentManageSystem.Web" singleton="false">
    <property name="StudentBll" ref="studentBll"></property>
  </object>

  <object id="studentBll" type="StudentManageSystem.Business.StudentBLL, StudentManageSystem.Business" scope="request">
    <property name="StudentRepository" ref="studentRepository"/>
  </object>

  <!-- repository -->
  <object name="smsDbContext" type="StudentManageSystem.Repository.SMSContext,StudentManageSystem.Repository" scope="request">
    <constructor-arg name="connectionStringOrName" value="SMSContext">
    </constructor-arg>
  </object>
  <object id="repositoryBase" abstract="true" scope="request">
    <constructor-arg ref="smsDbContext"/>
  </object>
  <object name="studentRepository" type="StudentManageSystem.Repository.StudentRepository,StudentManageSystem.Repository" parent="repositoryBase" scope="request"/>
  <object name="scoreRepository" type="StudentManageSystem.Repository.ScoreRepository,StudentManageSystem.Repository" parent="repositoryBase" scope="request"/>
  <object name="gradeRepository" type="StudentManageSystem.Repository.GradeRepository,StudentManageSystem.Repository" parent="repositoryBase" scope="request"/>
  <object name="subjectRepository" type="StudentManageSystem.Repository.SubjectRepository,StudentManageSystem.Repository" parent="repositoryBase" scope="request"/>
</objects>
View Code

 

5. 在Web层Views/Home添加Student视图,然后在HomeController里添加对应的action。同样也加入StudentBll属性,为注入做准备

做好准备之后,F5运行,见证奇迹的时刻到了……

呵呵,报错了,看看什么原因?是spring没有找到StudentRepository类库,无法实例化。将StudentManageSystem.Repository项目的生成路径改到StudentManageSystem.Web/bin目录下,重新F5运行起来~~~

 

生成成功,StudentBll已经注入成功,并且能从数据库里取出内容,如下。

 

好了,就到这里吧,抛个砖,希望能引出玉来,哈哈~~~里面有些说明不清楚的地方请大家包涵,欢迎讨论。

代码下载地址:http://pan.baidu.com/s/1o6Lu6Fo

 

posted @ 2015-03-30 16:11  baby_cz  阅读(720)  评论(1编辑  收藏  举报