Csla framework-Business Object status

Source: Internet
Author: User

(If you feel the following ideas have problems, please speed upLeave!)

With regard to object State maintenance, I started to understand that in linq2sql and EF, the context should track the status of the currently used objects, whether it is a new object, or retrieve objects from the database and update and delete them, and track their statuses in the context. Whether it is a simple object or a complex object that contains sub-objects, it should be stateful. The same is true in the csla framework (of course, the specific processing methods are different ).

In the csla framework, state management is only useful for editable objects, including editable root and sub-objects, editable root and sub-object sets, and dynamic editable objects. Read-Only objects are not editable, so they are not in the State.

The status attributes in the framework include isnew, isselfdirty, isdirty, isselfvalid, isvalid, issavable, and isdeleted. In the previous versions, isself and isselfvalid are not available. The display format is implemented by the user. In the current version, the field management function is available, therefore, these functions are directly implemented within the Framework, which is much more convenient than before. The meaning of each State can be seen from the name. The following is a small explanation of my opinion:

(The status does not work independently. Some statuses are based on some other statuses, such as isdirty and isselfdirty, and whether the issavable and isdeleted statuses can be saved, multiple States such as isnew (of course, they may also be related to the isbusy status of the object); these statuses will take effect successively when they are saved !)

Isnew, as the name implies, indicates the meaning of the new object. It identifies if the primary key of the object in memory exists in the database, the value is false; otherwise, the value is true, when a new object is created in the Framework, the default value is true. After the old object is deleted, the system will also assign the value true (which does not match the primary key in the database ). If isnew is true during saving, the system will call the new method. Otherwise, it will be modified or deleted.

Isselfdirty (this attribute is not available at the time of Version 2.0), which identifies whether the data of an object has been updated after it is created/obtained (it is called a dirty object in the book ), therefore, the dirty state of the object is false by default when the object is created or re-acquired. When the object is assigned a value to the object attribute or is indirectly operated through the method, the dirty property is identified as true. Isdirty is roughly the same as isselfdirty, but isdirty is also related to its sub-object status, that is, the isdrity of the object is false only when the isselfdirty of the object itself is false and the isdirty of all sub-objects is false. Otherwise, the current object is identified as dirty, so every time you access this attribute, it will traverse the status of all its sub-objects (of course, if there is true, it will return directly ), this function is implemented in earlier versions by rewriting isdirty in objects, in the current version, the framework can automatically track through field management (provided that the sub-objects in the attribute are implemented through field management in the Framework ). When isdirty is set to false, the current object (and sub-objects) is not edited. Therefore, if isdirty is set to true, the system will continue to determine the data operation method to be executed based on other statuses,

The principle of isvalid and isselfvalid is the same as that of the dirty object, except whether the verification is based on the object's data validity. It is true only when all fields of the current object are valid, that is, they do not violate any rules. Unified management of Data Validity and verification rules is also a goal of the Framework.

Isdeleted to identify whether the current object is deleted. The Framework supports direct deletion and delayed deletion. whether to delete a property is the basis for delayed deletion (note that the method for deleting sub-objects is deletechild () and the root object is delete (), not common !), It identifies the currently editable object as isdeleted = true. The system does not directly submit the object, but marks the deletion. When the user finally calls save () the system deletes the object or sub-object according to the deletion status of the current object or sub-object.Delete callDivision method.

The last is the issavable attribute, which is a collection of the preceding attributes and identifies whether the current object can be saved. Here, we reference the framework attribute.Code:

 
1:/// <Summary>
 
2:/// Returns <see langword = "true"/> if this object is both dirty and valid.
 
3:/// </Summary>
 
4:/// <Remarks>
5:/// An object is considered dirty (changed) If
 
6:/// <See CREF = "P: csla. businessbase. isdirty"/> Returns <see langword = "true"/>. It is
 
7:/// Considered valid if isvalid
 
8:/// Returns <see langword = "true"/>. The issavable property is
 
9:/// A combination of these two properties.
 
10:/// </Remarks>
11:/// <Returns> A value indicating if this object is both dirty and valid. </returns>
 
12:[Browsable (False)]
 
13:Public Virtual BoolIssavable
 
14:{
 
15:Get
 
16:{
 
17:BoolAuth;
 
18:If(Isdeleted)
19:Auth = csla. Security. authorizationrules. candeleteobject (This. GetType ());
 
20:Else If(Isnew)
 
21:Auth = csla. Security. authorizationrules. cancreateobject (This. GetType ());
 
22:Else
 
23:Auth = csla. Security. authorizationrules. caneditobject (This. GetType ());
 
24:Return(Auth & isdirty & isvalid &&! Isbusy );
25:}
 
26:}

It can be seen that in addition to the above status, there are also auth and isbusy. Auth is used to obtain the user permissions in the corresponding status. As for isbusy, I have never paid attention to it...

Attribute applications mainly control the Page Status and store data. For example, if the issavable object is set to false, the Save button is invalid. If the object is a new object, the delete button can be disabled, there is also a major application point, that is, when saving, as mentioned above, the following code is also posted:

1:/// <Summary>
 
2:/// Saves the object to the database.
 
3:/// </Summary>
 
4:/// <Remarks>
 
5:/// <Para>
 
6:/// Calling this method starts the save operation, causing the object
 
7:/// To be inserted, updated or deleted within the database based on
 
8:/// Object's current state.
9:/// </Para> <para>
 
10:/// If <see CREF = "core. businessbase. isdeleted"/> is <see langword = "true"/>
 
11:/// The object will be deleted. Otherwise, if <see CREF = "core. businimap4. isnew"/>
 
12:/// Is <see langword = "true"/> the object will be inserted.
 
13:/// Otherwise the object's data will be updated in the database.
 
14:/// </Para> <para>
15:/// All this is contingent on <see CREF = "core. businimap4. isdirty"/>. If
 
16:/// This value is <see langword = "false"/>, no data operation occurs.
 
17:/// It is also contingent on <see CREF = "core. businessbase. isvalid"/>.
 
18:/// If this value is <see langword = "false"/>
 
19:/// Exception will be thrown to indicate that the UI attempted to save
 
20:/// Invalid object.
21:/// </Para> <para>
 
22:/// It is important to note that this method returns a new version of
 
23:/// Business object that contains any data updated during the save operation.
 
24:/// You must update all object references to use this new version of
 
25:/// Business object in order to have access to the correct object data.
 
26:/// </Para> <para>
27:/// You can override this method to add your own custom behaviors to the Save
 
28:/// Operation. For instance, you may add some security checks to make sure
 
29:/// The user can save the object. If all security checks pass, you wowould then
 
30:/// Invoke the base save method via <C> base. Save () </C>.
 
31:/// </Para>
 
32:/// </Remarks>
33:/// <Returns> A new object containing the saved values. </returns>
 
34:Public VirtualT save ()
 
35:{
 
36:T result;
 
37:If(This. Ischild)
 
38:Throw NewNotsupportedexception (resources. nosavechildexception );
 
39:If(Editlevel> 0)
40:Throw NewValidation. validationexception (resources. nosaveeditingexception );
 
41:If(! Isvalid &&! Isdeleted)
 
42:Throw NewValidation. validationexception (resources. nosaveinvalidexception );
 
43:If(Isbusy)
 
44:Throw NewValidation. validationexception (resources. busyobjectsmaynotbesaved );
 
45:If(Isdirty)
46:Result = (t) dataportal. Update (This);
 
47:Else
 
48:Result = (t)This;
 
49:Onsaved (result,Null,Null);
 
50:ReturnResult;

 

Over!

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.