EF – TransactionScope Tutorial
Step 1. Create the new interface class file under the Core project and the new repository class file under the Infra project. New interface class file name sets “ITransactionScopeRepository.” New repository class file name sets “TransactionScopeRepository.”
Create the CRUD methods in the ITransactionScopeRepository file.
public interface ITransactionScopeRepository<T> where T : class
{
/// <summary>
/// GetAll
/// </summary>
/// <returns>IEnumerable<T></returns>
Task<IEnumerable<T>> GetAll();
/// <summary>
/// Get information by ID
/// </summary>
/// <param name="id">int32</param>
/// <returns></returns>
Task<T> GetById(int id);
/// <summary>
/// Create new data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Create(T entity);
/// <summary>
/// Update data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Update(T entity);
/// <summary>
/// Delete data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Delete(T entity);
} Step 2. Add the CURD method in the ITransactionScopeRepository interface class file and the TransactionScopeRepository repository class file.
Note: If you don’t know how to add the generic type interface class in the controller files or how to use the generic type interface class file, please follow the “Generic Type in Repository – .NET Core Part.“
Step 3. Create a new controller file and view files under the web project. New controller file name sets “TransactionScopeController.”
Step 4. Add the repository interface class file to the controller file.
Step 5. Add AOP in the controller file.
Note: If you don’t know how to create the aspect-oriented programming in .NET core case, please follow the “Aspect-oriented programming in .NET Core.“
Step 6. Turn on the MS SQL profiler, then run the MS SQL profiler and the web application project under the debug model. Remember a thing that doesn’t close the MS SQL profiler windows.
Step 7. The MS SQL profiler will show all trace events when you click any button or load the initial page. It can let the developer understand the web application running logic between the web application and the MS SQL.
Conclusion
We demo the two method cases in the .NET Core case, such as the TransactionScope methods and the BeginTransaction method, then using MS SQL profiler sees the trace transaction events. We can understand these methods are from System.Transactions to apply, and these methods executing speed have a few difference that we can describe that non-distributed transaction and distributed transactions difference parts. In the MSDN article, we know the TransactionScope supports the distributed transactions where multiple databases involved a single transaction and non-distributed transaction. Database.BeginTransaction only supports non-distributed transactions( a local transaction where all actions are done with a single database.)
Reference
Template method – It means the program defines an algorithm skeleton in the superclass, which lets subclasses…
Strategy – It allows the programs to define a family of an algorithm, put each…
State – This pattern can let the program’s object alter its behavior when its internal state changes,…
Observer – It lets the programs define a subscription mechanism to notify multiple objects, which means when…
Null Object – Designed to act as a default value of an object. From Design Pattern –…
Memento – It allows the programs to save and restore the object’s previous state without revealing its…