In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to analyze the query delivery in the Web application architecture. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
In Web application development, one of the most common and changeable requirements is to obtain data lists according to different query conditions. How to pass query conditions will affect the ability of the program to respond to changes in requirements, which must be considered in the architecture.
We start by passing query conditions with a bunch of parameters, such as:
List GetMsgList (int pageIndex, int pageSize, int RecipientId)
As a result, an interface is written for each different query, resulting in a bunch of interfaces; as the query condition changes, so does the interface. The most painful thing about writing programs is the frequent changes in interfaces.
Later, query objects are used, such as:
List GetMsgList (SiteMsgQuery msgQuery)
In this way, when the query condition changes, you only need to modify the definition of SiteMsgQuery, and the interface remains the same. After using this method, writing code is much less painful than before.
However, there is a problem with using this method. To complete a query, you need to instantiate the query object SiteMsgQuery twice and the domain object SiteMsgManager (responsible for business logic). In the blog park program architecture, the instantiation of query objects is completed in the presentation layer. If it is called by ajax, json will automatically reverse the order of query objects; the instantiation of domain objects is completed in the service layer.
To make the code feel better, we tried again by unquerying the object SiteMsgQuery and putting its properties in the domain object. This reduces one instantiation, only once, and if it is an ajax call, you can achieve "zero instantiation" on the server side.
Let's take a look at the code example:
Definition of the domain model:
[DataContract] public class SiteMsgManager {public SiteMsgManager () {} # region Properies [DataMember] public int PageIndex {get; set;} [DataMember] public int PageSize {get; set;} [DataMember] public int RecipientId {get; set;} public List List {get; set } # endregion public void GetList () {using (SpaceObjectContext context = new SpaceObjectContext ()) {this.List = context.SiteMsgs .Where (msg = > msg.RecipientSpaceUserId = = this.RecipientId) .Ord erByDescending (msg = > msg.id) .Skip ((PageIndex-1) * PageSize) .Take (this.PageSize) .ToList () }}
Service implementation class (which is also the service implementation of WCF):
Public class MsgService: IMsgService {public List GetMsgList (SiteMsgManager siteMsgManager) {siteMsgManager.GetList (); return siteMsgManager.List;}}
UI layer call code (WCF call, ASP.NET MVC controller):
Public class MsgController: Controller
{
/ / call ajax
[HttpPost]
Public ActionResult List (SiteMsgManager msgManager)
{
Return View ("MsgList", GetInboxMsgList (msgManager))
}
Public ActionResult Inbox ()
{
SiteMsgManager msgManager = new SiteMsgManager ()
{
PageIndex = 1
PageSize = 30
}
Return View ("Inbox", GetInboxMsgList (msgManager))
}
Private List GetInboxMsgList (SiteMsgManager msgManager)
{
Int spaceUserId = Util.GetCurrentUser
(System.Web.HttpContext.Current). SpaceUserID
MsgManager.RecipientId = spaceUserId
MsgServiceClient client = new MsgServiceClient ()
List siteMsgList = client.GetMsgList (msgManager). ToList ()
Try {client.Close ();}
Catch {client.Abort ();}
Return siteMsgList
}
}
Take a look at the above List method for ajax to call. There is no need to instantiate SiteMsgManager. The system automatically deserializes the SiteMsgManager object according to the json parameters passed by the ajax client.
Let's look at the ajax client code:
Function GetMsgList (pageIndex, pageSize) {var msgManager = {} msgManager.PageIndex = pageIndex; msgManager.PageSize = pageSize; $. AjaxSettings.dataType = 'plain/text'; $. AjaxSettings.url =' / msg/list'; $. AjaxSettings.data ='{"msgManager":'+ JSON.stringify (msgManager) +'}'; $. AjaxSettings.success = function (data) {$("# msg_list") .html (data) }; $.Ajax ();}
Js also conveys a target.
The whole process of ajax invocation is as follows: js object (msgManager)-> json- > MsgController (MVC Controller)-> proxy domain object SiteMsgManager (instance of WCF client proxy class)-> WCF service interface-> WCF service implementation (automatically generates domain object SiteMsgManager through deserialization and calls GetList () method)-> domain object completes business logic operation and returns data.
In this way, it feels more enjoyable to write code than before. We also began to use this architecture in actual development, and further improved according to the actual use.
The above is how to analyze query delivery in Web application architecture. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.