[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Collapse]Feature Center
  [Expand]Data Representation
  [Expand]Object Relational Mapping
  [Collapse]Connecting to a Data Store
    Data Access Layer
    Object Access Layer
    Unit of Work
    Cached Data Store
    Transferring Data via WCF Services
  [Expand]Data Exchange and Manipulation
  [Expand]Querying a Data Store
  [Expand]Using XPO in Web Applications
 [Expand]Design-Time Features
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Unit of Work

eXpress Persistent Objects can perform semi-automatic change tracking using the Unit of Work principle. Unit of Work maintains a list of persistent objects that are affected by a transaction. It keeps track of every change to every persistent object during a transaction that can affect a data store. With a single call to the UnitOfWork.CommitChanges method, all the changes made to the persistent objects are automatically saved to a data store. When working with common sessions, you need to save each persistent object individually.

Unit of Work is represented by a UnitOfWork or ExplicitUnitOfWork class. These classes inherit the base functionality from the Session class and provide the UnitOfWork.CommitChanges method that is used to save all changed persistent objects to a data store. The only requirement for this is that the property setters call the XPBaseObject.OnChanged method. To simplify things, you can call the SetPropertyValue method to meet the requirement, as shown below.

Expanded Declaring a Persistent Object


SetPropertyValue automatically calls the XPBaseObject.OnChanged method to track changes made to the Name and Location properties. Once Unit of Work's UnitOfWork.CommitChanges method is called, the changes will be automatically saved to a data store. You should always implement properties this way, if you wish changes to be recognized automatically in Units of Work.

Expanded Using Units of Work in XPO

The following sample creates a new instance of the UnitOfWork class (uow object) with default settings. In this case, there is no specific rollback code and the changes are only committed to the database if the UnitOfWork.CommitChanges method is executed. The UnitOfWork class derives from Session and thus all objects that are being handled within a unit must be fetched via that unit or associated with it by means of the session constructors in the XPO base classes.

After this we create three persistent objects of the UOWPerson type that has been declared above. If the Session has been used, then we save each persistent object individually by calling its XPBaseObject.Save method. While using Unit of Work, only one UnitOfWork.CommitChanges method call is needed to save all the made changes.

Internally, the UnitOfWork class uses a single transaction that is started automatically, committed in the UnitOfWork.CommitChanges method (or rolled back when the unit is being disposed).


You can try the functionality described here in the Data Exchange and Manipulation | UnitsOfWork section of the XPO Tutorials demo (C:\Users\Public\Documents\DevExpress Demos 19.1\Components\WinForms\Bin\XpoTutorials.exe).

Expanded See Also

Is this topic helpful?​​​​​​​