Description
This issue has been moved from a ticket on Developer Community.
If I am mistaken, I would like to know. But I understand that Microsoft recommends Blazor programmers have a separate .razor, .cs, and .css files for each Razor component. The separate files separate concerns (SoC) and fulfill the single responsibility principle (SRP). I think even with a Razor file having HTML, CSS, and @code all in the same file, Blazor supports SoC and SRP. The automatically linking separate .cs and .css files to a .razor file carries SoC and SRP further.
However, what happens when you add a .razor file in Visual Studio Community is a contradiction. When you add a .razor file, you get just one file set up to contain CSS (you have to add <style>), HTML, and CS (@code). Furthermore, if you want the automatic linking in Blazor Webassembly, you have to uncomment a line in the index.html file.
To go with the higher level of SoC and SRP, you add a .cs file and a .css file in two additional steps. And then, you always have to add the word "partial" before the class keyword.
So, if Microsoft indeed promotes SoC and SRP to a higher degree, all three files should be added in one step when you add a .razor file. The keyword partial should already be inserted. You should have @code in the .razor file, but it should be commented out. If a programmer wants @code, they can comment it out. And the programmer can optionally optimize by deleting code-empty .css and .cs files. The automatic linking should not be commented out in the index.html file. Instead, it should be a comment explaining that you can disable the linking by commenting out or deleting "the following line." And, very importantly, CSS in the linked .css file should override styling in all Blazor built-in components.
But let us say Microsoft recommends using Blazor in a very simple way. Then, Visual Studio Community should do precisely what it does: Generate one .razor file that is ready to go. But it should also have <style></style> already included in the file. Then, programmers writing more complex code can enable the three separate files to link automatically.
I favor the second approach of promoting Blazor as a simple way to accomplish most things, with Blazor satisfying the needs of more advanced programming wherever needed or desired.
So, I would sum up that if Microsoft recommends the simple, all-in-one .razor file (when simple will do), Microsoft should express it more. Or Microsoft could keep the SoC and SRP as the well-known Microsoft recommendations and make some important enhancements ((1) Blazor components affected by CSS in a separate file, (2) get all three files ready to go when you add a .razor file).
Thank you for providing this feature request facility.
Original Comments
Feedback Bot on 12/17/2024, 06:17 AM:
Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.