title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
21:1727 | }); |
21:1728 | |
21:1729 | Also, add app.UseCors(); in the ASP.NET Core pipeline, immediately before app.UseAuthorization(); . |
21:1730 | Now, we must enable the Blazor application to communicate with this server. Launch the solution again and take note of the REST API URL (in my case, https://localhost:7269/), and then replace the URL in the HttpClient configuration in the Program.cs file of the Blazor application with this URL: |
21:1731 | builder.Services.AddScoped(sp => new HttpClient { |
21:1732 | BaseAddress = new Uri(`https://localhost:7269/`)}); |
21:1733 | |
21:1734 | Let’s also copy the same connection string of the old web project into the PackagesManagementBlazor.Server appsettings.json file: |
21:1735 | `ConnectionStrings`: { |
21:1736 | `DefaultConnection`: `Server=(localdb)mssqllocaldb;Database=package-management;Trusted_Connection=True;MultipleActiveResultSets=true` |
21:1737 | }, |
21:1738 | |
21:1739 | This way, we can reuse the database we have created. We also need to add the same DDD tools we added to the old web project. Add a folder named Tools in the project root and copy the contents of the ch07 -> ApplicationLayer folder of the GitHub repository associated with the book there. |
21:1740 | In order to finish the solution setup, we just need to connect PackagesManagementBlazor.Server with the domain layer by adding the following code at the end of the services configuration code in the Program.cs file: |
21:1741 | builder.services.AddDbLayer(Configuration |
21:1742 | .GetConnectionString(`DefaultConnection`), |
21:1743 | `PackagesManagementDB`); |
21:1744 | |
21:1745 | It is the same method we added to the old web project. Finally, we can also add the AddAllQueries extension method, which discovers all queries in the web project: |
21:1746 | builder.services.AddAllQueries(this.GetType().Assembly); |
21:1747 | |
21:1748 | We don’t need other automatic discovery tools since this is a query-only application. |
21:1749 | Due to a bug in Entity Framework Core 8, you also need to change a setting in the server project that prevents the usage of .NET culture. You must change the InvariantGlobalization project setting to false: |
21:1750 | <InvariantGlobalization>false</InvariantGlobalization> |
21:1751 | |
21:1752 | At this point, we have the project completely configured. We just need to implement server-side code and client-side code that implements our package search. |
21:1753 | The next subsection explains how to design the server-side REST API. |
21:1754 | Implementing the required ASP.NET Core REST APIs |
21:1755 | As the first step, let’s define the ViewModels used in the communication between the server and the client applications. They must be defined in the PackagesManagementBlazor.Shared project that is referenced by both applications. |
21:1756 | Let’s start with the PackageInfosViewModel ViewModel, which will be the data structure used by the Blazor application to exchange package info with the server-side REST API: |
21:1757 | using System; |
21:1758 | namespace PackagesManagementBlazor.Shared |
21:1759 | { |
21:1760 | public class PackageInfosViewModel |
21:1761 | { |
21:1762 | public int Id { get; set; } |
21:1763 | public required string Name { get; set; } |
21:1764 | public decimal Price { get; set; } |
21:1765 | public int DurationInDays { get; set; } |
21:1766 | public DateTime? StartValidityDate { get; set; } |
21:1767 | public DateTime? EndValidityDate { get; set; } |
21:1768 | public required string DestinationName { get; set; } |
21:1769 | public int DestinationId { get; set; } |
21:1770 | public override string ToString() |
21:1771 | { |
21:1772 | return $`{Name}. {DurationInDays} days in {DestinationName}, price: {Price}`; |
21:1773 | } |
21:1774 | } |
21:1775 | } |
21:1776 | |
21:1777 | Then, add the ViewModel that encloses all packages to return to the Blazor application: |
21:1778 | using System.Collections.Generic; |
21:1779 | namespace PackagesManagementBlazor.Shared |
21:1780 | { |
21:1781 | public class PackagesListViewModel |
21:1782 | { |
21:1783 | Public required ReadOnlyCollection<PackageInfosViewModel> |
21:1784 | Items { get; set; } |
21:1785 | } |
21:1786 | } |
21:1787 | |
21:1788 | Now, we can also add our query that searches packages by location. Let’s add a Queries folder in the root of the PackagesManagementBlazor.Server project and then add the interface that defines our query, IPackagesListByLocationQuery: |
21:1789 | using DDD.ApplicationLayer; |
21:1790 | using PackagesManagementBlazor.Shared; |
21:1791 | using System.Collections.Generic; |
21:1792 | using System.Threading.Tasks; |
21:1793 | namespace PackagesManagementBlazor.Server.Queries |
21:1794 | { |
21:1795 | public interface IPackagesListByLocationQuery: IQuery |
21:1796 | { |
21:1797 | Task<ReadOnlyCollection<PackageInfosViewModel>> |
21:1798 | GetPackagesOf(string location); |
21:1799 | } |
21:1800 | } |
21:1801 | |
21:1802 | Finally, let’s also add the query implementation: |
21:1803 | public class PackagesListByLocationQuery(MainDbContext ctx):IPackagesListByLocationQuery |
21:1804 | { |
21:1805 | |
21:1806 | public async Task<ReadOnlyCollection<PackageInfosViewModel>> |
21:1807 | GetPackagesOfAsync(string location) |
21:1808 | { |
21:1809 | Return new ReadOnlyCollection<PackageInfosViewModel> |
21:1811 | .Where(m => m.MyDestination.Name.StartsWith(location)) |
21:1812 | .Select(m => new PackageInfosViewModel |
21:1813 | { |
21:1814 | StartValidityDate = m.StartValidityDate, |
21:1815 | EndValidityDate = m.EndValidityDate, |
21:1816 | Name = m.Name, |
21:1817 | DurationInDays = m.DurationInDays, |
21:1818 | Id = m.Id, |
21:1819 | Price = m.Price, |
21:1820 | DestinationName = m.MyDestination.Name, |
21:1821 | DestinationId = m.DestinationId |
21:1822 | }) |
21:1823 | .OrderByDescending(m=> m.EndValidityDate) |
21:1824 | .ToListAsync()); |
21:1825 | } |
21:1826 | } |
21:1827 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.