From 7baa975c6224b02b9dac1e3ea65a536fbd5476bc Mon Sep 17 00:00:00 2001 From: Benjin Dubishar Date: Wed, 8 Feb 2023 11:35:01 -0800 Subject: [PATCH] Shifting handler-running methods to shared location (#1851) * Shifting helper methods to shared location * widening scope --- .../SqlProjects/SqlProjectsService.cs | 70 +-------------- .../Utility/BaseService.cs | 89 +++++++++++++++++++ 2 files changed, 90 insertions(+), 69 deletions(-) create mode 100644 src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs diff --git a/src/Microsoft.SqlTools.ServiceLayer/SqlProjects/SqlProjectsService.cs b/src/Microsoft.SqlTools.ServiceLayer/SqlProjects/SqlProjectsService.cs index 091fb90b..eafec25e 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/SqlProjects/SqlProjectsService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/SqlProjects/SqlProjectsService.cs @@ -18,7 +18,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects /// /// Main class for SqlProjects service /// - class SqlProjectsService + public sealed class SqlProjectsService : BaseService { private static readonly Lazy instance = new Lazy(() => new SqlProjectsService()); @@ -216,74 +216,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects #region Helper methods - /// - /// Synchronous action with standard ResultStatus - /// - /// - /// - /// - private async Task RunWithErrorHandling(Action action, RequestContext requestContext) - { - await RunWithErrorHandling(async () => await Task.Run(action), requestContext); - } - - /// - /// Asynchronous action with standard ResultStatus - /// - /// - /// - /// - private async Task RunWithErrorHandling(Func action, RequestContext requestContext) - { - await RunWithErrorHandling(async () => - { - await action(); - - return new ResultStatus() - { - Success = true, - ErrorMessage = null - }; - }, requestContext); - } - - - /// - /// Synchronous action with custom result - /// - /// - /// - /// - /// - private async Task RunWithErrorHandling(Func action, RequestContext requestContext) where T : ResultStatus, new() - { - await RunWithErrorHandling(async () => await Task.Run(action), requestContext); - } - - /// - /// Asynchronous action with custom result - /// - /// - /// - /// - /// - private async Task RunWithErrorHandling(Func> action, RequestContext requestContext) where T : ResultStatus, new() - { - try - { - T result = await action(); - await requestContext.SendResult(result); - } - catch (Exception ex) - { - await requestContext.SendResult(new T() - { - Success = false, - ErrorMessage = ex.Message - }); - } - } - private SqlProject GetProject(string projectUri) { if (!Projects.ContainsKey(projectUri)) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs b/src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs new file mode 100644 index 00000000..142e9267 --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs @@ -0,0 +1,89 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using System.Threading.Tasks; +using Microsoft.SqlTools.Hosting.Protocol; + +namespace Microsoft.SqlTools.ServiceLayer.Utility +{ + /// + /// Base class for services that contains helpful methods + /// + public abstract class BaseService + { + #region Runners with error handling + + /// + /// Synchronous action with standard ResultStatus + /// + /// + /// + /// + public static async Task RunWithErrorHandling(Action action, RequestContext requestContext) + { + await RunWithErrorHandling(async () => await Task.Run(action), requestContext); + } + + /// + /// Asynchronous action with standard ResultStatus + /// + /// + /// + /// + public static async Task RunWithErrorHandling(Func action, RequestContext requestContext) + { + await RunWithErrorHandling(async () => + { + await action(); + + return new ResultStatus() + { + Success = true, + ErrorMessage = null + }; + }, requestContext); + } + + + /// + /// Synchronous action with custom result + /// + /// + /// + /// + /// + public static async Task RunWithErrorHandling(Func action, RequestContext requestContext) where T : ResultStatus, new() + { + await RunWithErrorHandling(async () => await Task.Run(action), requestContext); + } + + /// + /// Asynchronous action with custom result + /// + /// + /// + /// + /// + public static async Task RunWithErrorHandling(Func> action, RequestContext requestContext) where T : ResultStatus, new() + { + try + { + T result = await action(); + await requestContext.SendResult(result); + } + catch (Exception ex) + { + await requestContext.SendResult(new T() + { + Success = false, + ErrorMessage = ex.Message + }); + } + } + + #endregion + } +}