From 669c9ae4041df7447bd1fb1ae4b75f8c8aad6f64 Mon Sep 17 00:00:00 2001 From: Vincent Baaij Date: Thu, 30 Jan 2025 16:33:59 +0100 Subject: [PATCH 1/3] Implement feature request #3269 --- .../EntityFrameworkAsyncQueryExecutor.cs | 15 +++++++++++++-- .../DataGrid.EntityFrameworkAdapter/README.md | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs index 614abccad4..8b1877d1ab 100644 --- a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs +++ b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs @@ -1,23 +1,34 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query; using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure; namespace Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter; -internal class EntityFrameworkAsyncQueryExecutor : IAsyncQueryExecutor, IDisposable +/// +/// An implementation for Entity Framework Core. +/// +public class EntityFrameworkAsyncQueryExecutor : IAsyncQueryExecutor, IDisposable { private readonly SemaphoreSlim _lock = new(1); + /// public bool IsSupported(IQueryable queryable) => queryable.Provider is IAsyncQueryProvider; + /// public Task CountAsync(IQueryable queryable, CancellationToken cancellationToken) => ExecuteAsync(() => queryable.CountAsync(cancellationToken)); + /// public Task ToArrayAsync(IQueryable queryable, CancellationToken cancellationToken) => ExecuteAsync(() => queryable.ToArrayAsync(cancellationToken)); - private async Task ExecuteAsync(Func> operation) + /// + protected virtual async Task ExecuteAsync(Func> operation) { try { diff --git a/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md b/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md index 2cdec74d6d..f259a22b8f 100644 --- a/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md +++ b/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md @@ -29,12 +29,25 @@ Install the package by running the command: dotnet add package Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter ``` +## Creating a derived AsyncQueryExecutor implementation +Starting with v4.11.4, the `EntityFrameworkAsyncQueryExecutor` class is made public +so that you can derive from it and override the `ExecuteAsync` method to provide +custom query execution logic. This is useful if you want to add custom query processing +logic, such as logging, caching, or query translation or if you want specific [error handling](https://github.com/microsoft/fluentui-blazor/issues/3269). + ## Usage -In your Program.cs file you need to add the following: +When using the provided implementation, you need to add add the following in the `Program.cs` file: + ``` builder.Services.AddDataGridEntityFrameworkAdapter(); ``` +When using a custom implementation, you need to add this custom implementation to the DI container in the `Program.cs` file yourself. +You do **not** call `AddDataGridEntityFrameworkAdapter` in this case. +``` +builder.Services.AddScoped(); +``` + ## Support The Microsoft Fluent UI Blazor library is an open source project and is **not** an official part of ASP.NET Core, which means it’s **not** officially supported and isn’t committed to ship updates as part of any official .NET updates. It is built and maintained by Microsoft employees (**and** other contributors) From 57b5fc98bf00b2b11ee6b57414031dec01000c19 Mon Sep 17 00:00:00 2001 From: Vincent Baaij Date: Fri, 7 Feb 2025 15:09:19 +0100 Subject: [PATCH 2/3] Implement alternative solution to making EntityFrameworkAsyncQueryExecutor inheritable in order to be able to handle specific Exceptions. Thanks @miguelhasse --- ...ityFrameworkAdapterServiceCollectionExtensions.cs | 11 +++++++++-- .../EntityFrameworkAsyncQueryExecutor.cs | 9 ++++++--- .../ODataAdapterServiceCollectionExtensions.cs | 8 +++++++- .../DataGrid.ODataAdapter/ODataAsyncQueryExecutor.cs | 12 +++++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAdapterServiceCollectionExtensions.cs b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAdapterServiceCollectionExtensions.cs index cadf2921ef..536e832413 100644 --- a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAdapterServiceCollectionExtensions.cs +++ b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAdapterServiceCollectionExtensions.cs @@ -1,7 +1,13 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + using Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter; using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace Microsoft.Extensions.DependencyInjection; +#pragma warning restore IDE0130 // Namespace does not match folder structure /// /// Provides extension methods to configure on a . @@ -12,8 +18,9 @@ public static class EntityFrameworkAdapterServiceCollectionExtensions /// Registers an Entity Framework aware implementation of . /// /// The . - public static void AddDataGridEntityFrameworkAdapter(this IServiceCollection services) + /// A function to determine whether to ignore exceptions. + public static void AddDataGridEntityFrameworkAdapter(this IServiceCollection services, Func? ignoreException = null) { - services.AddScoped(); + services.AddScoped(sp => new EntityFrameworkAsyncQueryExecutor(ignoreException)); } } diff --git a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs index 8b1877d1ab..e99ab9d97e 100644 --- a/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs +++ b/src/Extensions/DataGrid.EntityFrameworkAdapter/EntityFrameworkAsyncQueryExecutor.cs @@ -11,7 +11,7 @@ namespace Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapt /// /// An implementation for Entity Framework Core. /// -public class EntityFrameworkAsyncQueryExecutor : IAsyncQueryExecutor, IDisposable +internal class EntityFrameworkAsyncQueryExecutor(Func? ignoreException = null) : IAsyncQueryExecutor, IDisposable { private readonly SemaphoreSlim _lock = new(1); @@ -27,8 +27,7 @@ public Task CountAsync(IQueryable queryable, CancellationToken cancel public Task ToArrayAsync(IQueryable queryable, CancellationToken cancellationToken) => ExecuteAsync(() => queryable.ToArrayAsync(cancellationToken)); - /// - protected virtual async Task ExecuteAsync(Func> operation) + private async Task ExecuteAsync(Func> operation) { try { @@ -47,6 +46,10 @@ protected virtual async Task ExecuteAsync(Func> { return default!; } + catch (Exception ex) when (ignoreException?.Invoke(ex) == true) + { + return default!; + } } void IDisposable.Dispose() => _lock.Dispose(); diff --git a/src/Extensions/DataGrid.ODataAdapter/ODataAdapterServiceCollectionExtensions.cs b/src/Extensions/DataGrid.ODataAdapter/ODataAdapterServiceCollectionExtensions.cs index 9115d25956..08acd41336 100644 --- a/src/Extensions/DataGrid.ODataAdapter/ODataAdapterServiceCollectionExtensions.cs +++ b/src/Extensions/DataGrid.ODataAdapter/ODataAdapterServiceCollectionExtensions.cs @@ -1,7 +1,13 @@ -using Microsoft.FluentUI.AspNetCore.Components.DataGrid.ODataAdapter; +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure; +using Microsoft.FluentUI.AspNetCore.Components.DataGrid.ODataAdapter; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace Microsoft.Extensions.DependencyInjection; +#pragma warning restore IDE0130 // Namespace does not match folder structure /// /// Provides extension methods to configure on a . diff --git a/src/Extensions/DataGrid.ODataAdapter/ODataAsyncQueryExecutor.cs b/src/Extensions/DataGrid.ODataAdapter/ODataAsyncQueryExecutor.cs index 4ca709a4b4..433e791757 100644 --- a/src/Extensions/DataGrid.ODataAdapter/ODataAsyncQueryExecutor.cs +++ b/src/Extensions/DataGrid.ODataAdapter/ODataAsyncQueryExecutor.cs @@ -1,15 +1,25 @@ -using Microsoft.OData.Client; +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure; +using Microsoft.OData.Client; namespace Microsoft.FluentUI.AspNetCore.Components.DataGrid.ODataAdapter; +/// +/// An implementation for Microsoft.OData.Client. +/// internal class ODataAsyncQueryExecutor : IAsyncQueryExecutor { + /// public bool IsSupported(IQueryable queryable) => queryable.Provider is DataServiceQueryProvider; + /// public async Task CountAsync(IQueryable queryable, CancellationToken cancellationToken) => (int)(await ExecuteAsync(((DataServiceQuery)queryable.Take(0)).IncludeCount(), cancellationToken)).Count; + /// public async Task ToArrayAsync(IQueryable queryable, CancellationToken cancellationToken) => [.. await ExecuteAsync(queryable, cancellationToken)]; From e0beab8b7e29447f9bfd6d670bda31f8f41260e1 Mon Sep 17 00:00:00 2001 From: Vincent Baaij Date: Fri, 7 Feb 2025 15:18:39 +0100 Subject: [PATCH 3/3] Edit readme --- .../DataGrid.EntityFrameworkAdapter/README.md | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md b/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md index f259a22b8f..fc5a732b29 100644 --- a/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md +++ b/src/Extensions/DataGrid.EntityFrameworkAdapter/README.md @@ -28,13 +28,6 @@ Install the package by running the command: ``` dotnet add package Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter ``` - -## Creating a derived AsyncQueryExecutor implementation -Starting with v4.11.4, the `EntityFrameworkAsyncQueryExecutor` class is made public -so that you can derive from it and override the `ExecuteAsync` method to provide -custom query execution logic. This is useful if you want to add custom query processing -logic, such as logging, caching, or query translation or if you want specific [error handling](https://github.com/microsoft/fluentui-blazor/issues/3269). - ## Usage When using the provided implementation, you need to add add the following in the `Program.cs` file: @@ -42,12 +35,20 @@ When using the provided implementation, you need to add add the following in the builder.Services.AddDataGridEntityFrameworkAdapter(); ``` -When using a custom implementation, you need to add this custom implementation to the DI container in the `Program.cs` file yourself. -You do **not** call `AddDataGridEntityFrameworkAdapter` in this case. -``` -builder.Services.AddScoped(); +## Changing the adapter's behavior +Starting with v4.11.4, the `EntityFrameworkAsyncQueryExecutor` exposes a way to ignore exceptions which may occur during query execution. +This can be useful when you want to handle exceptions in a custom way, for example, by logging them. To ignore exceptions, you can +supply a `Func` to the `IgnoreException` property of the `EntityFrameworkAsyncQueryExecutor` instance. The function +should return `true` if the exception should be ignored and `false` otherwise. An example: +```csharp +builder.Services.AddFluentUIComponents() + .AddDataGridEntityFrameworkAdapter(ex => ex is SqlException sqlEx + && sqlEx.Errors.OfType().Any(e => (e.Class == 11 && e.Number == 0) || (e.Class == 16 && e.Number == 3204))); ``` +For more information see also https://github.com/microsoft/fluentui-blazor/issues/3269. + + ## Support The Microsoft Fluent UI Blazor library is an open source project and is **not** an official part of ASP.NET Core, which means it’s **not** officially supported and isn’t committed to ship updates as part of any official .NET updates. It is built and maintained by Microsoft employees (**and** other contributors)