You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
38
38
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
+
39
55
## **Usage**
40
56
41
-
### **1. Service Registration**
57
+
### **2. Service Registration**
42
58
43
59
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.
44
60
@@ -62,7 +78,7 @@ public void ConfigureServices(IServiceCollection services)
62
78
63
79
This setup uses the traditional `AddDbContextFactory` method to configure `DbContextFactory` for `UserDbContext` and specifies the desired service lifetime for the factory.
64
80
65
-
### **2. Using the Repository and Unit of Work in Controllers**
81
+
### **3. Using the Repository and Unit of Work in Controllers**
66
82
67
83
After configuring the services, use the `IUnitOfWork` and repository pattern in your controllers to manage database operations.
68
84
@@ -98,7 +114,7 @@ public class UserController : Controller
98
114
}
99
115
```
100
116
101
-
### **3. Automatic Timestamps for Entities**
117
+
### **4. Automatic Timestamps for Entities**
102
118
103
119
If your entities implement the `IHasDateTimeOffset` interface, their `CreatedAt` and `UpdatedAt` properties will be automatically managed when using repository methods.
104
120
@@ -119,21 +135,6 @@ public class TestUserEntity : IHasDateTimeOffset
119
135
}
120
136
```
121
137
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.
0 commit comments