Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor CRUD scaffolding updates #2743

Open
guardrex opened this issue May 13, 2024 · 18 comments
Open

Blazor CRUD scaffolding updates #2743

guardrex opened this issue May 13, 2024 · 18 comments

Comments

@guardrex
Copy link

guardrex commented May 13, 2024

I'm going to place everything that I find in this one issue (several comments). If you want me 🦖 to PR the updates, I can do it after I'm done writing a large tutorial that makes use of this scaffolding in a few weeks.

Should the semicolons in the Block entries of the following file be there?

I think I received a double semicolon (unless it was due to a stray keystroke on my part 😈) ...

builder.Services.AddQuickGridEntityFrameworkAdapter();;

Nevermind! The double-semicolon seems to have been a stray keystroke on my part.

@guardrex
Copy link
Author

guardrex commented May 13, 2024

IGNORE (probably)

VS is suggesting a primary ctor, so this code will go away. See my comment at ...

#2743 (comment)

In this file ...

public @Model.DbContextTypeName (DbContextOptions<@Model.DbContextTypeName> options)

Shouldn't ...

public @Model.DbContextTypeName (DbContextOptions<@Model.DbContextTypeName> options)

... be ...

public @(Model.DbContextTypeName)(DbContextOptions<@Model.DbContextTypeName> options)

... because I see a stray space in the code ...

public BlazorWebAppMoviesContext (DbContextOptions<BlazorWebAppMoviesContext> options)

@guardrex guardrex changed the title Double semicolon for scaffolding into a Blazor Web App Blazor CRUD scaffolding updates May 16, 2024
@guardrex
Copy link
Author

More NITs 😈 ...

@guardrex
Copy link
Author

In the file ...

https://github.com/dotnet/Scaffolding/blob/main/src/Scaffolding/VS.Web.CG.Mvc/Templates/Blazor/Edit.tt

... the model isn't lowercased to match what's done for the other components of the set ...

- @page "/<#= pluralModel #>/edit"
+ @page "/<#= pluralModelLowerInv #>/edit"

Because of it, the path comes out (for example using a movies database example) ...

@page "/Movies/edit"

... instead of the expected ...

@page "/movies/edit"

@guardrex
Copy link
Author

guardrex commented May 16, 2024

The following cross-links will need to be removed because TryUpdateModelAsync, which is a focus of the cross-linked content, is Mvc.ControllerBase/RazorPages.PageBase API of MVC/RP and not available for Razor components to use.

WRT documentation coverage: The Blazor Web App tutorial (in progress) will touch on overposting with a short section, and I think we're going to have a special Blazor static SSR form overposting section in our Blazor forms article coverage. If so, I'll open a follow-up issue on this repo to get a new link placed that points to the new coverage.

UPDATE

Opened ...

Add Blazor forms overposting coverage
dotnet/AspNetCore.Docs#32733

... to place Blazor-specific overposting coverage. After coverage goes live, I'll open a new scaffolder repo issue to get the Blazor cross-links into the Blazor bits here and remove the main doc set cross-link. The main doc set coverage will be linked via the Blazor coverage, so devs will still be able to reach it for more info.

@guardrex

This comment was marked as resolved.

@danroth27
Copy link
Member

Unfortunately, I don't think we ever formalized our Blazor style guide, but in product code (templates, scaffolding, etc) I think we generally only prefix with @ when required. I wouldn't jump to update the docs to do this just yet though.

@guardrex
Copy link
Author

There are also extra spaces in the Razor markup files of the CRUD components.

@guardrex
Copy link
Author

guardrex commented Jun 3, 2024

Missing private access modifier from the Edit component's {MODEL NAME}Exists method, which is explicitly stated for doc coverage by convention ...

bool <#= modelName #>Exists(<#= primaryKeyShortTypeName #> <#= primaryKeyNameLowerInv #>)

- bool <#= modelName #>Exists(<#= primaryKeyShortTypeName #> <#= primaryKeyNameLowerInv #>) 
+ private bool <#= modelName #>Exists(<#= primaryKeyShortTypeName #> <#= primaryKeyNameLowerInv #>) 

@guardrex
Copy link
Author

guardrex commented Jun 4, 2024

I'm not sure where this is located (probably over in the main repo actually), but there isn't a return on the last line of the appsettings.json file ...

image

I'll go look now and see if this is in the project template over there. BRB ....................

The serviceDependencies.json file has the same problem ...

image

... and this one ...

image

Those are the only ones. Every other file is fine.

I'm not sure where those are located, here or back on the main repo. I'll leave this comment here in case the PU engineer wants to hunt them down and put in a fix for them.

@guardrex
Copy link
Author

"namespace" is misspelled in three spots ...

https://github.com/search?q=repo%3Adotnet%2FScaffolding%20namesapce&type=code

@guardrex
Copy link
Author

guardrex commented Jun 17, 2024

Not related to Blazor, but something that I'll pile on here WRT the minimalapi generator ...

The help list of options doesn't look right ...

Selected Code Generator: minimalapi

Generator Arguments:
  -e|--endpoints                  : Endpoints class to use. (not file name)
  -m|--model                      : Model class to use
  -dc|--dataContext               : DbContext class to use
  -outDir|--relativeFolderPath    : Specify the relative output folder path from project where the file needs to be generated, if not specified, file will be generated in the project folder
  -o|--open                       : Use this option to enable OpenAPI
  -namespace|--endpointsNamespace : Specify the name of the namespace to use for the generated Endpoints file
  databaseProvider                : Database provider to use. Options include 'sqlserver' (default), 'sqlite', 'cosmos', 'postgres'.
  useSqLite                       : Flag to specify if DbContext should use SQLite instead of SQL Server.

... the last two (databaseProvider and useSqLite) don't have the proper command formats and don't have a short name command.

@guardrex
Copy link
Author

Two of the components cross-link for more information on overposting ...

// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD

Cross-refs:

However, that's not a very helpful cross-link because it lands here ...

https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/crud?view=aspnetcore-8.0#update-the-create-page

... with API and an approach that isn't available in a Razor component scenario.

We now have a new overposting section in the Blazor Forms overview article. Update the Create and Edit templates to point to the Blazor content. Here's the doc URL:

https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks

... and I can't create an aka.ms address for it. If an aka.ms address is created by someone, make sure that it's version agnostic and without a culture segment.

@deepchoudhery
Copy link
Member

Hey @guardrex, thanks a lot for the incoming feedback, looking at it currently and fixing the templates accordingly.

@guardrex
Copy link
Author

guardrex commented Jul 11, 2024

@deepchoudhery ... Nevermind on the comment that I left here. I forgot for a moment that we're changing our documentation descriptions and examples to avoid prefixing nonliterals with @, so what you have in the scaffolding code is fine 👍 ...

<QuickGrid Class="table" Items="DbFactory.CreateDbContext().{CLASS}">

... is correct, no @ on DbFactory.CreateDbContext().{CLASS}.

I'm still getting used to this because we've gone about five years the other way. Old habits are hard to break! 😄

@deepchoudhery
Copy link
Member

All good, going to go through this issue again soon and catalog remaining changes into a comment for easier tracking.

@guardrex
Copy link
Author

guardrex commented Jul 12, 2024

Found another one ... there's a blank line after the opening @page directive. Although many community devs do that, we don't do it in Blazor documentation. I recommend that you collapse that down to a single block of Razor directives.

This extra line is in all of the component files: Index, Create, Edit, Delete, and Details.

- @page "/movies"
- 
- @using Microsoft.EntityFrameworkCore
- @using Microsoft.AspNetCore.Components.QuickGrid
- @using BlazorWebAppMovies.Models
- @inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory
+ @page "/movies"
+ @using Microsoft.EntityFrameworkCore
+ @using Microsoft.AspNetCore.Components.QuickGrid
+ @using BlazorWebAppMovies.Models
+ @inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory

@guardrex
Copy link
Author

guardrex commented Aug 14, 2024

@danroth27 ... I'm not sure if we're proceeding with the tutorial updates because not all of the scaffolding updates are done yet. If they're worked piecemeal, I need to go back into the tutorial files repeatedly (or again after 9.0 GA) ... it's time consuming to repeatedly revisit it ... thus costly 💰.

Nevermind on the next bit ... the packages updated right after I mentioned it ........

Also, I just ran the scaffolder after a Pre7 SDK/VS updates to find the scaffolded code hasn't changed yet ...

<QuickGrid Class="table" Items="DbFactory.CreateDbContext().Movie">
    ...
</QuickGrid>

I see the template file is updated ...

https://github.com/dotnet/Scaffolding/blob/main/src/Scaffolding/VS.Web.CG.Mvc/Templates/Blazor/Index.tt

When will the package(s) update?

@guardrex
Copy link
Author

VS static analysis is suggesting a primary ctor in Data/BlazorWebAppMoviesContext.cs ...

image

public class BlazorWebAppMoviesContext(DbContextOptions<BlazorWebAppMoviesContext> options) : DbContext(options)
{
    public DbSet<BlazorWebAppMovies.Models.Movie> Movie { get; set; } = default!;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants