ArticleDirectory
HalfDddArchitecture
This is a self-summarized architecture, driven by semi-domain.
Actual project structure:
1.Sample. Web: Presentation Layer
2.Sample. app: ApplicationProgramLayer
3.Sample. Core: Business logic layer
4.Sample. impl: Specific implementation
5.Frameworks: Common component Diagram
Presentation LayerCode :
1.NewsapplicationNewsapp =New Newsapplication(This. Contextuserid );
A.A new news object of applicationlayer, which is used to pass in the current user ID
2.PostnewsrequestRequest = getuserinput ();
A.GenerateRequest object (like a message, divided into two request/response)
3.PostnewsresponseResponse = newsapp. postnews (request );
A.InputRequest object, execution function, return response object
4.Newsapp. bindbrokenmessages2webpagebindablefieldvalidators (This);
A.If the function execution failsNewsappThe object has an attribute.List <message> brokenrules,Contains the exceptions during the function execution,Bindbrokenmessages2webpagebindablefieldvalidators: This function sets an error message to the webpage based on the brokenrules list.
Business logic layer code Business logic layer Engineering Structure Diagram
Newsmanager Code
Post News Service method code
It is recommended that the order of code blocks in business methods be as follows:
1.Clear the exception message list
2.Call the verification class to verify the validity of the Business Object
3.Execute non-validation business logic
You need to use interfaces to avoid coupling specific external classes in the business logic.
The following three interfaces are used in the business logic:
To inject a specific class that implements an interface into the business logic2Methods: constructor injection and property injection. The constructor injection method is preferred. The Code is as follows:
UseridRepresents the user currently calling this business methodID
Basemgr Class Definition
Brokenmessageenabled Class Definition (inherit this Class Can record all the exception information Brokenrulemessage list Medium)
Entity Definition
UseAttribute in the system. componentmodel. dataannotations namespace to automatically verify features (the assembly system. componentmodel. dataannotations. dll must be referenced)
How to verify entities
On the left is the code written when verification is performed in the business logic layer.
The right side is a specific verification class, inherited inBasevalidator<Newsentity>, Is a model class, where T is the newsentity defined above; in this new verification class (postnewsvalidator), there is only one method that requires override, And the name is validateextrarules, the reason why I was named extra but not basic is thatBasevalidator<Newsentity> Basic verification has been performed for US (that is, the attribute written in newsentity). extrarules here refers to the rules that require more operations, such: when the body contains an ABC string, the body can be input at will, or the database operations, file operations, and other complex rules that need to be used for verification depend on the external system.
Warehousing Interface Definition
This warehouse can be simply considered as a previousDalLayer
Take the business logic Code mentioned above as an example:
InewsrepositoryThere are only empty shells
InsertWhere does the method come from? As follows:
The add, modify, delete, and retrieve operations have been defined. Therefore, you do not need to define additional methods if you only call common operations.
However, if you need to use other methods, such:Searchuser (username, encryptedpassword)InInewsrepositoryThis method is defined in
Application Layer Code Function
Because all external system operations have been defined as interfaces in the business logic layer, therefore, to use the business logic method, the presentation layer needs to inject the specific implementation classes that implement those interfaces into the business logic operation class. Considering the decoupling and code encapsulation, therefore, the application layer is used to inject interfaces.
Bootstrap Class
Associate specific implementation classes for each interface (using open-source toolsStructuremap)
The Code is as follows:
At this time,Objectfactory. Getinstance <Iemailsender> () The code returns the SNF. emailsenderprovider instance.
Message request / Response
Here, there is only one message mode, for example:PostnewsThis is divided according to the new message mode.3Step:
GeneratePostnewsOfRequestMessage
CallPostnewsBusiness Method
ReceivePostnewsService method returnResponseMessage
The code for the presentation layer to call the application layer is as follows:
Newsapplication Description
Note that only one constructor is input at this time.UseridIndicates the current operationUserid, The specific external implementation class will be passed in the constructorStructuremapTool to obtain
PostnewsThe method has been replaced with the inputRequestParameters, outgoingResponseObject format
InPostnewsThere is no business logic in the method. In this case, this function focuses on Object conversion, interface reference, and other assembly work.
Unit Test
Unit test is required for classes in the business logic layer.
Learning direction
Ddd(Domain driven developing,Domain-driven programming)
Unit Test Art
Design Mode
Layered and layered functions