SMARTPERSISISTENCELAYER 2.0 entity operation articles
Through the previous "setup", you can start using the powerful features of the SPL in our system. This section mainly introduces the SPL's entity (Entity) operation.
principle
The entity in the SPL may differ from other PL entities, and generally The operation can only be used with the "secondary class" to perform the operation, and this operation is not a strong type, and the developer may not completely stand in an object-oriented perspective development system, so the SPL is integrated into the integrated part of the entity Common operation, INSERT, UPDATE, DELETE, RETRIEVE operation, so when we want to operate the entity, these methods of the entity can be directly called to implement strong type operation control.
In order to automate this function, entity (entity) To inherit the EntityObject abstraction class in the SPL, such as:
Public Class Studentent: EntityObject
In this way, this entity automatically has the functionality of save (), reteve () and delete ().
Entity operation
The common operation of the entity is new, updated, deleted, retrieve, reflected in SPL as save (), delete (), retrieve (), save () It will automatically determine if the entity has been persisted (IsPersistence) to make Insert and Update distinguish.
Below I am still in the example of the "Basic Setup" to demonstrate:
The XML of the entity is defined as follows:
clas>
The entity class is as follows: content, please refer to "Basics"
Namespace Business Studies
{
Using system;
Using system.collections;
Using system.data;
Using personistencelayer;
Public Class Studentent: EntityObject
{
// Constant definition part ...
/ / Local variable definition part ...
// Property definition part ...
}
}
Entity added:
In the previous programming mode, a record in a table in the database is inserted. Some people use the SQL statement to execute, pay attention to the correctness of the SQL statement, single quotes ("'", "'" Replace); there is also a way of using Command's parameter, which can avoid single quotes; and some people use stored procedures to improve performance, then these processes affect the development speed, in the SPL our process changes Quite simple:
StudENTentity Student = new studentent (); // Subgenerate a student object
// The following property assignments
//student.id=1; // This is automatic growth, SPL will automatically get
Student.no = "
200401
"
;
Student.name = "Zhang San";
Student.birdhday = datetime.parse ("1979-01
-twenty two
"
);
Student.grade = 2;
Student.score = 580;
Try
{Student.save (); // entity save
} catch (plexception exp) // throw an exception
{
IF (exp.errtype == error) // If it is a primary key conflict
{
Response.write ("Primary Conflict");
Else
Throw Exp;
}
Step analysis:
1. Direct New an entity object to assign the object attribute
2. Then Save (), if a primary key conflict occurs, can be processed through the Catch PersistenLayer.plexception, and determine if Persistencelayer.ErrorTypes.Notunique, you can complete the new record.
Automatic growth: As the ID is the automatic growth column, it is not necessary to assign a value when assigning, and after save (), the automatically generated primary key will automatically assign the ID attribute of the entity Student, which immediately can use this generated value. .
Entity acquisition:
After the data is inserted, we will update, delete, etc.
In my design concept, each table will set a primary key, which will make it easy to query, the SPL is also the primary key function, the entity RETRIEVE () is based on the primary key of the entity, to obtain a unique record, the result is either a If it is not. For example, we have to get a record of student ID = 5:
StudENTentity Student = new studENTENTITY (); // instantiation
Student.id = 5; // assign the value to the primary key
Student.Retrieve (); // Perform a RETRIEVE ()
If (student.ispersistence) // is obtained
{
/ / Assign the control on the interface
TXTNAME.TEXT = student.name; ......
}
Step analysis:
1. Give the primary key, the example is a single-primary key, of course, I have been providing a single-primary key, but the SPL is also supporting multi-primary key, as long as it is defined as primary = true in the XML file, then before retrieve () To be assigned first.
2. Take the RETRIEVE (), this action is to read the field value of the primary key from the XML file, then read from the database, if there is, the value is assigned to the entity property; if there is no existence, no operation is made
3. For the entity, ISPERSISTENCE, if it is obtained, this value is true, and if not obtained is false, it can be done according to this value.
Therefore, when you get the value after getting, it is necessary to perform ISPERSISTENCE judgment, which is a better program habit.
Entity update:
When we get the entity and assign the value to the control on the interface, it is necessary to update it after the customer is modified.
StudENTentity Student = new studENTENTITY (); // instantiation
StudENT.ID = 5; // Assignment key value
Student.Retrieve (); // Get entity
If (student.ispersistence) // if there is
{
Student.name = txtname.text; // Get a new value
.......
Try
{
Student.save () // update
} CARTCH (PLEXCEPTION EXP) // Capture Exception
{
if (exp.errtype == ErrorTypes.restricterror) // If it is a cascaded update exception
{
Response.write ("Class Union Update Constraint Error"; response.end (); // Friendly Tips
Else
{
Throw Exp;
}
}
}
Step analysis:
1. First obtain the entity according to the primary key value, I am very recommended to do this, because the system is multi-person operation, before you get the database, you can avoid the concurrency errors that the entity is deleted, of course If you want to completely avoid this congeniality, we can set the timestamp field, which is timestamp comparison, and the implementation of the timestamp in the SPL will be described in future articles.
2. Using the Save () method, this is used in the same way with the new time, because Save () methods are different from the ISPERSISTENCE value of the entity: such as direct NEW an entity, perform Save (), The default is new, and after retrieve (), this value is TRUE, it will automatically operate with an UPDATE. This mechanism is very easy to use, because sometimes we can make SPLs to decide. We can also do RETRIEVE () before updating, and directly assign the ispersistence value to true or update operation.
3. Cascading process, when we update, we often encounter cascading constraints, you can capture ERRORTYPES.RESTRICTERROR for friendly information tips.
Entity deletion:
The deletion operation is similar to the front, it is also very simple. StudENTENTENTITYTENT = New StudENTENTENTITY ();
STUDENT.ID = 5;
Student.Retrieve ();
at (student.ispersistence)
{
Try
{
Student.delete ();
} catch (plexception exp)
{
if (exp.errtype == ErrorTypes.restricterror) // If it is a cascading deletion exception
{
Response.write ("Classified Delete Constrained Error"; Response.end (); // Friendly Tips
Else
{
Throw Exp;
}
}
}
Step analysis:
1. Like the entity, you must pass the primary key to get the entity, and then delete it after performing IsPersistence, which can delete concurrency errors.
2. When deleting, we can capture the level of the criterion to delete the error exception and perform a prompt for friendly information.
extensions
Dynamic assignment
Sometimes we will encounter this situation: There are many fields of the entity. Some fields are defined, such as 12 amounts, can be defined as "Price1, Price2 ...", like this to assign Reduce the input amount when values, we usually use dynamic assembly attribute names, SPL provides SetAttributeValue ("Properties Name," Attribute Value ") method to support this assignment method, such as:
StudENTENTENTITYTENT = New StudENTENTENTITY ();
For (int i = 1; i <= 12; i )
{
STUDENT. SetAttributeValue ("Price" i.tostring (), I.toString ());
}
Of course, what should be noted, do not appear attribute names that don't exist.
Multi-note
In large systems, we will encounter multiple accounts, that is, multiple account sets databases, the data structure is exactly the same, and the entity is active when performing operation (Save, Delete, Retrieve), will dynamically move from different data sources Get it, expands a multi-book function for this entity.
The entity is by default that the data source configured in XML is read. We can dynamically specify the data source, such as:
StudENTENTENTITYTENT = New StudENTENTENTITY ();
Student.DatabaseName = "DB
2"
; // Dynamically specify other data source names
STUDENT.ID = 5;
..........
Student.save ();
Dynamically specify which data source operation to specify the entity to specify which data source operations for the specific entity by specifying the value of the DatabaseName.
The DB2 here is a logical data source, and the functional introduction to the multi-book set will be described in detail in another article.
Memory storage
Inner BMB storage is a new function of SPL2.0. If you want to save your entity to memory, reduce the number of access to the database, thereby increasing the overall performance of the system, as long as IssaveTomemory = True is specified in the entity XML configuration file.
Such as:
clas>
In system development, it is completely transparent to the entity read from the database or from memory.
About memory storage features will be described in detail in additional articles.
to sum up:
The above introduces the situation of the entity in the SPL, of course, this is just the function of the entity itself, and everyone may also think of "I want to get records according to custom query conditions?"
"I want to make bulk updates and batch deletions in the table?"
These functions are the functionality of the criteria in the SPL, which will be described in subsequent articles.
Listen to
November 2004
MSN: TINTOWN_LIU@hotmail.com