Spring. NET + nhibenate + ASP. net mvc + extjs Series 5 --asp.net MVC + extjs

Source: Internet
Author: User

In the previous series, we have completed the database design, data access and business logic. Next we will complete the front-end MVC and extjs interface sections.

During this time, spring.net has released version 1.2, and Asp.net MVC has also updated RC1 refresh. nhib.pdf to version 2. 0. The entire demo.ProgramCorresponding updates are also made.

Asp.net MVC associates background services with front-end interfaces and calls business logic in the Controller to complete frontend calls and view forwarding. There are two problems:
How does the Controller call the business logic?
It is best not to directly call business logic objects. According to the interface-oriented programming principles, IOC dependency injection is used here to inject actual business objects into the controller, in this way, only business interface programming is performed in the controller, but it is irrelevant to the specific implementation. if the secondary development has been modified, you only need to add the corresponding business logic implementation, and then modify the configuration file.
Does the Controller directly use the entity object of nhib.pdf?
The entity object of Nhibernate contains many complex one-to-many, many-to-many ing relationships, which can easily cause recursive calls. In addition, many attributes are added only for programming, developers at other layers are not required to know. in addition, the complex entity objects of Nhibernate are not easy to serialize, which often results in loop reference. If lazy loading is used, session Closure may occur.
So here we introduce DTO (Data transfer objectIs used to convert background business objects. because extjs client is used, the foreground uses JSON objects. In this way, the conversion between JSON and DTO is completed, and the conversion between DTO and Nhibernate objects is completed.
 

The following uses user as an example to complete the control layer.

      1. DTO
      The first is DTO, which is very simple. Here we only need to include the attributes that need to interact with the foreground, and DTO can not only act as the model in MVC, but also be used in datacontract in WCF.

 Namespace Directcenter. DTO {[datacontract] Public     Class Userdto {[datamember] Public   String Userid; [datamember] Public   String Username; [datamember] Public   String Managerid; [datamember]Public   String Managername; [datamember] Public   String Departmentid; [datamember] Public   String Departmentname; [datamember] Public   String Companyid; [datamember] Public   String CompanyName; [datamember] Public Datetime? Validfrom; [datamember] Public Datetime? Validto; [datamember] Public   String Telephone; [datamember] Public   String Mobile; [datamember] Public   String Email ;}}

We can see that the attributes of the DTO object are inconsistent with those of the original nhib.pdf object. How can we convert the two of them? If hard encoding is used for each conversion, the flexibility of the conversion is poor if a helper class is written using reflection. therefore, dtomapper is introduced to complete DTO and entity conversion. how can we convert object attributes such as department and company in the nhib.pdf object through dtomapper ?? You can inject the corresponding business manager in dtomapper, but this is too complicated.
 
In this way, all the business logic interfaces to be used are put into allmanagerfactory, and then the specific implementation is injected through spring.net, so that the business logic instances can be obtained anywhere.
Dependency injection is as follows:

     <  Object  ID = "Managerfactory"   Type = "Directcenter. DTO. allmanagerfactory, directcenter. DTO"  >                <  Property   Name = "Usermanager"   Ref = "Usermanagertrans"  />             <  Property   Name ="Companymanager"   Ref = "Companymanagertrans"  />               <  Property   Name = "Departmentmanager"   Ref = "Departmentmanagertrans"  />       </  Object  > 

In allmanagerfactory, you can get the object directly from applicationcontext:

 
Public StaticAllmanagerfactory managerfactory {Get{Var webapplicationcontext = contextregistry. getcontext ()AsWebapplicationcontext; allmanagerfactory manager = webapplicationcontext. GetObject ("Managerfactory")AsAllmanagerfactory;ReturnManager ;}}

In this way, we can directly convert the DTO and Nhibernate objects in userdtomapper, but it still feels too complicated and hard-coded.

  Public  Class Userdtomapper: basedtomapper { Public   Static Userdto maptodto (user model) {userdto DTO = New Userdto (); DTO. userid = model. userid; DTO. Username = model. Username; DTO. managerid = model. Manager = Null ? "  ": Model. Manager. userid; DTO. managername = model. Manager = Null ? "  ": Model. manager. username; DTO. mobile = model. mobile; DTO. telephone = model. telephone; DTO. validfrom = model. validfrom; DTO. validto = model. validto; DTO. companyid = model. company = Null ? "  ": Model. Company. companyid; DTO. companyName = model. Company = Null ? "  ": Model. Company. fullname; DTO. Dimension mentid = model. Department = Null ? "  ": Model. Department. Dimension mentid; DTO. departmentname = model. Department = Null ? " ": Model. Department. departmentname; DTO. Email = model. Email; Return Dto ;} Public   Static User mapfromdto (userdto DTO) {user = New User (); User. userid = DTO. userid; user. Username = DTO. Username; user. Manager = DTO. managerid = Null ? Null : Managerfactory. usermanager. getuser (DTO. managerid); User. mobile = DTO. mobile; user. telephone = DTO. telephone; user. validfrom = DTO. validfrom; user. validto = DTO. validto; user. company = DTO. companyid = Null ? Null : Managerfactory. companymanager. getcompany (DTO. companyid); User. Department = DTO. extends mentid = Null ? Null : Managerfactory. departmentmanager. getdepartment (DTO. Dimension mentid); User. Email = DTO. Email; user. createtime = datetime. now; Return User ;}}

2. Controller

Here I use mvccontrib to make Asp.net MVC run in the spring.net container (Mvccontrib also contains structuremap, Windsor, nvelocity and other support for Asp.net MVC)In this case, we must define the Controller object in spring.net.

 
 <Object ID="Usercontroller"Singleton="False" Type="Directcenter. controllers. usercontroller, directcenter. controllers" ></Object>

note that if Singleton is not set to false, the spring.net container manages the controller, in this way, only one instance exists. The controllercontext of MVC will not be cleared during each request. As a result, when modelbinder binds the controller parameter, it actually binds the value of the same name parameter in the previous request.
like DTO, we define a basecontroller base class and add allmanagerfactory to the base class, in this way, each inherited controller can directly use the business interface.
for example, If you log on to the console, You can implement the following: (usercontroller. CS)

 [acceptverbs (httpverbs. post)]  Public  actionresult login ( string  userid,  string  password) {var rdto =  New  resultdto (); User user = managerfactory. usermanager. getuser (userid);  If  (user! =  null  & user. password. trim () = password. trim () {rdto. message = "Login successful"; rdto. result =  true  ;}< span style = "color: # 0000ff"> else  {rdto. message = "Login Failed"; rdto. result =  false ;}< span style = "color: # 0000ff "> return   This . JSON (rdto);} 

MVC-related things will not be explained here. The newly added jsonresult facilitates operations in JSON format. we can see that the returned result is a resultdto object, not a simple string. This is to standardize the return value, because in general, the foreground not only needs the return value, but in other cases, the front-end needs to know the execution result and prompt information. resultdto includes the execution result, the returned data, and the following three attributes are prompted:

 
[Datacontract]Public ClassResultdto {[datamember]Public BoolResult; [datamember]Public StringMessage; [datamember]Public ObjectData ;}

In this way, the foreground can judge and execute Based on the resultdto. Result attribute. If it is true, data is read, and if it is false, message is displayed.
Come here, go home for dinner, and I will try the rest later.CodeDownload.

 
 

Author: lone knight (like a year of water)
Source: http://lonely7345.cnblogs.com
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

Related Article

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.