Unit Of Work

Overview

UnitOfWork<T> is a generic class implementing the Unit of Work pattern. It manages a collection of entities of type T, tracks changes, and provides CRUD (Create, Read, Update, Delete) operations. It’s designed for use in scenarios where data binding, change tracking, and asynchronous data operations are essential.

Key Features

  • Asynchronous CRUD Operations: Supports asynchronous operations for creating, updating, and deleting entities.
  • Change Tracking: Tracks the state of entities (added, modified, deleted) for efficient update operations.
  • Event Handling: Implements event handlers for property and collection changes, along with custom events for various data operation stages.
  • Data Source Interaction: Flexible interaction with various data sources, as defined by IDMEEditor and IDataSource interfaces.
  • Observable Collections: Manages collections of entities with ObservableBindingList<T> for data binding and notification support.
  • Error Handling and Logging: Robust error handling and logging capabilities.
  • Undo Functionality: Ability to undo delete operations.

Properties

  • IsDirty: Indicates whether there are unsaved changes in the current unit of work.
  • Units: The primary collection of entities of type T being managed.
  • FilteredUnits: A filtered collection of entities based on specified criteria.
  • EntityStructure: Contains metadata about the entity type T.
  • PrimaryKey: The primary key property name of the entity type T.

Methods

CRUD Operations

  • Create(T entity): Adds a new entity to the Units collection.
  • Read(string id): Retrieves an entity by its primary key.
  • Update(T entity): Updates an existing entity in the Units collection.
  • Delete(T entity): Removes an entity from the Units collection.

Asynchronous Operations

  • Commit(): Asynchronously commits all changes made in the current unit of work.
  • InsertAsync(T doc), UpdateAsync(T doc), DeleteAsync(T doc): Asynchronous methods for CRUD operations.

Utility Methods

  • Clear(): Resets the unit of work to its initial state.
  • GetSeq(string SeqName): Retrieves the next value of a specified sequence.
  • GetPrimaryKeySequence(T doc): Obtains the primary key sequence for the given entity.
  • FindDocIdx(T doc): Finds the index of a given document in the Units collection.
  • UndoDelete(): Undoes the last delete operation.

Event Handlers

  • ItemPropertyChangedHandler: Handles property changes of individual entities.
  • Units_CollectionChanged: Responds to changes in the Units collection.

Events

  • PreInsert, PostCreate, PreDelete, PreUpdate, PostUpdate: Events triggered at various stages of data operations.

Usage

The UnitOfWork<T> class is typically used in applications that require robust data management with change tracking, data binding, and asynchronous operations. It is well-suited for desktop applications, enterprise solutions, and any scenario requiring an organized approach to entity management.

Note

This documentation provides an overview and is not exhaustive. Users of the UnitOfWork<T> class should refer to the actual code for more detailed information about each method, property, and event.