Skip to content

Blazor snippet samp and NRT work#24248

Merged
guardrex merged 5 commits into
mainfrom
guardrex/blazor-snippet-samp-and-nrt-work
Dec 14, 2021
Merged

Blazor snippet samp and NRT work#24248
guardrex merged 5 commits into
mainfrom
guardrex/blazor-snippet-samp-and-nrt-work

Conversation

@guardrex
Copy link
Copy Markdown
Collaborator

@guardrex guardrex commented Dec 9, 2021

Fixes #23064

@guardrex guardrex changed the title Guardrex/blazor snippet samp and nrt work Blazor snippet samp and NRT work Dec 9, 2021
@guardrex
Copy link
Copy Markdown
Collaborator Author

guardrex commented Dec 14, 2021

@JeremyLikness ... I'm wrapping up all of the Blazor NRT bits with this PR 😅.

Recall that I hit a problem with one NRT warning in the EFCore-Blazor Server sample app on the earlier PR that addressed sample app NRTs. I punted 🏈🦶 on it at the time 🏃. The NRT warning is in the Index component for deleting a contact ...

private async Task DeleteContactAsync()
{
    using var context = DbFactory.CreateDbContext();
    Filters.Loading = true;

    if (Wrapper is not null)
    {
        var contact = await context.Contacts?.FirstAsync(c => c.Id == Wrapper.DeleteRequestId);

        if (contact != null)
        {
            context.Contacts?.Remove(contact);
            await context.SaveChangesAsync();
        }
    }

    Filters.Loading = false;

    await ReloadAsync();
}

Cross-ref: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/samples/6.0/BlazorServerEFCoreSample/BlazorServerDbContextExample/Pages/Index.razor#L200-L220

The NRT warning is on the line ...

var contact = await context.Contacts?.FirstAsync(c => c.Id == Wrapper.DeleteRequestId);

By avoiding the use of a null conditional operator on context.Contacts and going with an old skool null check, it's happy with the following ...

private async Task DeleteContactAsync()
{
    using var context = DbFactory.CreateDbContext();
    Filters.Loading = true;

    if (Wrapper is not null && context.Contacts is not null)
    {
        var contact = await context.Contacts.FirstAsync(c => c.Id == Wrapper.DeleteRequestId);

        if (contact is not null)
        {
            context.Contacts?.Remove(contact);
            await context.SaveChangesAsync();
        }
    }

    Filters.Loading = false;

    await ReloadAsync();
}

That works when running the app. Does that seem ok to you? The prior code runs fine, but it's probably best if we perform the update to avoid doc issues on the warning.

UPDATE: I'm going to go ahead with that to get this merged. I'm trying to wrap up some things before BUGGIN' OUT ⛄. Let me know if you need a change to that ☝️ bit that I changed on the PR for DeleteContactAsync. 👂

@guardrex guardrex marked this pull request as ready for review December 14, 2021 13:52
@guardrex guardrex closed this Dec 14, 2021
@guardrex guardrex reopened this Dec 14, 2021
@guardrex guardrex merged commit 71789d0 into main Dec 14, 2021
@guardrex guardrex deleted the guardrex/blazor-snippet-samp-and-nrt-work branch December 14, 2021 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Place 6.0 Blazor code block examples into Blazor snippet sample apps

1 participant