C
C#2mo ago
Fwedi

Trying to understand MVVM and MVC. Blazor ASP.NET

I'm trying to desgin a web portal using blazor but I can't really wrap my head around how I could code it. Mainly how exactly I could connect the database and the front end. Sorry if I'm asking too much. How could I make use of design patterns like mvc and mvvm to help me out with this? Is one strictly better or more modern or are they both useful and practical? Do I have to pick between the two or is it possible to use both patterns at the same time? Is it possible to have a component that displays a table based on what I query the database? Like for example: If I query the database to "select col1, col2 from A" or to "select col3, col4, col5 from B" Would it be possible to have one dynamic component thst can receive the result of both queries and display the data?
1 Reply
Dusty
Dusty2mo ago
It depends what you are talking about, you're mixing up a lot of frameworks and design patterns. - MVVM (Model View Viewmodel) is a design pattern that is usually used in WPF (Windows Presentation Forms) apps, so Windows Desktop Apps - MVC (Model View Controller) is an old design pattern that is often used in server side renderered web apps where you use controllers and views. ASP.NET Core has an Implementation for that Now to blazor: If you wanna build a modern web app, no matter if you have no interactivity, a lot of interactivity or just a little bit, you should use it. However there are a few different versions of blazor: - Blazor Server: Everything is computed on the server and the client needs an active websocket connection to the server. It has some drawbacks like delays under load e.g. if user x does action y the browser has to send the request to the server, the server has to compute it and send back HTML over the websocket - Blazor WASM (Web Assembly): This is your usual SPA (Single Page Application) approach. It works like other JS frameworks (React, VueJS, Angular, Svelte etc.). It's entirely running on the browser via web assembly and gets rendered client side. If you want to get data from a database for example you need to build your own API (usually a RESTful API) for your resources/models which returns JSON responses of them. On the client you send requests to the API, parse the data and render it. You can do the same with Blazor Server, it also acts as an SPA but is implemented not like the other typical SPA frameworks. - Blazor Web: This is the newest version of Blazor, which supports dynamic rendering on the server or client on a per component level. For new projects this is the one suggested to use. I've personally used it at my company but we've switched back to blazor WASM as we've encountered too many drawbacks/over complicated stuff for a production ready app. So depending on what you build I would suggest Blazor Web or Blazor WASM.