Unit of Work Models
Class Diagrams
Sequence Diagram
The UnitOfWork class is an interface to the Object Model repository or database. We layer this in this manner to allow us to create mocks of the Repository layer. The UnitOfWork contains a reference to the repository of type ISystemRepository. If the UnitOfWork class receives an instantiation request containing a valid ISystemRepository reference, it will utilize that instance, which in the case of Unit Testing, is exactly the behavior we want.
When we’re not testing, the default constructor looks for the correct path to get to the database on the device.
The actual units of work are public methods on the UnitOfWork class that return objects of type UnitOfWorkResults<T>, an abstract class that acts as the base class for a number of type-specific UnitOfWorkResults<T> instances. T represents the type of object to be returned in the base model’s Results property.
If an error occurs in the Repository level, it is captured in the UnitOfWork class and attached to the UnitOfWorkResults<T> class in the WorkException property. In the case of an error, T will generally result in a NULL value being assigned to the results object’s Results property.
It is the responsibility of the calling class to handle any errors upon an unsuccessful attempt to perform the given task. Success or failure is indicated by the Successful property on the results class’s base class.