Wednesday, March 28, 2018

ASP.NET Core Razor Pages & Separation of Concerns

On a recent post/thread regarding Razor Pages, there was a lot of concerns about spaghetti code and not following a proper presentation model that separates business logic out of the view and controller/viewmodel.

To be fair, the examples I have seen of Razor Pages and related articles that include data access code and other non View stuff in either the View or PageModel are not helping here and driving anxiety through the roof.

I think Razor Pages, however, can gracefully handle a proper Presentation Model and separation of concerns. In fact, it's the MVVM presentation model in spades. There are, however, no examples I have seen that break the "Model" part out when discussing Razor pages.

In a material over simplification, the approach is straightforward.

  1. Put your business logic (non View stuff) and business data in a proper Model

  2. Put your presentation logic and data shaped for presentation in the PageModel (aka ViewModel)

    Wrap the Model

    No references to the Page (View)

    The OnGet, OnPost and Handlers are not that conceptually different from how a UWP ViewModel responds to View events and commands. And yes, if the response requires business logic then your interacting with the Model.

  3. Put only your View stuff in the Page (aka the View)

    Two way bind to the PageModel (ViewModel)

If you have worked on UWP projects or Xamarin projects using Xamarin Forms, this will be easy pleasy for you. I don't see anything in Razor Pages that requires you to violate separation of concerns or that inhibits a proper presentation model approach.

Just my 2 cents. Peace

ASP.NET Core Razor Pages & Separation of Concerns Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team