title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
21:1325 | context.Database.Migrate() is useful to quickly set up and update staging and test environments. When deploying in production, if we don’t have the credentials for creating a new database or for modifying its structure, we can also produce an SQL script from the migrations using the migration tools. Then, this script should be examined before being applied by the person in charge of maintaining the database and, finally, applied with their credentials. |
21:1326 | To create migrations, we must add the aforementioned extension methods to the ASP.NET Core MVC Program.cs file, as shown here: |
21:1327 | ... |
21:1328 | builder.Services.AddRazorPages(); |
21:1329 | builder.Services.AddDbLayer( |
21:1330 | builder.Configuration.GetConnectionString(`DefaultConnection`), |
21:1331 | `PackagesManagementDB`); |
21:1332 | ... |
21:1333 | app.UseAuthentication(); |
21:1334 | app.UseAuthorization(); |
21:1335 | ... |
21:1336 | |
21:1337 | Please be sure that both the authorization and authentication middleware have been added to the ASP.NET Core pipeline in the right order; otherwise, the authentication/authorization engine will not work. |
21:1338 | Then, we must add the connection string to the appsettings.json file, as shown here: |
21:1339 | { |
21:1340 | `ConnectionStrings`: { |
21:1341 | `DefaultConnection`: `Server=(localdb)mssqllocaldb;Database=package-management;Trusted_Connection=True;MultipleActiveResultSets=true` |
21:1342 | }, |
21:1343 | ... |
21:1344 | } |
21:1345 | |
21:1346 | Finally, let’s add Microsoft.EntityFrameworkCore.Design to the ASP.NET Core project. |
21:1347 | At this point, let’s open the Visual Studio Package Manager Console, select PackageManagementDB as the default project, and then launch the following command: |
21:1348 | Add-Migration Initial -Project PackageManagementDB |
21:1349 | |
21:1350 | The preceding command will scaffold the first migration. We may apply it to the database with the Update-Database command. Please note that if you copy the project from GitHub, you don’t need to scaffold migrations since they have already been created, but you still need to update the database. |
21:1351 | In the next subsection, we will define the application layer that contains the business logic for manipulating the aggregates. |
21:1352 | Defining the application layer |
21:1353 | As a first step, for simplicity, let’s freeze the application culture to en-US by adding the following code to the ASP.NET Core pipeline: |
21:1354 | app.UseAuthorization(); |
21:1355 | // Code to add: configure the Localization middleware |
21:1356 | var ci = new CultureInfo(`en-US`); |
21:1357 | app.UseRequestLocalization(new RequestLocalizationOptions |
21:1358 | { |
21:1359 | DefaultRequestCulture = new RequestCulture(ci), |
21:1360 | SupportedCultures = new List<CultureInfo> |
21:1361 | { |
21:1362 | ci, |
21:1363 | }, |
21:1364 | SupportedUICultures = new List<CultureInfo> |
21:1365 | { |
21:1366 | ci, |
21:1367 | } |
21:1368 | }); |
21:1369 | |
21:1370 | Then, let’s create a Tools folder and place the ApplicationLayer code there, which you can find in the ch11 code of the GitHub repository associated with this book. With these tools in place, we can add the code that automatically discovers and adds all queries, command handlers, and event handlers to the DI engine, as shown here: |
21:1371 | ... |
21:1372 | ... |
21:1373 | builder.Services.AddAllQueries(this.GetType().Assembly); |
21:1374 | builder.Services.AddAllCommandHandlers(this.GetType().Assembly); |
21:1375 | builder.Services.AddAllEventHandlers(this.GetType().Assembly); |
21:1376 | |
21:1377 | Then, we must add a Queries folder to place all queries and their associated interfaces. As an example, let’s have a look at the query that lists all packages: |
21:1378 | public class PackagesListQuery(MainDbContext ctx) :IPackagesListQuery |
21:1379 | { |
21:1380 | public async Task<IReadOnlyCollection<PackageInfosViewModel>> GetAllPackages() |
21:1381 | { |
21:1382 | return await ctx.Packages.Select(m => new PackageInfosViewModel |
21:1383 | { |
21:1384 | StartValidityDate = m.StartValidityDate, |
21:1385 | EndValidityDate = m.EndValidityDate, |
21:1386 | Name = m.Name, |
21:1387 | DurationInDays = m.DurationInDays, |
21:1388 | Id = m.Id, |
21:1389 | Price = m.Price, |
21:1390 | DestinationName = m.MyDestination.Name, |
21:1391 | DestinationId = m.DestinationId |
21:1392 | }) |
21:1393 | .OrderByDescending(m=> m.EndValidityDate) |
21:1394 | .ToListAsync(); |
21:1395 | } |
21:1396 | } |
21:1397 | |
21:1398 | The query object is automatically injected into the application DB context. The GetAllPackages method uses LINQ to project all of the required information into PackageInfosViewModel and sorts all results in descending order on the EndValidityDate property. |
21:1399 | Projections that involve several properties are time-wasting and error-prone; that’s why there are mapping libraries that automatically generate these projections using naming conventions and configuration settings. Mapping libraries helps also in copying data from one object to another, such as, for example, from a ViewModel to a DTO, and vice versa. |
21:1400 | Among all mapping software, it is worth at least mentioning AutoMapper (https://www.nuget.org/packages/AutoMapper). |
21:1401 | PackageInfosViewModel is placed in the Models folder together with all other ViewModels. It is common practice to organize ViewModels into folders by defining a different folder for each controller. It is worth analyzing the ViewModel used for editing packages: |
21:1402 | public class PackageFullEditViewModel: IPackageFullEditDTO |
21:1403 | { |
21:1404 | public PackageFullEditViewModel() { } |
21:1405 | public PackageFullEditViewModel(IPackage o) |
21:1406 | { |
21:1407 | Id = o.Id; |
21:1408 | DestinationId = o.DestinationId; |
21:1409 | Name = o.Name; |
21:1410 | Description = o.Description; |
21:1411 | Price = o.Price; |
21:1412 | DurationInDays = o.DurationInDays; |
21:1413 | StartValidityDate = o.StartValidityDate; |
21:1414 | EndValidityDate = o.EndValidityDate; |
21:1415 | } |
21:1416 | ... |
21:1417 | ... |
21:1418 | |
21:1419 | It has a constructor that accepts an IPackage aggregate. This way, package data is copied into the ViewModel that is used to populate the edit view. It implements the IPackageFullEditDTO DTO interface defined in the domain layer. This way, it can be directly used to send IPackage updates to the domain layer. |
21:1420 | All properties contain validation attributes that are automatically used by client-side and server-side validation engines. Each property contains a Display attribute that defines the label to give to the input field that will be used to edit the property. It is better to place the field labels in the ViewModels than it is to place them directly into the views since, this way, the same names are automatically used in all views that use the same ViewModel. The following code block lists all its properties: |
21:1421 | public int Id { get; set; } |
21:1422 | [StringLength(128, MinimumLength = 5), Required] |
21:1423 | [Display(Name = `name`)] |
21:1424 | public string Name { get; set; }= null!; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.