At the end of the year, I was very busy and everyone was looking forward to the future. For more than 30 users, development and stability were difficult to choose from. I recently sent some cover letters, so I have no news and nothing to do. Do something to help you.
I previously made a public platform query system. During Development, I found some public platform interface problems: Message Processing for public platform development. It was quite painful to develop, for the incoming messages, we need to parse them and return them one by one. It is painful to write them. I believe someone understands this. During development, I have been thinking about how to simplify development. For the time being, I can't think of a good model for development. I just wrote one by myself, hoping to help everyone.
Code has been released to github: https://github.com/JamesYing/JCWX
Step 1: create a Model class library. I have written the messages sent, events, and returned messages as demos.
RequestMessage: The sent message and event. This class is a virtual class that inherits from WXMessage.
ResponseMessage: the message returned to the server, also a virtual class, inherited from WXMessage.
In Message Processing for public platform development, we know that the server sends an xml file in Post mode and requests it. inputStream is a Stream type, which has been considered before. It is obtained using XmlSerializer. deserialize (stream) is used to parse objects. However, in practice, it is found that MsgType cannot be determined. I have to judge before parsing. This is not suitable for performance, therefore, I have rewritten the RequestMessage constructor, RequestMessage (XElement), and set Stream-> XElement to construct RequestMessage (to be used. ). However, I still keep the original Deserialize method. Usage: RequestMessage. Deserializ <RequestMessage> (stream). For details, refer to my source code.
We know that Xml Information is returned to the user. You can directly use ResponseMessage. Serializable () to return the xml information to the user. In the ResponseMessage constructor, there is a ResponseMessage (RequestMessage message) to convert FromUserName and ToUserName, which will be explained in subsequent articles.
Step 2: create a Business class library and provide some public interfaces for development.
IMessageRole: Information Processing rule. inherit this interface to implement the rule.
IMessageHandler: process the information and report it to the user based on the information.
NotHandlerMessage: inherits from IMessageHandler. This is an implementation that returns a null value if it cannot be processed. You can also write it by yourself.
In the IMessageRole interface, there is only one IMessageHandler MessageRole (XElement xml). To improve the performance, I converted the Stream into XElement through xml. element ("MsgType") is used to determine the type, and an IMessageHandler is returned.
IMEssageHandler: ResponseMessage HandlerRequestMessage (XElement xml); return different responsemessages to users based on different requests.
This quick framework is very simple. In practice, you need to write it yourself:
1. Information Processing rules inherit from IMessageRole. during processing, you can perform analysis based on MsgType, or analyze based on different Text content, and return different IMessageHanlder.
2. Information Processing, inheriting IMessageHandler, this may need to be written many times, depending on your project requirements.
The public platform provides a test interface, but I haven't used it yet, so I simply simulated it myself.
Create a RequestTextMessage ):
Simulate as Stream:
Now, with the simulated environment, we can write our own rules:
Write two more message processing classes:
In TextMessageHandler, the processing type is text information. If the content contains info, A ResponseTextMessage is returned. Next we will practice:
Please add the response judgment to check whether it is null, because we do not have to process every message.
Check the running result:
OK. The operation is successful. tests have not yet been conducted. The test code will be added later. I believe this quick development framework will help you develop public platforms better. Okay, I'm off duty. I will explain some models tomorrow.
Code has been released to github: https://github.com/JamesYing/JCWX