Esframework development manual SeriesArticleI have introduced in detail how to use the esplus provided by esplus. application. customizeinfo space to send and process custom information. In the demo we introduced earlier, we also showed how to define information types and protocols, and how to implement icustomizehandler to process received information. In general, in a simple business system, we can process all the information in a customizehandler class like a demo, and concentrate all the business logic in this place. However, when the business becomes more complex, you will find that the customizehandler class will become larger and larger, and there are also a lot of business logic associated with little. According to the design principle of "low coupling and high cohesion", we need to split the complicated customizehandler into multiple highly cohesive and low coupling classes, classify the received information separately.
I. Divide and conquer the design phase
As I mentioned earlier, the most fundamental principle of separation and governance is the basic object-oriented design concept --High Cohesion and low coupling.
In actual projects, the analysis goal of High Cohesion and low coupling is our business logic. Therefore, splitting mimizehandler is actually to split the business logic. Further, the custom information to be processed is actually a display of the business logic type, finally, we split the types of custom information.
Assume that the main business logic of a project can be divided into three types: A, B, and C. Custom information can also be divided into three types: A, B, and C. Our experience is as follows, divide the values (integers) of different types of information into different integer segments. For example, if the type value of user-defined information of type A is 0-100, type B is 101-200, and type C is 201-300, when we want to add an information type to a certain business logic, we need to add a value within the corresponding value range. After such processing, when we receive a custom information, we can determine which type of business it belongs to based on its type.
During system design, our designers usually organize all the information types into a "protocol type" document and put its definition into a DLL, both the server and client developers use the DLL definition and follow the standard description of the information type in the document. For example, the following "protocol type" document can be designed for the above example:
II. Implementation Phase of divide and conquer
After you classify custom information and complete the information format conventions, You can implement an information processor. For businesses A, B, and C, we naturally implement the correspondence between the three information processors, named acustomizehandler, bcustomizehandler, and ccustomizehandler. Now the question is, how can I mount these processors to the esframework/esplus framework? Fortunately, esframework/esplus provides perfect support for the divide-and-conquer policy. We do not need to manually map the information type to the corresponding processor.
The esplus. application. customizeinfo namespace provides the iintegratedcustomizehandler interface, which can be integrated, on both the server and the client (passive). Its definition is as follows:
/// <Summary>
/// Icustomizehandler that can be integrated by complexcustomizehandler.
/// </Summary>
Public Interface Iintegratedcustomizehandler :Icustomizehandler
{
/// <Summary>
/// Whether the current processor can process custom information of the target type.
/// </Summary>
/// <Param name = "informationtype"> Type of custom information </Param>
/// <Returns> Yes? </Returns>
Bool Canhandle ( Int Informationtype );
}
Copy code
Iintegratedcustomizehandler inherits from icustomizehandler, indicating that it can do exactly the same thing as icustomizehandler, except that it processes a subset of the entire business logic. The added canhandle method is used to describe the custom information that the current processor can process. You only need to implement the iintegratedmimizehandler interface. The canhandle method added by the processor is very simple. For example, bcustomizehandler implements canhandleCodeAs follows:
Public BoolCanhandle (IntInformationtype)
{
ReturnInformationtype> =101& Informationtype <=200;
}
Copy code
After implementing various service processors, You need to combine them and link them to the esframework/esplus framework.
3. the merging phase of divide and conquer
Split before mergeThe final phase of divide-and-conquer is "integration". Only by integrating acustomizehandler, bcustomizehandler, and ccustomizehandler can a complete business processor be formed to process all the custom information received.
Esplus. application. the mizeinfo namespace provides the complexcustomizehandler class in both the server and the client (passive). It is a comprehensive processor and is equivalent to a package. It can combine acustomizehandler, bcustomizehandler, and ccustomizehandler, besides, complexcustomizehandler implements the iintegratedcustomizehandler interface, which indicates two points:
- Complexcustomizehandler implements the iintegratedcustomizehandler interface, while iintegratedcustomizehandler inherits from the icustomizehandler interface, so it can be directly attached to the esframework/esplus framework.
- Complexcustomizehandler implements the iintegratedcustomizehandler interface, indicating that it can be integrated by other complexcustomizehandler again. Just like in a giant system, the business logic can be split down step by step, and finally merged up by complexcustomizehandler.
The implementation principle of complexcustomizehandler is very simple. It only distributes the received custom information to the correct processor for processing, and does not participate in any actual business process. The class diagram is as follows:
For the above example, we put the samples, bcustomizehandler, and ccustomizehandler instances in the handlerlist of complexcustomizehandler, and inject the complexmimizehandler object into the initialize method of rapidpassiveengine and rapidserverengine, you can connect to the esframework/esplus framework.
4. More instructions
Although esframework/esplus provides good support for this policy, it is not the only mode to implement the policy of divide and conquer. You can use iintegratedcustomizehandler and complexcustomizehandler to split the business logic and merge it according to your habits and methods, finally, we will share the same things with each other-as long as we follow the most fundamental design principle of "low coupling and high cohesion.