Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Use Lazy<T> for GeometryServiceProvider.Instance.
Browse files Browse the repository at this point in the history
On platforms that support it, anyway.
  • Loading branch information
airbreather committed Apr 13, 2018
1 parent 8635a94 commit e405106
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/GeoAPI/GeoAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<DefineConstants>$(DefineConstants);FEATURE_DEFAULT_PARAMETERS</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_INTERFACE_VARIANCE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_DOUBLE_TRYPARSE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_LAZY_T</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_STRING_TOUPPERINVARIANT</DefineConstants>
</PropertyGroup>

Expand All @@ -82,6 +83,7 @@
<DefineConstants>$(DefineConstants);FEATURE_DEFAULT_PARAMETERS</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_INTERFACE_VARIANCE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_DOUBLE_TRYPARSE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_LAZY_T</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_STRING_TOUPPERINVARIANT</DefineConstants>
</PropertyGroup>

Expand Down Expand Up @@ -110,6 +112,10 @@
<DefineConstants>$(DefineConstants);HAS_SYSTEM_STRING_TOUPPER_CULTUREINFO</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' Or '$(TargetFramework)' == 'net40-client' Or '$(TargetFramework)' == 'net403' Or '$(TargetFramework)' == 'net403-client' Or '$(TargetFramework)' == 'net45' ">
<DefineConstants>$(DefineConstants);HAS_SYSTEM_LAZY_T</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
Expand All @@ -119,6 +125,7 @@
<DefineConstants>$(DefineConstants);FEATURE_INTERFACE_VARIANCE</DefineConstants>

<DefineConstants>$(DefineConstants);HAS_SYSTEM_DOUBLE_TRYPARSE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_LAZY_T</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_SYSTEM_STRING_TOUPPERINVARIANT</DefineConstants>
</PropertyGroup>

Expand Down
12 changes: 11 additions & 1 deletion src/GeoAPI/GeometryServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ namespace GeoAPI
/// </summary>
public static class GeometryServiceProvider
{
private static IGeometryServices _instance;
#if HAS_SYSTEM_LAZY_T
private static readonly Lazy<IGeometryServices> ReflectedInstance = new Lazy<IGeometryServices>(ReflectInstance);
#else
private static readonly object LockObject = new object();
#endif

private static volatile IGeometryServices _instance;

/// <summary>
/// Gets or sets the <see cref="IGeometryServices"/> instance.
/// </summary>
public static IGeometryServices Instance
{
#if HAS_SYSTEM_LAZY_T
get => _instance ?? ReflectedInstance.Value;
set => _instance = value ?? throw new ArgumentNullException(nameof(value));
#else
get
{
lock (LockObject)
Expand All @@ -35,6 +44,7 @@ public static IGeometryServices Instance
_instance = value;
}
}
#endif
}

#if HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES
Expand Down

0 comments on commit e405106

Please sign in to comment.