Skip to content

Commit

Permalink
update CommonServiceLocator to remove Microsoft.NETCore.App dependancy (
Browse files Browse the repository at this point in the history
#302)

* 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.

* add helper method to get all instances of a service.
  • Loading branch information
hahn-kev authored Jul 26, 2024
1 parent cb03011 commit e68ba41
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +22,7 @@ internal static class BootstrapNewLanguageProject
/// <summary>
/// Create a new Language Project.
/// </summary>
internal static void BootstrapNewSystem(IServiceLocator servLoc)
internal static void BootstrapNewSystem(IServiceProvider servLoc)
{
using (var nonUndoableUOW = new NonUndoableUnitOfWorkHelper(servLoc.GetInstance<IActionHandler>()))
{
Expand Down
7 changes: 3 additions & 4 deletions src/SIL.LCModel/DomainServices/M3ModelExportServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -315,7 +314,7 @@ select ExportItemAsReference(slot, "Slots"),
/// <summary>
/// Export the full lexicon when exporting both grammar and lexicon.
/// </summary>
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<ILexEntryRepository>()),
Expand Down Expand Up @@ -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<IMoStemMsaRepository>().AllInstances()
Expand Down Expand Up @@ -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<IMoStemAllomorphRepository>().AllInstances()
Expand Down
31 changes: 29 additions & 2 deletions src/SIL.LCModel/ILcmServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
// (http://www.gnu.org/licenses/lgpl-2.1.html)

using System;
using Microsoft.Practices.ServiceLocation;
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
{
/// <summary>
/// This interface defines LCM extensions to IServiceLocator, mainly shortcuts for particular
/// GetService() calls.
/// </summary>
public interface ILcmServiceLocator : IServiceLocator
public interface ILcmServiceLocator : IServiceProvider
{
/// <summary>
/// Shortcut to the IActionHandler instance.
Expand Down Expand Up @@ -95,4 +97,29 @@ internal interface IServiceLocatorInternal
LoadingServices LoadingServices { get; }
IUnitOfWorkService UnitOfWorkService { get; }
}

/// <summary>
/// Helpers to provide drop in methods that match the api of IServiceLocator, but use IServiceProvider instead.
/// </summary>
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<TService>(this IServiceProvider provider)
{
return (TService)provider.GetService(typeof(TService));
}

public static IEnumerable<TService> GetAllInstances<TService>(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<TService>();
//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<TService>) provider.GetService(typeof(IEnumerable<TService>));
}
}
}
4 changes: 2 additions & 2 deletions src/SIL.LCModel/IOC/LcmServiceLocatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +54,7 @@ internal LcmServiceLocatorFactory(BackendProviderType backendProviderType, ILcmU
/// </summary>
/// <returns>An IServiceLocator instance.</returns>
/// ------------------------------------------------------------------------------------
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
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.LCModel/SIL.LCModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="1.4.0" />
<PackageReference Include="CommonServiceLocator" Version="2.0.7" />
<PackageReference Include="GitVersion.MsBuild" Version="5.6.10" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="protobuf-net" Version="2.4.6" />
Expand Down
4 changes: 2 additions & 2 deletions tests/SIL.LCModel.Tests/DomainImpl/NotebookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ITextFactory>().Create();
owner.TextRA = result;
Expand Down

0 comments on commit e68ba41

Please sign in to comment.