ASP.NET Persistence Module¶
When working with ASP.NET, the Suite provides a Middleware that implements a Unit of Work per-request strategy. Basically, a single Unit of work will be created, and its lifetime handled, automatically.
The behavior of the Unit of Work is determined by the HTTP Request method, and
the chosen UnitOfWorkStrategy
.
If the HTTP Request method is GET, a Unit of Work with no associated transaction will be created. Otherwise, the behavior will depend on the selected strategy:
UnitOfWorkStrategy.AutoCommit
: a transaction will be created before calling the next middleware in the pipeline, and it will be committed after that middleware returns. This is the default value if not overridden.UnitOfWorkStrategy.ManualCommit
: a transaction will be created before calling the next middleware in the pipeline, but won't be committed nor rolled back after that middleware returns. Read below how to commit.UnitOfWorkStrategy.AlwaysRollback
: a transaction will be created before calling the next middleware in the pipeline, and it will be rolled back after that middleware returns.UnitOfWorkStrategy.Disabled
: no unit of work will be created.
The default UnitOfWorkStrategy
can be overridden by configuration, either
through AspNetPersistenceModuleOptions
, or by using the [UnitOfWorkStrategy]
attribute on the IAppService
class, or the method inside an App Service.
Using the ManualCommit strategy¶
When using the ManualCommit
you'll need to commit the transaction manually. In
order to do so, you can use the IUnitOfWorkManager.Current
:
Default values from Configuration¶
Important
Whenever possible, try to use the default AutoCommit
behavior and avoid
configuration or attribute customization.
The ASP.NET Persistence Module can be configured through IConfiguration
.
By code, when depending on the module:
Or through other means like the appsettings.json
:
JSON | |
---|---|
Using the [UnitOfWorkStrategy]¶
In the below example, the Unit of Work will be rolled back by default after every request.
The Calculate
method, will be rolled back automatically due to the class
attribute. The Create
method will be automatically committed, if no exceptions
are thrown during the execution of the request.