From 551789443451e8032c204fec997792b3f8972d59 Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Sat, 27 Jan 2024 16:23:06 +0200 Subject: [PATCH] Extend the IResourceProvider interface --- .../Interfaces/IResourceProviderService.cs | 40 +++++++++- .../ResourceProviderServiceBase.cs | 76 +++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/dotnet/Common/Interfaces/IResourceProviderService.cs b/src/dotnet/Common/Interfaces/IResourceProviderService.cs index 03d19e4676..970cb1a103 100644 --- a/src/dotnet/Common/Interfaces/IResourceProviderService.cs +++ b/src/dotnet/Common/Interfaces/IResourceProviderService.cs @@ -26,16 +26,16 @@ public interface IResourceProviderService /// /// Gets a resource based on its logical path. /// - /// The type of the requested resource. - /// The logical path of the requested resource. + /// The type of the resource. + /// The logical path of the resource. /// The instance of the resource corresponding to the specified logical path. Task GetResourceAsync(string resourcePath) where T: class; /// /// Gets a resource based on its logical path. /// - /// The type of the requested resource. - /// The logical path of the requested resource. + /// The type of the resource. + /// The logical path of the resource. /// The instance of the resource corresponding to the specified logical path. T GetResource(string resourcePath) where T : class; @@ -45,5 +45,37 @@ public interface IResourceProviderService /// The logical path of the action to be executed. /// The that contains details about the result of the execution. Task ExecuteAction(string actionPath); + + /// + /// Creates or updates a resource based on its logical path. + /// + /// The type of the resource. + /// The logical path of the resource. + /// The instance of the resource being created or updated. + /// + Task UpsertResourceAsync(string resourcePath, T resource) where T : class; + + /// + /// Creates or updates a resource based on its logical path. + /// + /// The type of the resource. + /// The logical path of the resource. + /// The instance of the resource being created or updated. + void UpsertResource(string resourcePath, T resource) where T : class; + + /// + /// Deletes a resource based on its logical path. + /// + /// The type of the resource. + /// The logical path of the resource. + /// + Task DeleteResourceAsync(string resourcePath) where T : class; + + /// + /// Deletes a resource based on its logical path. + /// + /// The type of the resource. + /// The logical path of the resource. + void DeleteResource(string resourcePath) where T : class; } } diff --git a/src/dotnet/Common/Services/ResourceProviders/ResourceProviderServiceBase.cs b/src/dotnet/Common/Services/ResourceProviders/ResourceProviderServiceBase.cs index ca45c18b3f..bd86589318 100644 --- a/src/dotnet/Common/Services/ResourceProviders/ResourceProviderServiceBase.cs +++ b/src/dotnet/Common/Services/ResourceProviders/ResourceProviderServiceBase.cs @@ -94,6 +94,42 @@ public async Task GetResourceAsync(string resourcePath) where T : class return await GetResourceAsyncInternal(instances); } + /// + public async Task UpsertResourceAsync(string resourcePath, T resource) where T : class + { + if (!_isInitialized) + throw new ResourceProviderException($"The resource provider {_name} is not initialized."); + var instances = GetResourceInstancesFromPath(resourcePath); + await UpsertResourceAsync(resourcePath, resource); + } + + /// + public void UpsertResource(string resourcePath, T resource) where T : class + { + if (!_isInitialized) + throw new ResourceProviderException($"The resource provider {_name} is not initialized."); + var instances = GetResourceInstancesFromPath(resourcePath); + UpsertResource(instances, resource); + } + + /// + public async Task DeleteResourceAsync(string resourcePath) where T : class + { + if (!_isInitialized) + throw new ResourceProviderException($"The resource provider {_name} is not initialized."); + var instances = GetResourceInstancesFromPath(resourcePath); + await DeleteResourceAsync(instances); + } + + /// + public void DeleteResource(string resourcePath) where T : class + { + if (!_isInitialized) + throw new ResourceProviderException($"The resource provider {_name} is not initialized."); + var instances = GetResourceInstancesFromPath(resourcePath); + DeleteResource(instances); + } + /// /// The internal implementation of Initialize. Must be overridden in derived classes. /// @@ -134,6 +170,46 @@ protected virtual async Task GetResourceAsyncInternal(List + /// The internal implementation of UpsertResource. Must be overridden in derived classes. + /// + /// The list of objects parsed from the resource path. + /// The instance of the resource being created or updated. + /// + protected virtual void UpsertResource(List instances, T resource) => + throw new NotImplementedException(); + + /// + /// The internal implementation of UpsertResourceAsync. Must be overridden in derived classes. + /// + /// The list of objects parsed from the resource path. + /// The instance of the resource being created or updated. + /// + protected virtual async Task UpsertResourceAsync(List instances, T resource) + { + await Task.CompletedTask; + throw new NotImplementedException(); + } + + /// + /// The internal implementation of DeleteResource. Must be overridden in derived classes. + /// + /// The list of objects parsed from the resource path. + /// + protected virtual void DeleteResource(List instances) => + throw new NotImplementedException(); + + /// + /// The internal implementation of DeleteResourceAsync. Must be overridden in derived classes. + /// + /// The list of objects parsed from the resource path. + /// + protected virtual async Task DeleteResourceAsync(List instances) + { + await Task.CompletedTask; + throw new NotImplementedException(); + } + private List GetResourceInstancesFromPath(string resourcePath) { if (string.IsNullOrWhiteSpace(resourcePath))