Unit of Work
The Unit of Work pattern is a central concept in ensuring that data manipulation operations are coordinated and handled as a single transaction.
This is especially critical in systems where multiple related operations must succeed or fail together, maintaining data integrity and consistency.
In Backlot, the IUnitOfWork interface defines the contract for implementing the Unit of Work pattern.
RavenUnitOfWork
Section titled “RavenUnitOfWork”The RavenUnitOfWork class is an implementation of the IUnitOfWork interface specifically designed for use with RavenDB.
It manages a session with the database and ensures that commands are executed as part of a single transaction.
This class encapsulates the logic required to commit changes to the RavenDB instance and provides a mechanism for disposing of resources.
Key Components
Section titled “Key Components”- AsyncSession: Manages the session with the RavenDB instance, allowing for asynchronous operations.
- Commands: A dictionary that stores the commands to be executed as part of the unit of work.
- Commit(): Executes all commands stored in the
Commandsdictionary and then saves the changes to the database. - Dispose(): Cleans up resources, ensuring that the session and command dictionary are properly disposed of.
Configuration RavenDB
Section titled “Configuration RavenDB”Use the following director to configure the RavenDB repositories with a Raven unit of work:
public class Director(IFileSystem fileSystem, IConfigurationManager configurationManager, ContainerBuilder builder) : AutofacContainerDirector< RavenRelationRepository, RavenPersistedRoleRepository, RavenUnitOfWork, BasicUserContext, CacheFactory>(fileSystem, configurationManager, builder){ protected override string SecretKey => "<your secret>";}Memory Unit of Work
Section titled “Memory Unit of Work”For testing and prototyping purposes, a dummy implementation of the IUnitOfWork interface is used.
This implementation does not actually persist changes but is useful for development and testing scenarios.
Dummy Unit of Work
Section titled “Dummy Unit of Work”The DummyUnitOfWork class is a no-op implementation of the IUnitOfWork interface.
It is used when the implementation does not require or support the unit of work pattern.
Configuration Memory
Section titled “Configuration Memory”Use the following director to configure the memory repositories with a dummy unit of work:
public class Director(IFileSystem fileSystem, IConfigurationManager configurationManager, ContainerBuilder builder) : AutofacContainerDirector< MemoryRelationRepository, MemoryPersistedRoleRepository, DummyUnitOfWork, BasicUserContext, CacheFactory>(fileSystem, configurationManager, builder){ protected override string SecretKey => "<your secret>";}This setup allows for rapid development cycles and testing scenarios without the overhead of a persistent data store.
