-
Notifications
You must be signed in to change notification settings - Fork 94
RazorPageApp
The GitHub repo contains an example ASP.NET Core Razor Pages application, in the project RazorPageApp.
You can look at the code, or run the web application locally by cloning the repo and pressing F5.
NOTE: This web application uses a SQLite in-memory database so that it will run anywhere.
The EF Core database, in the DataLayer has two key entity classes mapped to a database:
-
Book
entity which is set up as a DDD-styled class, with create/update done via methods inside the class. The RazorPageApp accesses this class, and its aggregates, via GenericServices and DTOs. -
Author
entity which is a standard-styled class, with create/update done via property setters. The RazorPageApp accesses this class directly, i.e. it uses the classAuthor
directly.
RazorPageApp gives you examples of most of the GenericServices' commands using both direct access and via DTOs. Here is a list of the CRUD pages, and where you can find the code.
Here are two quite different ways to create a new entry in the database.
A new Author
entity is created directly by you creating the entity class directly and asking GenericServices to add it to the database, while a new Book
entity, which is much more complex to set up, is created using a static method.
Here is the links to the code
- Create
Author
- Authors/Create.cshtml.cs using the entity class. - Create
Book
- Home/Create.cshtml.cs - this uses the DTO calledCreateBookDto.cs
This shows how to use the ReadManyNoTracked<T>
command, which builds an EF Core Select
query to show
data to the user. Inside GenericServices the Select
queries are build using
AutoMapper.
- ReadMany
Author
- Authors/Index.cshtml.cs. This uses the simple DTO calledAuthorWithBookCountDto
. - ReadMany
Book
- Home/Index.cshtml.cs - this uses the DTO calledBookListDto.cs
. Because the query is so complex you need to provide extra information to AutoMapper, which you can do via aPerDtoConfig
file, in this case calledBookListDtoConfig
.
While the Author
update is pretty simple the Book
entity has a number of update commands.
I suggest you look at some of them to see what they do.
- Update
Author
- See Authors/Edit. - Update
Book
- Update publication date Home/ChangePubDate.cshtml.cs.
- Add review Home/AddReview.cshtml.cs.
- Add a price promotion Home/AddPromotion.cshtml.cs.
- Remove price promotion Home/RemovePromotion.cshtml.cs.
*Note: The last one, RemovePromotion, needs to define which method to call because there are no properties to provide,
so it can't work out that the RemovePromotion
method needs to be called.
Deleting seems simple, but it gets complex when you think about the relationships. The two examples, delete an author and delete a book, both have potential relationships that can change things. See the code for more explanations.
- Delete
Author
- see Authors/Delete.cshtml.cs. This stops the delete if the author has any books. - Delete
Book
- see Home/Delete.cshtml.cs. This would throw an exception if you tried to delete a book that was in an customer's order.
Many times you will need to write direct EF Core code to setup other parts of the display or the entity being updated.
You can access the instance of the DbContext vai GenericServices' Context
property.
You can see this in Home/Create.cshtml.cs code.