Made some good progress this week.
Most of it was OFF stream, but hey. Thatās what the record buttonās for. š¤£
Started by dropping in some custom validation logic Iāve been messing with, then replaced the MVC project with a Razor Pages project (and fumbled around with scaffolding forever), then took a break and figured out how to tackle that issue, drop in DTOs, and get the first UI actually working.
Get the Git
Check out the project on Github
Watch the Replay Redemption Video
Custom Validation
If youāve been following along, you may remember from waaaay back in August (23 years ago I believe) that I was wrestling with how to implement validation across the system in a way that repeated as little code as possible.
After some experimenting, I landed on an approach that Iām happy with (inspired by Hassan Habibās OtripleS project as so many things I do are).
The approach lets us utilize the built in Data Annotations validation feature thatās used automatically by ASP.Net Core, but in our business logic and in a unit testable way.
I demonstrate how the logic works (along with the tests, of course) in this Twitch highlight.
Getting Razor Pages Going
Next up, I replaced the MVC project with a Razor Pages project.
I had a few reasons for going this direction:
- Razor Pages is the āfirst class citizenā in ASP.Net Core world these days. MVC is 1000% still viable, etc., but I wanted to try the shiny new thing.
- The Razor Pages model (one page:one feature) makes good sense for an app like this one.
- I thought itād be easier to deal with the DTO/ViewModel thing (I was mistaken, but only a little).
Anyhoo, I got the project started, dropped in the Bootstrap 4 demo for an admin dashboard, and then I wrestled with getting Razor pages scaffolding to work for far too long on stream.
Turned out it was a stupid bug on my part. See the vid above for details.
Managed to get scaffolding working (very useful since Iāve never written with Razor Pages), then dove into what was finally clicking on DTOs.
Adding DTOs to the Mix
As I was dealing with getting my Razor Pages PageModel situated like I liked it, it finally hit me how DTOs should fit into my mix, at least.
I definitely didnāt want to pass and model bind the actual core entity object, of course. This opens you up to issues like overposting attacks, exposed data, etc. So I started building a ViewModel.
While I was typing in the manual mapping code to go from entity to ViewModel, it finally occured to me that I shouldnāt be doing that mapping in the UI layer itself: it should be mapped in the business logic layer** to keep things DRY and the logic centralized.
But I didnāt want to pass a UI-level ViewModel to my service, so how do Iā¦
Oh. THATās what a DTO is good for. š” š¤¦āāļø
That light bulb moment now shining brightly in my head, I added my first DTOs (CreateCourseDto
and UpdateCourseDto
), refactored my service methods (tests go red), fixed the tests (tests go green), and had a satisfied stretch break before moving back to the UI.
First Working UI!
DTOs and validation now in place and ready to rock, I moved back to the Razor Page now scaffolded and awaiting my updates. I replaced the direct entity and DbContext access with my DTOs and the CourseService
Iāve been building, fired it up, and lo and behold, it worked. Courses created.
A very successful week indeed.
Eventually. š¤£
Wrapping Up
These big decisions finally handled, Iām excited to pick up the pace and start really building things out.
I found a great opensource Admin Dashboard template for Bootstrap 4 that Iām gonna work on next week, then start fleshing out the rest of our Course creation/editing/management processes.
Until next time folks!
- Fitzy