Transaction scripts and domain models at the business logic layer

Source: Internet
Author: User
: This article mainly introduces the transaction script and domain model at the business logic layer. For more information about PHP tutorials, see. Previous BlogYou have learned about the frontend controller, page controller, and application controller. if they have carefully arranged the communication between the external world and the system, the business logic layer is used to process the application's business. The business logic layer should stay away from those external "noises ". Business logic is the fundamental purpose of the entire application, and other parts of the system serve this part.

Here we will introduce two commonly used domain logic modes: transaction script mode and domain model mode.

I. transaction script

1.1 Concepts

Transaction Script: processes are used to organize business logic. each process processes a single request from the presentation layer. It seems a little abstract. Most business applications can be considered as a series of transactions. sometimes, transactions may display the database information, and sometimes may involve many checksum calculation steps. The transaction script organizes all these logics into a single process, and each transaction has its own transaction script, that is, it has its own execution process, note that common subtasks between transactions can be divided into multiple subprograms.

1.2 Why use transaction scripts

The advantage of the transaction script mode is that you can quickly get the desired results. Each script can process input data well and operate the database to ensure desired results. Therefore, it is a fast and effective mechanism, and does not need to invest a lot of time and effort in complex design. it is not suitable for small and tight projects.

1.3 implement transaction scripts

With my work experience, many programmers are unaware of using this model, including my previous experience.

Now suppose there is a postBlogAnd deleteBlogThe two services are considered as two transaction scripts respectively.

Php code

  1. // Create a base class for data processing. assume that pdo is used.
  2. Abstract class Base {
  3. Function _ construct (){
  4. // Before this is usedBlogLecture Registry
  5. $ Dsn = \ woo \ base \ApplicationRegistry: getDSN ();
  6. If (is_null ($ dsn )){
  7. Throw new \ woo \ base \ AppException("No DSN ");
  8. }
  9. Self: $ DB = new \ PDO ($ dsn );
  10. Self: $ DB-> setAttribute (\ PDO: ATTR_ERRMODE, \ PDO: ERRMODE _Exception);
  11. }
  12. Protected function doStatement (){
  13. // Execute SQL
  14. }
  15. }
  16. Class blogManager extends Base {
  17. Static $ add_blog = "insert into blog (name) values (? )";
  18. Static $ del_blog = "delete from blog WHERE (?) ";
  19. // AddBlogTransaction script
  20. Function addBlog (...){
  21. // Processing parameters, spelling SQL statements in add_blog format, calling the parent class doStatement for execution, notifying friends, and a series of subprograms...
  22. }
  23. // DeleteBlogTransaction script
  24. Function delBlog (...){
  25. // Process parameters, spelling SQL statements in del_blog format, and calling the parent class doStatement for execution.
  26. }
  27. }
  28. ?>

This example is very simple, but it just reflects the advantages of transaction scripts because of its simplicity. If you write a more complex application, this method makes the project not easily scalable, because the transaction scripts always inevitably penetrate into each other, leading to code duplication.

2. domain model

2.1 Concepts

Domain Model: it is difficult to express clearly. Simply put, the domain model symbolizes various participants in projects in the real world. "EverythingObjectThe principle is fully embodied here. ElsewhereObjectThere are always various specific responsibilities. in the domain model, they are often described as a group of attributes and additional proxies. They are something that does something.

2.2 Why use domain models

In actual code, there will be many transaction script modes, and it will be found that repeated code is a common problem. When different transactions need to execute the same task, repetition seems to be the fastest solution, but this greatly increases the cost of code maintenance. Sometimes it can be solved through refactoring, but slowly copying and pasting may become an inevitable part of development.

2.3 implement domain model

To achieve comparison, reference the transaction model example and map the domain model class directly to the table of the relational database (this will make development easier)

Php code

  1. // Create a base class for data processing. assume that pdo is used.
  2. Abstract class Base {
  3. Function _ construct (){
  4. // Before this is usedBlogLecture Registry
  5. $ Dsn = \ woo \ base \ApplicationRegistry: getDSN ();
  6. If (is_null ($ dsn )){
  7. Throw new \ woo \ base \ AppException("No DSN ");
  8. }
  9. Self: $ DB = new \ PDO ($ dsn );
  10. Self: $ DB-> setAttribute (\ PDO: ATTR_ERRMODE, \ PDO: ERRMODE _Exception);
  11. }
  12. Protected function doStatement (){
  13. // Execute SQL
  14. }
  15. }
  16. Class blogModel extends Base {
  17. Function addBlog (...){}
  18. }
  19. // Create a base class for the domain model
  20. Abstract class DomainObject {
  21. Private $ id;
  22. // $ Id is the primary key id of the table data
  23. Function _ construct ($ id = null ){
  24. $ This-> id = $ id;
  25. }
  26. Function getId (){
  27. Return $ this-> id;
  28. }
  29. // Keep everything in mindObject
  30. Static function getCollection ($ type ){
  31. // ObtainObject
  32. }
  33. }
  34. Class Blog extends DomainObject {
  35. Private $ name;
  36. Private $ feed;
  37. Function _ construct ($ id = null, $ name = null ){
  38. $ This-> name = $ name;
  39. $ This-> feed = self: getCollection ("\ woo \ domain \ Feed ");
  40. Parent: :__ construct ($ id );
  41. }
  42. Function addBlog (...){
  43. // Call the blogModel method to add
  44. // Call the feed to send a notification to a friend.
  45. }
  46. Function setFeed (FeedCollection $ Feed ){
  47. $ This-> feed = $ Feed;
  48. }
  49. }
  50. ?>

Conclusion 2.4

The simplicity or complexity of the domain model depends on the complexity of the business logic. The advantage of using a domain model is that when you design a model, you can focus on the problems to be solved by the system, while other problems (such as persistence and performance) can be solved by other layers.

In implementation projects, most programmers focus half of their attention on the database when designing domain models. Separating the domain model from the data layer may cause a certain price. you may also put the database code directly into the model (although you may use a data entry to process the actual SQL ). For relatively simple models, especially when classes and data tables are one-to-one, this method is completely feasible and can reduce coordinationObjectAnd the external system created by the database.

The above introduces the transaction script and domain model at the business logic layer, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.