title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
21:1125 | Finally, let the ASP.NET Core MVC project reference both projects, while PackagesManagementDB references PackagesManagementDomain. We suggest you define your own projects and then copy the code of this book’s GitHub repository into them as you read this section. |
21:1126 | The next subsection describes the code of the PackagesManagementDomain domain layer abstraction project. |
21:1127 | Defining the domain layer abstraction |
21:1128 | Once the PackagesManagementDomain Standard 2.1 library project has been added to the solution, we’ll add a Tools folder to the project root. Then, we’ll place all the DomainLayer tools contained in the code associated with ch11. Since the code contained in this folder uses data annotations and defines DI extension methods, we must also add references to the System.ComponentModel.Annotations and Microsoft.Extensions.DependencyInjection.Abstration NuGet packages. |
21:1129 | Then, we need an Aggregates folder containing all the aggregate definitions (remember, we implemented aggregates as interfaces) – namely, IDestination, IPackage, and IPackageEvent. Here, IPackageEvent is the aggregate associated with the table where we will place events to be propagated to other applications. |
21:1130 | As an example, let’s analyze IPackage: |
21:1131 | public interface IPackage : IEntity<int> |
21:1132 | { |
21:1133 | void FullUpdate(IPackageFullEditDTO packageDTO); |
21:1134 | string Name { get; set; } = null!; |
21:1135 | string Description { get;} = null!; |
21:1136 | decimal Price { get; set; } |
21:1137 | int DurationInDays { get; } |
21:1138 | DateTime? StartValidityDate { get;} |
21:1139 | DateTime? EndValidityDate { get; } |
21:1140 | int DestinationId { get; } |
21:1141 | |
21:1142 | } |
21:1143 | |
21:1144 | It contains the same properties as the Package entity, which we saw in Chapter 13, Interacting with Data in C# – Entity Framework Core. The only differences are the following: |
21:1145 | |
21:1146 | It inherits from IEntity<int>, which furnishes all basic functionalities of aggregates |
21:1147 | It has no Id property since it is inherited from IEntity<int> |
21:1148 | All properties are read-only, and it has a FullUpdate method since all aggregates can only be modified through update operations defined in the user domain (in our case, the FullUpdate method) |
21:1149 | |
21:1150 | Now, let’s also add a DTOs folder. Here, we place all interfaces used to pass updates to the aggregates. Such interfaces are implemented by the application layer ViewModels used to define such updates. In our case, it contains IPackageFullEditDTO, which we can use to update existing packages. If you would like to add the logic to manage destinations, you must define an analogous interface for the IDestination aggregate. |
21:1151 | An IRepositories folder contains all repository specifications – namely, IDestinationRepository, IPackageRepository, and IPackageEventRepository. Here, IPackageEventRepository is the repository associated with the IPackageEvent aggregate. As an example, let’s have a look at the IPackageRepository repository: |
21:1152 | public interface IPackageRepository: |
21:1153 | IRepository<IPackage> |
21:1154 | { |
21:1155 | Task<IPackage?> GetAsync(int id); |
21:1156 | IPackage New(); |
21:1157 | Task<IPackage?> Delete(int id); |
21:1158 | } |
21:1159 | |
21:1160 | Repositories always contain just a few methods since all business logic should be represented as aggregate methods – in our case, just the methods to create a new package, to retrieve an existing package, and to delete an existing package. The logic to modify an existing package is included in the FullUpdate method of IPackage. |
21:1161 | Finally, as with all domain layer projects, PackagesManagementDomain contains an Events folder containing all domain event definitions. In our case, the folder is named Events and contains the package-deleted event and the price-changed event: |
21:1162 | public class PackageDeleteEvent: IEventNotification |
21:1163 | { |
21:1164 | public PackageDeleteEvent(int id, long oldVersion) |
21:1165 | { |
21:1166 | PackageId = id; |
21:1167 | OldVersion = oldVersion; |
21:1168 | } |
21:1169 | public int PackageId { get; } |
21:1170 | public long OldVersion { get; } |
21:1171 | |
21:1172 | } |
21:1173 | public class PackagePriceChangedEvent: IEventNotification |
21:1174 | { |
21:1175 | public PackagePriceChangedEvent(int id, decimal price, |
21:1176 | long oldVersion, long newVersion) |
21:1177 | { |
21:1178 | PackageId = id; |
21:1179 | NewPrice = price; |
21:1180 | OldVersion = oldVersion; |
21:1181 | NewVersion = newVersion; |
21:1182 | } |
21:1183 | public int PackageId { get; } |
21:1184 | public decimal NewPrice { get; } |
21:1185 | public long OldVersion { get; } |
21:1186 | public long NewVersion { get; } |
21:1187 | } |
21:1188 | |
21:1189 | When an aggregate sends all its changes to another application, it should have a version property. The application that receives the changes uses this version property to apply all changes in the right order. An explicit version number is necessary because changes are sent asynchronously, so the order in which they are received may differ from the order in which they were sent. For this purpose, events that are used to publish changes outside of the application have both OldVersion (the version before the change) and NewVersion (the version after the change) properties. Events associated with delete events have no NewVersion since, after being deleted, an entity can’t store any versions. |
21:1190 | For more details on how to use and process version information to restore the right order of incoming messages, please refer to the A worker microservice with ASP.NET Core section of this chapter. |
21:1191 | The next subsection explains how all interfaces defined in the domain layer abstraction are implemented in the domain layer implementation. |
21:1192 | Defining the domain layer implementation |
21:1193 | The data layer project contains references to the Microsoft.AspNetCore.Identity.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer NuGet packages, since we are using Entity Framework Core with SQL Server. It references Microsoft.EntityFrameworkCore.Tools and Microsoft.EntityFrameworkCore.Design, which are needed to generate database migrations, as explained in the Entity Framework Core migrations section of Chapter 13, Interacting with Data in C# – Entity Framework Core. |
21:1194 | We have a Models folder that contains all database entities. They are similar to the ones in Chapter 13, Interacting with Data in C# – Entity Framework Core. The only differences are as follows: |
21:1195 | |
21:1196 | They inherit from Entity<T>, which contains all the basic features of aggregates. Please note that inheriting from Entity<T> is only needed for aggregate roots; all other entities must be defined as explained in Chapter 7, Understanding the Different Domains in Software Solutions. In our example, all entities are aggregate roots. |
21:1197 | They have no Id since it is inherited from Entity<T>. |
21:1198 | Some of them have an EntityVersion property that is decorated with the [ConcurrencyCheck] attribute. It contains the entity version, which is essential for propagating all entity changes to other applications. The ConcurrencyCheck attribute is needed to prevent concurrency errors while updating the entity version. This prevents suffering the performance penalty implied by a transaction. |
21:1199 | |
21:1200 | More specifically, when saving entity changes, if the value of a field marked with the ConcurrencyCheck attribute is different from the one that was read when the entity was loaded in memory, a concurrency exception is thrown to inform the calling method that someone else modified this value after the entity was read but before we attempted to save its changes. This way, the calling method can repeat the whole operation with the hope that this time, no one will write the same entity in the database during its execution. |
21:1201 | The only alternative to the ConcurrencyCheck attribute would be: |
21:1202 | |
21:1203 | Start a transaction. |
21:1204 | Read the interested aggregate. |
21:1205 | Increment its EntityVersion property. |
21:1206 | Update the aggregate. |
21:1207 | Save all changes. |
21:1208 | Close the transaction. |
21:1209 | |
21:1210 | The transaction duration would be unacceptably long since the transaction should be maintained for the time of various database commands – namely, from the initial read to the final update – thus, preventing other requests from accessing the involved tables/records for too long a time. |
21:1211 | On the contrary, by using the ConcurrencyCheck attribute, we open just a very short single-command transaction when the aggregate is saved to the database: |
21:1212 | |
21:1213 | Read the interested aggregate. |
21:1214 | Increment the value of the EntityVersion property. |
21:1215 | Update the aggregate. |
21:1216 | Save all changes with a fast single-command transaction. |
21:1217 | |
21:1218 | It is worth analyzing the code of the Package entity: |
21:1219 | public class Package: Entity<int>, IPackage |
21:1220 | { |
21:1221 | public void FullUpdate(IPackageFullEditDTO o) |
21:1222 | { |
21:1223 | if (IsTransient()) |
21:1224 | { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.