Skip to content

Commit 404ad19

Browse files
authored
Update README.md
1 parent 5d300f6 commit 404ad19

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,25 @@ dotnet add package SampleDotnet.RepositoryFactory --prerelease
3636

3737
Ensure that your project targets one of the following frameworks: **.NET 6.0**, **.NET 7.0**, or **.NET 8.0**. This package does not support .NET 5.
3838

39+
### **1. Parallel Operations and DbContextFactory**
40+
41+
The package supports parallel operations by using a `DbContextFactory` to create new `DbContext` instances. This approach prevents concurrency issues that occur when the same `DbContext` instance is used across multiple threads.
42+
43+
## **Special Note on Transaction Management**
44+
45+
> **IMPORTANT**: Proper transaction management is crucial when working with multiple `DbContext` instances.
46+
47+
### **Key Points to Consider:**
48+
49+
- **Using Multiple DbContexts**: If you are using multiple `DbContext` instances, each managing its own transaction, be aware that if a transaction commits successfully on one `DbContext` but fails on another, it rolls back already committed transactions to maintain data consistency (Compensate Transaction). This scenario requires careful handling to ensure that all related changes are either fully committed or completely rolled back across all DbContexts.
50+
51+
- **Using a Single DbContext**: When using a single `DbContext` instance for all operations within a unit of work, transaction management is simplified. All operations are either committed together or rolled back together, eliminating the need to manually roll back already committed transactions from different `DbContext` instances.
52+
53+
### **Best Practice**: To avoid the complexity of handling multiple transactions, use a single `DbContext` instance within the scope of a unit of work whenever possible. This approach ensures straightforward transaction management and helps maintain data integrity.
54+
3955
## **Usage**
4056

41-
### **1. Service Registration**
57+
### **2. Service Registration**
4258

4359
To use the `RepositoryFactory` and `UnitOfWork` in your application, register the necessary services in the `Startup` class or wherever you configure services in your application.
4460

@@ -62,7 +78,7 @@ public void ConfigureServices(IServiceCollection services)
6278

6379
This setup uses the traditional `AddDbContextFactory` method to configure `DbContextFactory` for `UserDbContext` and specifies the desired service lifetime for the factory.
6480

65-
### **2. Using the Repository and Unit of Work in Controllers**
81+
### **3. Using the Repository and Unit of Work in Controllers**
6682

6783
After configuring the services, use the `IUnitOfWork` and repository pattern in your controllers to manage database operations.
6884

@@ -98,7 +114,7 @@ public class UserController : Controller
98114
}
99115
```
100116

101-
### **3. Automatic Timestamps for Entities**
117+
### **4. Automatic Timestamps for Entities**
102118

103119
If your entities implement the `IHasDateTimeOffset` interface, their `CreatedAt` and `UpdatedAt` properties will be automatically managed when using repository methods.
104120

@@ -119,21 +135,6 @@ public class TestUserEntity : IHasDateTimeOffset
119135
}
120136
```
121137

122-
### **4. Parallel Operations and DbContextFactory**
123-
124-
The package supports parallel operations by using a `DbContextFactory` to create new `DbContext` instances. This approach prevents concurrency issues that occur when the same `DbContext` instance is used across multiple threads.
125-
126-
## **Special Note on Transaction Management**
127-
128-
> **IMPORTANT**: Proper transaction management is crucial when working with multiple `DbContext` instances.
129-
130-
### **Key Points to Consider:**
131-
132-
- **Using Multiple DbContexts**: If you are using multiple `DbContext` instances, each managing its own transaction, be aware that if a transaction commits successfully on one `DbContext` but fails on another, you may need to manually roll back already committed transactions to maintain data consistency. This scenario requires careful handling to ensure that all related changes are either fully committed or completely rolled back across all DbContexts.
133-
134-
- **Using a Single DbContext**: When using a single `DbContext` instance for all operations within a unit of work, transaction management is simplified. All operations are either committed together or rolled back together, eliminating the need to manually roll back already committed transactions from different `DbContext` instances.
135-
136-
### **Best Practice**: To avoid the complexity of handling multiple transactions, use a single `DbContext` instance within the scope of a unit of work whenever possible. This approach ensures straightforward transaction management and helps maintain data integrity.
137138

138139
## **Descriptions of Key Components**
139140

0 commit comments

Comments
 (0)