Data access method
The purpose of data access is to persist saving objects. In WebSharp, the PersistenceManager interface is defined to implement this feature. The definition of PersistenceManager can be seen: Figure 1: Websharp main interface definition --persistenceManager
We can use the following ways to hold a target:
Product Product = New Product (TRUE);
... // Handling Product
PersistenceManager PM = persistenceManagerFactory.Instance ().
CreatePersistenceManager ();
PM.PersistneWobject (p);
PM.Close ();
The code is very simple and intuitive, and there is no big pile of database-operated code, and it is not easy to have errors.
You can also initialize a persistenceManager by passing a persistenceManagerFactory to PersistenceManagerFactory, such as:
PersistenceProperty PP = New PersistenceProperty ();
PP ... // Set the properties of PP
PersistenceManager PM = persistenceManagerFactory.instance (). CreatePersistenceManager (PP);
About PersistenceProperty, you can see the following system persistent configuration information section.
Transaction processing
In many cases, when processing objects, we need to use transaction, especially in processing a pair of multi-structured objects similar to the ware list in the example above. In Websharp, we can complete this feature through the Transaction interface. The definition of the Transaction interface is visible: attached 1: Websharp main interface definition - TRANSACTION
Here is an example of using transaction processing:
Product Product = New Product (TRUE);
... // Handling Product
PersistenceManager PM = persistenceManagerFactory.Instance ().
CreatePersistenceManager ();
Transaction Trans = PM.CurrentTransaction;
Trans.begin ();
Try
{
PM.PersistneWobject (p);
TRANS.COMMIT ();
}
Catch (EXCPTION E)
{
TRANS. ROLLBACK ();
}
Finally
{
PM.Close ();
}