Let the object decide the retention operation type (add, delete, modify, and delete)

Source: Internet
Author: User


This article does not consider the impact of database concurrency and cache efficiency.

Retention of object refers to saving the object changes back to data storage. In terms of databases, they are insert, update, and delete.
1. If the entity loaded from the data source does not change, no retention operation is required.
2. Execute insert when the newly created object is retained.
3. The object loaded from the data source has changed and is updated when it is retained.
4. When the client decides to delete an object, delete the object.

Decision makers of retention operations
1. The client can act as the final decision of the retention operation. In the UI Layer implementation, the client needs to call different addition, deletion, and modification methods based on judgment. Not recommended.
2. The (recommended) entity itself can also act as the final decision of the retention operation, implemented at the domain layer, which makes the entity no longer a simple anemia model, however, this design makes the system more intelligent, and the UI Layer only needs to call a simple save method, so you do not have to worry about the details of the retained. Recommended.

Object retention
1. Call the Save Method

ModelService.Save(Model model);

2. The Retention behavior is determined based on the presistancemode attribute of the model in the SAVE method.
3. Model implements the ientity interface, while presistancemode is a read-only attribute on this interface.
4. presistancemode is defined as an enumeration. This definition is defined together with the ientity interface.

public enum PersistanceMode { None, Insert, Update, Delete }

Entity retention Judgment Method
1. The entity uses three tags to determine the presistancemode. The three tags are defined as three fields.

protected bool isNew = false;protected bool isModified = false;protected bool isRemoved = false;

2. Determine presistancemode based on the combination of the three
3. isremoved is the simplest and can be set by the client.
4. isnew is relatively simple and can be implemented in two ways.
First, when the object is retained, the query method queries the data storage with the Object ID. If no isnew = true is found.
Second, the initial assignment method is used to identify whether an object is created or loaded when it is created. This method is discussed in this article.
5. ismodified is complicated and can be implemented in two ways.
First, the process method is used to change attributes through the unified changeproperty method (process), where the ismodified value is set. The method for changing attributes of a set attribute should be completed.
Second, the comparison method is used to save the initial value of an object, and compare the initial value to determine and set the ismodified value.

Complexity of entity retention judgment
1. Save the object after it is created, presistancemode. insert.
2. The object is created, deleted, and saved, presistancemode. None.
3. create, modify, and save the object. presistancemode. insert.
4. create, modify, and delete an object and save the object. presistancemode. None.
5. Object loading, no modification, not deleted, saved, and presistancemode. None.
6. The object is loaded, modified, and saved. presistancemode. Update.
7. objects are loaded, modified, and deleted, and saved. presistancemode. Delete.
8. objects are loaded, unmodified, deleted, and saved. presistancemode. Delete.

Public persistancemode
{
Get {return this. getpersistancemode ();}
}

 

/// <Summary>
/// Deduce the entity retention mode based on the object field value
/// </Summary>
/// <Returns> </returns>
Private persistancemode getpersistancemode ()
{
If (this. isnew)
{
If (this. isremoved)
{
/* None */
}
Else
{
If (this. ismodified) {return persistancemode. insert;/* Insert */
}
Else
{
/* None */
}
}
}
Else
{
If (this. isremoved)
{
If (this. ismodified)
{
Return persistancemode. Delete;/* Delete */
}
Else
{
Return persistancemode. Delete;/* Delete */
}
}
Else
{
If (this. ismodified)
{
Return persistancemode. Update;/* update */
}
Else
{
/* None */
}
}
}
Return persistancemode. None;
}

 

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.