In the MVC mode, how does one distinguish the application logic (Controller layer) from the business logic (model layer )?

Source: Internet
Author: User

Most of the current frameworks are in the MVC mode, but here is a summary of how MVC works:
Basic principle: business logicCodeIt should be written in M and appliedProgramThe logic should be written in C. V is just a simple display of data.
For example, you can add a commodity to the shopping cart.
The user clicks the "add to shopping cart" button to send a request. The server starts to process the request. The process is as follows:
1. Check whether the current user has permissions (such as logon, user account status, and shopping availability)
2. Check whether the product ID to be added is valid,
3. Check whether the inventory of the commodities to be added is sufficient.
4. Add the item to the shopping cart and save the shopping cart status.
5. Feedback
In the above process:
1: Application logic (generally implemented by the Framework): because it is not directly related to the business of "add product to shopping cart"
2: business logic: you cannot buy products that do not exist. This is the basic condition for business operation.
3: business logic: the product inventory determines whether the product can be purchased. This is the basic condition for the business.
4: business logic
5: application logic
Represented in code, which may be like the following:
// Cart Controller
Class controller_cart
{
Function actionaddgoods ()
{
$ Goods_id = (INT) $ _ Get ['goods _ id'];
Cart: instance ()-> Add ($ goods_id)-> Save ();

Echo 'added successfully ';
}
}

// Cart Model
Class cart
{
/**
* All items in the shopping cart
*/
Public $ items = array ();

/**
* In the list mode, the unique instance of the shopping cart object is returned.
*/
Static function instance ()
{
...
}

Function add ($ goods_id, $ quantity = 1)
{
$ Goods = goods: Find ($ goods_id)-> get ();
// Check the ID and inventory quantity
If ($ goods-> ID & $ quantity> $ goods-> remaining)
{
// Add the item to the shopping cart
$ This-> items [] = array ($ goods, $ quantity );
}
Else
{
Throw new cartexecption ('invalid item id ');
}
Return $ this;
}
}

This code is incomplete, but the most important part is the separation of application logic and business logic.
If the process goes down and the user has to settle, the Code is as follows:
Class controller_cart
{
Function actioncheckout ()
{
Cart: instance ()-> checkout ();

Echo 'successfully ';
}
}
Class cart
{
Function checkout ()
{
// Start a Database Transaction
....

Try
{
// Create a new order object
// $ This-> owner is the owner of the current shopping cart (User)
$ Order = New Order ($ this-> owner );

// Add all items in the shopping cart to the order
Foreach ($ this-> items as $ item)
{
List ($ goods, $ quantity) = $ item;
$ Order-> Add ($ goods, $ quantity );
}
// Save the order
$ Order-> Save ();

// Clear the shopping cart
$ This-> items = array ();
}
Catch (exception $ ex)
{
// Roll back the transaction if an error occurs
....

// Throw an exception again
Throw $ ex;
}

// Return the New Order
Return $ order;
}
}

Class order extends Model
{
Public $ items;

Function add ($ goods, $ quantity)
{
$ This-> items [] = array ($ goods, $ quantity );
Return $ this;
}

Function save ()
{
Foreach ($ this-> items as $ item)
{
List ($ goods, $ quantity) = $ item;
// Reduce the inventory of each item in the order when saving the order
$ Goods-> decrremaining ($ quantity );
}

// Call the save of the parent class
Parent: Save ();

Return $ this;
}
}

The settlement code is easy to understand:
1. Call the checkout () method of the shopping cart.
2. Start the database transaction. In this way, when the order fails to be saved (for example, the inventory quantity is insufficient), the database will be rolled back to ensure that the database content is not affected.
3. Add all items in the shopping cart to the order
4. Call the SAVE () method of the Order object
4.1. traverse all items of the Order to reduce the inventory of goods (if this fails, the decrremaining () method of the product will throw an exception)
4.2 call the SAVE () method of the model parent class.
5. Clear the shopping cart and return the New Order object.

The whole process assumes that creating an order is equivalent to the customer's order confirmation, and inventory is reduced at this time. It may also be because the inventory is reduced only after the backend confirms order distribution, which is related to the seller's business strategy.

In the two examples, the business logic is implemented in the model, and the Controller (that is, the layer that encapsulates the application logic) only processes the input data, calls the business method, and feedbacks the results.

Related Article

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.