From ee01993673efa5c9e1b574858f5e50388a166e21 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Tue, 16 Jul 2024 12:12:51 +0700 Subject: [PATCH 1/2] upgrade common service locator from 1.4.0 to 2.0.7. Replace use of IServiceLocator with IServiceProvider as this is now in the BCL. Write helper class to provide source compatability with IServiceLocator. --- .../BootstrapNewLanguageProject.cs | 3 +-- .../DomainServices/M3ModelExportServices.cs | 7 +++---- src/SIL.LCModel/ILcmServiceLocator.cs | 20 +++++++++++++++++-- .../IOC/LcmServiceLocatorFactory.cs | 4 ++-- src/SIL.LCModel/SIL.LCModel.csproj | 2 +- .../DomainImpl/NotebookTests.cs | 4 ++-- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/SIL.LCModel/DomainServices/BootstrapNewLanguageProject.cs b/src/SIL.LCModel/DomainServices/BootstrapNewLanguageProject.cs index f365707f..1e29353c 100644 --- a/src/SIL.LCModel/DomainServices/BootstrapNewLanguageProject.cs +++ b/src/SIL.LCModel/DomainServices/BootstrapNewLanguageProject.cs @@ -4,7 +4,6 @@ using System; using System.Diagnostics; -using Microsoft.Practices.ServiceLocation; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.Text; using SIL.LCModel.Core.WritingSystems; @@ -23,7 +22,7 @@ internal static class BootstrapNewLanguageProject /// /// Create a new Language Project. /// - internal static void BootstrapNewSystem(IServiceLocator servLoc) + internal static void BootstrapNewSystem(IServiceProvider servLoc) { using (var nonUndoableUOW = new NonUndoableUnitOfWorkHelper(servLoc.GetInstance())) { diff --git a/src/SIL.LCModel/DomainServices/M3ModelExportServices.cs b/src/SIL.LCModel/DomainServices/M3ModelExportServices.cs index 39d08618..3074ede6 100644 --- a/src/SIL.LCModel/DomainServices/M3ModelExportServices.cs +++ b/src/SIL.LCModel/DomainServices/M3ModelExportServices.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Xml.Linq; using Icu; -using Microsoft.Practices.ServiceLocation; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.WritingSystems; @@ -315,7 +314,7 @@ select ExportItemAsReference(slot, "Slots"), /// /// Export the full lexicon when exporting both grammar and lexicon. /// - private static XElement ExportLexiconFull(IServiceLocator servLoc, Normalizer.UNormalizationMode mode) + private static XElement ExportLexiconFull(IServiceProvider servLoc, Normalizer.UNormalizationMode mode) { return new XElement("Lexicon", ExportEntries(servLoc.GetInstance()), @@ -349,7 +348,7 @@ from lexEntryType in lexEntryRef.VariantEntryTypesRS select ExportItemAsReference(lexEntryType, "LexEntryType")))); } - private static XElement ExportMsas(IServiceLocator servLoc) + private static XElement ExportMsas(IServiceProvider servLoc) { return new XElement("MorphoSyntaxAnalyses", from stemMsa in servLoc.GetInstance().AllInstances() @@ -407,7 +406,7 @@ from sense in senseRepos.AllInstances() ExportBestAnalysis(sense.Definition, "Definition", mode))); } - private static XElement ExportAllomorphs(IServiceLocator servLoc, Normalizer.UNormalizationMode mode) + private static XElement ExportAllomorphs(IServiceProvider servLoc, Normalizer.UNormalizationMode mode) { return new XElement("Allomorphs", from stemAllo in servLoc.GetInstance().AllInstances() diff --git a/src/SIL.LCModel/ILcmServiceLocator.cs b/src/SIL.LCModel/ILcmServiceLocator.cs index fe3ed991..cf1a809a 100644 --- a/src/SIL.LCModel/ILcmServiceLocator.cs +++ b/src/SIL.LCModel/ILcmServiceLocator.cs @@ -3,7 +3,6 @@ // (http://www.gnu.org/licenses/lgpl-2.1.html) using System; -using Microsoft.Practices.ServiceLocation; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.WritingSystems; using SIL.LCModel.Infrastructure; @@ -15,7 +14,7 @@ namespace SIL.LCModel /// This interface defines LCM extensions to IServiceLocator, mainly shortcuts for particular /// GetService() calls. /// - public interface ILcmServiceLocator : IServiceLocator + public interface ILcmServiceLocator : IServiceProvider { /// /// Shortcut to the IActionHandler instance. @@ -95,4 +94,21 @@ internal interface IServiceLocatorInternal LoadingServices LoadingServices { get; } IUnitOfWorkService UnitOfWorkService { get; } } + + /// + /// Helpers to provide drop in methods that match the api of IServiceLocator, but use IServiceProvider instead. + /// + public static class IocHelpers + { + public static object GetInstance(this IServiceProvider provider, Type serviceType) + { + //todo how to handle null? Should we throw an exception? + return provider.GetService(serviceType); + } + + public static TService GetInstance(this IServiceProvider provider) + { + return (TService)provider.GetService(typeof(TService)); + } + } } diff --git a/src/SIL.LCModel/IOC/LcmServiceLocatorFactory.cs b/src/SIL.LCModel/IOC/LcmServiceLocatorFactory.cs index c1a50428..97a61a27 100644 --- a/src/SIL.LCModel/IOC/LcmServiceLocatorFactory.cs +++ b/src/SIL.LCModel/IOC/LcmServiceLocatorFactory.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; -using Microsoft.Practices.ServiceLocation; +using CommonServiceLocator; using SIL.LCModel.Application; using SIL.LCModel.Application.Impl; using SIL.LCModel.Core.KernelInterfaces; @@ -54,7 +54,7 @@ internal LcmServiceLocatorFactory(BackendProviderType backendProviderType, ILcmU /// /// An IServiceLocator instance. /// ------------------------------------------------------------------------------------ - public IServiceLocator CreateServiceLocator() + public IServiceProvider CreateServiceLocator() { // NOTE: When creating an object through IServiceLocator.GetInstance the caller has // to call Dispose() on the newly created object, unless it's a singleton diff --git a/src/SIL.LCModel/SIL.LCModel.csproj b/src/SIL.LCModel/SIL.LCModel.csproj index d65092ce..fafcfa45 100644 --- a/src/SIL.LCModel/SIL.LCModel.csproj +++ b/src/SIL.LCModel/SIL.LCModel.csproj @@ -17,7 +17,7 @@ - + diff --git a/tests/SIL.LCModel.Tests/DomainImpl/NotebookTests.cs b/tests/SIL.LCModel.Tests/DomainImpl/NotebookTests.cs index b86597a6..577db39e 100644 --- a/tests/SIL.LCModel.Tests/DomainImpl/NotebookTests.cs +++ b/tests/SIL.LCModel.Tests/DomainImpl/NotebookTests.cs @@ -2,7 +2,7 @@ // This software is licensed under the LGPL, version 2.1 or later // (http://www.gnu.org/licenses/lgpl-2.1.html) -using Microsoft.Practices.ServiceLocation; +using System; using NUnit.Framework; using SIL.LCModel.Core.Text; @@ -122,7 +122,7 @@ public void DeleteNotebookRecord_NotOwnedText() Assert.That(lp.Texts.Contains(text), Is.True); } - private static IText MakeATextWith2Paragraphs(IServiceLocator servLoc, IRnGenericRec owner) + private static IText MakeATextWith2Paragraphs(IServiceProvider servLoc, IRnGenericRec owner) { var result = servLoc.GetInstance().Create(); owner.TextRA = result; From 75b897fad0fcab3b2d3efd8398deabccbbfff63a Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 19 Jul 2024 21:09:37 +0700 Subject: [PATCH 2/2] add helper method to get all instances of a service. --- src/SIL.LCModel/ILcmServiceLocator.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/SIL.LCModel/ILcmServiceLocator.cs b/src/SIL.LCModel/ILcmServiceLocator.cs index cf1a809a..7f500c64 100644 --- a/src/SIL.LCModel/ILcmServiceLocator.cs +++ b/src/SIL.LCModel/ILcmServiceLocator.cs @@ -3,10 +3,13 @@ // (http://www.gnu.org/licenses/lgpl-2.1.html) using System; +using System.Collections.Generic; +using CommonServiceLocator; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.WritingSystems; using SIL.LCModel.Infrastructure; using SIL.LCModel.Infrastructure.Impl; +using SIL.LCModel.IOC; namespace SIL.LCModel { @@ -110,5 +113,13 @@ public static TService GetInstance(this IServiceProvider provider) { return (TService)provider.GetService(typeof(TService)); } + + public static IEnumerable GetAllInstances(this IServiceProvider provider) + { + //structure map might not work the same way as the standard service provider, so we need to handle it separately. + if (provider is IServiceLocator serviceLocator) return serviceLocator.GetAllInstances(); + //the standard service provider handles listing all services like this, however that might not work the same in structure map if an IEnumerable is explicitly registered. + return (IEnumerable) provider.GetService(typeof(IEnumerable)); + } } }