loading...

CoreLMS: Custom Validation, Razor Pages, DTOs, and First UI Working!

fitzycodesthings profile image John "Fitzy" DeLancey Originally published at fct.dev ・3 min read

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

GitHub logo FitzyCodesThings / core-lms

An open source online learning management system project in ASP.Net Core MVC. Explores many best practices and patterns in modern software development.

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:

1) 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.
2) The Razor Pages model (one page:one feature) makes good sense for an app like this one.
3) 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

Posted on by:

fitzycodesthings profile

John "Fitzy" DeLancey

@fitzycodesthings

I'm a (primarily) DotNet developer and software consultant with 15+ years of experience. Lovin' live coding on Twitch.

Discussion

pic
Editor guide