Are DTOs useful in Blazor?

G in Sydney 1 Reputation point
2021-09-20T01:49:12.773+00:00

I'm just learning Blazor and trying to understand what the role of Data Transfer Objects (DTOs) between the Data Access Layer (DAL) and the GUI would play in either of the hosting models (Blazor Server and Blazor WebAssembly). For the sake of example, lets assume the DAL accesses a database via EF. I'd appreciate feedback on my thoughts and rationale below for each hosting model.

Blazor Server - DTOs probably not needed for most entities.
Blazor Server handles all the processing, including the rendering, on the server side. It communicates with the web browser using Signal R and is smart enough to only send the rendered elements it needs. Am I right in assuming that this means that, if I have a class with a large number of properties, but I only have, say, two bound to a form on the webpage, the server only ever renders the value of the two properties and sends that to the website over the internet, not all the properties, so unnecessary data transmission is already handled by Blazor Server. Note: I can see that, for a class with a property which holds confidential data eg password, social security number etc, a DTO would make sense to ensure that that data couldn't be exposed on a webpage, but that would be the exception rather than the rule.

Blazor WebAssembly - DTOs probably not needed for most entities in the Blazor app itself but may need to take out the DAL from the Blazor app and convert into an API and use this in the WebAssembly to access the data.
In the Blazor WebAssembly, the server and the client are both hosted in the browser, so communication between them is internal, not over a web connection, meaning the impact of passing unneeded information should be much less. Even then, if the client-server relationship works in the same way as Blazor Server, but with the communication now internal, wouldn't the data being transferred still only be the values to be rendered, not all the properties? And even if you did use a DTO in the WebAssembly program for a class with a confidential property, the original class including that property is still loaded in the server program in the browser.
The problem I do see is that, if you include the DAL in the WebAssembly, all your database calls from EF could pull in all the information for a class, not just the information you want to use, adding to traffic volumes and slowing the app down. The only way around this would be to take out the DAL and convert it to an API and access that from WebAssembly. Note: in the Blazor Server model, I am assuming the database accessed by the DAL is hosted locally to the server and therefore the impact of transmitting all data for a class to and from the server doesn't have a major impact on GUI response time.

Apologies if I'm over simplifying and misunderstand both Blazor and the application of DTOs. Thanks for any help you can give.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,500 questions
{count} votes