You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/05/31 19:18:57 UTC
[lucenenet] 01/02:
Lucene.Net.Tests.TestFramework.DependencyInjection: Simplified
ICodecFactory implementation so its registration doesn't rely on "named"
service extension methods.
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch docs/codec-configuration
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit a57e91f0f38a4aabaca4c521c65d7be3238d6bf8
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun May 31 23:17:36 2020 +0700
Lucene.Net.Tests.TestFramework.DependencyInjection: Simplified ICodecFactory implementation so its registration doesn't rely on "named" service extension methods.
---
.../Codecs/ServiceProviderCodecFactory.cs | 30 ++++++++++--
.../DependencyInjection/NamedServiceDescriptor.cs | 53 ----------------------
.../ServiceCollectionExtensions.cs | 43 ------------------
.../ServiceProviderExtensions.cs | 39 ----------------
.../Startup.cs | 6 +--
5 files changed, 28 insertions(+), 143 deletions(-)
diff --git a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Codecs/ServiceProviderCodecFactory.cs b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Codecs/ServiceProviderCodecFactory.cs
index 015f205..b30f61b 100644
--- a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Codecs/ServiceProviderCodecFactory.cs
+++ b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Codecs/ServiceProviderCodecFactory.cs
@@ -1,6 +1,8 @@
-using Lucene.Net.DependencyInjection;
+using Lucene.Net.Util;
using Microsoft.Extensions.DependencyInjection;
using System;
+using System.Collections.Generic;
+using System.Linq;
namespace Lucene.Net.Codecs
{
@@ -22,21 +24,39 @@ namespace Lucene.Net.Codecs
*/
/// <summary>
- /// An example of the most basic implementation of <see cref="ICodecFactory"/>
- /// to retrieve a codec from a <see cref="IServiceProvider"/>.
+ /// An example of a basic implementation of <see cref="ICodecFactory"/>
+ /// to retrieve a codec from a <see cref="IServiceProvider"/> lazily.
/// </summary>
- internal class ServiceProviderCodecFactory : ICodecFactory
+ internal class ServiceProviderCodecFactory : ICodecFactory, IServiceListable
{
private readonly IServiceProvider serviceProvider;
+ private readonly IDictionary<string, Type> codecTypes;
public ServiceProviderCodecFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
+
+ // Get the registered service collection, which can be used to get a list of registered types
+ var serviceCollection = serviceProvider.GetService<IServiceCollection>();
+
+ // Retrieve a list of registered types that subclass Codec. Codecs must be registered by
+ // their concrete type so we can differentiate between them later when calling GetService().
+ this.codecTypes = serviceCollection
+ .Where(t => typeof(Codec).IsAssignableFrom(t.ServiceType))
+ .ToDictionary(
+ t => NamedServiceFactory<Codec>.GetServiceName(t.ImplementationType),
+ t => t.ImplementationType
+ );
}
+ public ICollection<string> AvailableServices => codecTypes.Keys;
+
public Codec GetCodec(string name)
{
- return serviceProvider.GetService<Codec>(name);
+ if (codecTypes.TryGetValue(name, out Type implementationType))
+ return (Codec)serviceProvider.GetService(implementationType);
+
+ throw new ArgumentException($"The codec {name} is not registered.", nameof(name));
}
}
}
diff --git a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/NamedServiceDescriptor.cs b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/NamedServiceDescriptor.cs
deleted file mode 100644
index 51a8df3..0000000
--- a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/NamedServiceDescriptor.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using System;
-
-namespace Lucene.Net.DependencyInjection
-{
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /// <summary>
- /// A service descriptor that can be used to lookup a service implementation
- /// using a <see cref="string"/> name.
- /// </summary>
- internal class NamedServiceDescriptor
- {
- public NamedServiceDescriptor(string name, Type serviceType)
- {
- this.Name = name;
- this.ServiceType = serviceType;
- }
-
- public string Name { get; private set; }
- public Type ServiceType { get; private set; }
-
- public override bool Equals(object obj)
- {
- if (!(obj is NamedServiceDescriptor))
- return false;
-
- var other = (NamedServiceDescriptor)obj;
-
- return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) &&
- ServiceType.Equals(other.ServiceType);
- }
-
- public override int GetHashCode()
- {
- return Name.GetHashCode() ^ ServiceType.GetHashCode();
- }
- }
-}
diff --git a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceCollectionExtensions.cs b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceCollectionExtensions.cs
deleted file mode 100644
index 33f2668..0000000
--- a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceCollectionExtensions.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-using Microsoft.Extensions.DependencyInjection;
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-
-namespace Lucene.Net.DependencyInjection
-{
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /// <summary>
- /// Extensions to Microsoft.Extensions.DependencyInjection to add support for named service types
- /// </summary>
- public static class ServiceCollectionExtensions
- {
- internal static readonly IDictionary<NamedServiceDescriptor, Type> nameToTypeMap
- = new ConcurrentDictionary<NamedServiceDescriptor, Type>();
-
- public static IServiceCollection AddSingleton<TService, TImplementation>(
- this IServiceCollection serviceCollection,
- string name)
- where TService : class where TImplementation : class, TService
- {
- nameToTypeMap[new NamedServiceDescriptor(name, typeof(TService))]
- = typeof(TImplementation);
- return serviceCollection.AddSingleton<TImplementation>();
- }
- }
-}
diff --git a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceProviderExtensions.cs b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceProviderExtensions.cs
deleted file mode 100644
index 0c4e7a8..0000000
--- a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/DependencyInjection/ServiceProviderExtensions.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-using System;
-
-namespace Lucene.Net.DependencyInjection
-{
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /// <summary>
- /// Extensions to Microsoft.Extensions.DependencyInjection to add support for named service types
- /// </summary>
- internal static class ServiceProviderExtensions
- {
- public static T GetService<T>(this IServiceProvider provider, string name)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (string.IsNullOrEmpty(name))
- throw new ArgumentNullException(nameof(name));
-
- ServiceCollectionExtensions.nameToTypeMap.TryGetValue(
- new NamedServiceDescriptor(name, typeof(T)), out Type implementationType);
- return (T)provider.GetService(implementationType);
- }
- }
-}
diff --git a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Startup.cs b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Startup.cs
index 02767cd..437796a 100644
--- a/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Startup.cs
+++ b/src/Lucene.Net.Tests.TestFramework.DependencyInjection/Startup.cs
@@ -17,7 +17,6 @@
using Lucene.Net.Codecs;
using Lucene.Net.Configuration;
-using Lucene.Net.DependencyInjection;
using Lucene.Net.Util;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -71,7 +70,8 @@ public class Startup : LuceneTestFrameworkInitializer
{
serviceCollection.AddSingleton<IConfiguration>(configurationBuilder.Build());
- serviceCollection.AddSingleton<Codec, Lucene.Net.Codecs.Lucene46.Lucene46Codec>("Lucene46");
- serviceCollection.AddSingleton<Codec, MyCodec>("MyCodec");
+ serviceCollection.AddSingleton<Lucene.Net.Codecs.Lucene46.Lucene46Codec>();
+ serviceCollection.AddSingleton<MyCodec>();
+ serviceCollection.AddSingleton<IServiceCollection>(serviceCollection);
}
}