ASP. NET MVC + EF + spring.net Project Practice (iv)

Source: Internet
Author: User

This article writes how to use Spring.net to decouple individual projects

1. Add the Istudentbll file to the interface layer with Getstudent and getallstudents two methods, and then implement both methods in the STUDENTBLL class. Also add Studentviewmodel to the Studentmanagesystem.viewmodel layer.

Note that the attribute studentrepository is used here, and later the properties are injected with spring.net

  

2. Add unit test work, write unit tests for the BLL layer. The Nsubstitute is referenced here as a mock tool, isolating the repository layer for the BLL layer. Specific use of the method, can be self-Baidu, here do not explain.

  

3. Refer to the spring.net inside the Spring.core, in order to be able to inject, need to re-implement the Icontrollerfactory interface, as follows

 Public classSpringcontrollerfactory:icontrollerfactory {/// <summary>        ///Default controllerfactory/// </summary>        Private StaticDefaultcontrollerfactory Defalutf =NULL;  PublicIController Createcontroller (System.Web.Routing.RequestContext RequestContext,stringcontrollername) {            //Get Spring Context//Webapplicationcontext CTX = Contextregistry.getcontext () as Webapplicationcontext;            varCTX =Contextregistry.getcontext (); stringController = controllername +"Controller"; //Find out if the controller is configured            if(CTX. Containsobject (Controller)) {ObjectControllerf =CTX.                GetObject (Controller); return(IController) Controllerf; }            Else            {                if(Defalutf = =NULL) {Defalutf=Newdefaultcontrollerfactory (); }                returnDefalutf.            Createcontroller (RequestContext, controllername); }        }         Public voidReleasecontroller (IController Controller) {//Get Spring ContextIapplicationcontext CTX =Contextregistry.getcontext (); if(!CTX. Containsobject (Controller. GetType (). Name)) {if(Defalutf = =NULL) {Defalutf=Newdefaultcontrollerfactory (); } defalutf.            Releasecontroller (Controller); }        }         PublicSystem.Web.SessionState.SessionStateBehavior Getcontrollersessionbehavior (RequestContext RequestContext,stringcontrollername) {            returnSessionstatebehavior.default; }    }
View Code

Set the controller factory in the Application_Start method of Global.cs, as follows

4. Configuring the Spring.net file in Web. config

First, the node <sectiongroup> is added to the configsections, and as follows, this node must be the first

Then add spring context to the configsections side, you can use the external file (1), or you can use the Content node (2), and here I put the configuration node in the App_Data.

Objects file contents are as follows

<?XML version= "1.0" encoding= "Utf-8"?><Objectsxmlns= "Http://www.springframework.net">  <ObjectID= "HomeController"type= "StudentManageSystem.Web.Controllers.HomeController, studentmanagesystem.web"Singleton= "false">    < Propertyname= "STUDENTBLL"ref= "STUDENTBLL"></ Property>  </Object>  <ObjectID= "STUDENTBLL"type= "StudentManageSystem.Business.StudentBLL, studentmanagesystem.business"Scope= "Request">    < Propertyname= "Studentrepository"ref= "Studentrepository"/>  </Object>  <!--Repository -  <Objectname= "Smsdbcontext"type= "Studentmanagesystem.repository.smscontext,studentmanagesystem.repository"Scope= "Request">    <Constructor-argname= "Connectionstringorname"value= "Smscontext">    </Constructor-arg>  </Object>  <ObjectID= "Repositorybase"Abstract= "true"Scope= "Request">    <Constructor-argref= "Smsdbcontext"/>  </Object>  <Objectname= "Studentrepository"type= "Studentmanagesystem.repository.studentrepository,studentmanagesystem.repository"Parent= "Repositorybase"Scope= "Request"/>  <Objectname= "Scorerepository"type= "Studentmanagesystem.repository.scorerepository,studentmanagesystem.repository"Parent= "Repositorybase"Scope= "Request"/>  <Objectname= "Graderepository"type= "Studentmanagesystem.repository.graderepository,studentmanagesystem.repository"Parent= "Repositorybase"Scope= "Request"/>  <Objectname= "Subjectrepository"type= "Studentmanagesystem.repository.subjectrepository,studentmanagesystem.repository"Parent= "Repositorybase"Scope= "Request"/></Objects>
View Code

5. Add the student view to the Web layer views/home and add the corresponding action in the HomeController. Also add the STUDENTBLL attribute to prepare for injection

When you are ready, F5 run and witness the moment of wonder ...

Oh, error, see what the reason? Spring does not find the Studentrepository class library and cannot be instantiated. Change the build path of the Studentmanagesystem.repository project to the Studentmanagesystem.web/bin directory, and re-F5 to run.

The build succeeds, the STUDENTBLL has been injected successfully, and the content can be extracted from the database, as follows.

OK, let's go here, throw a brick, hope can lead to jade, haha ~ ~ ~ There are some instructions are not clear where please forgive, welcome to discuss.

Code: Http://pan.baidu.com/s/1o6Lu6Fo

PS: Hard wide, I am ready to change work, if there is a pro needs MVC people, you can contact me.

Introduction: 8 years ago. NET BS development experience, MVC4 years +,wcf2 years +,jquery6 years +;3 years of management experience, with 10 people around the team, high quality requirements. Location: Beijing

Contact: junior_ya@163.com, or reply to contact information, I send you a resume, thank you.

ASP. NET MVC + EF + spring.net Project Practice (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.