From e0df32ab19a238512045f5a6605c546a082a935e Mon Sep 17 00:00:00 2001 From: Gidon Date: Sun, 11 Feb 2018 22:11:35 +0200 Subject: [PATCH] Add netstandard 2.0 support to Autofac intregration --- .../AutofacConfigFixture.cs | 55 +++++++++ .../AutofacContrib.SolrNet.Tests.csproj | 9 +- .../AutofacMulticoreFixture.cs | 3 +- AutofacContrib.SolrNet.Tests/cores.json | 21 ++++ .../AutofacContrib.SolrNet.csproj | 11 +- .../Config/SolrConfigurationSection.cs | 2 +- .../Config/SolrServerElement.cs | 5 +- AutofacContrib.SolrNet/Config/SolrServers.cs | 15 ++- AutofacContrib.SolrNet/ISolrServer.cs | 9 ++ .../RegistrationException.cs | 14 +++ AutofacContrib.SolrNet/SolrNetModule.cs | 108 ++++++++++-------- AutofacContrib.SolrNet/SolrServer.cs | 22 ++++ README.md | 2 +- changelog.md | 5 +- 14 files changed, 221 insertions(+), 60 deletions(-) create mode 100644 AutofacContrib.SolrNet.Tests/AutofacConfigFixture.cs create mode 100644 AutofacContrib.SolrNet.Tests/cores.json create mode 100644 AutofacContrib.SolrNet/ISolrServer.cs create mode 100644 AutofacContrib.SolrNet/RegistrationException.cs create mode 100644 AutofacContrib.SolrNet/SolrServer.cs diff --git a/AutofacContrib.SolrNet.Tests/AutofacConfigFixture.cs b/AutofacContrib.SolrNet.Tests/AutofacConfigFixture.cs new file mode 100644 index 000000000..1aac19222 --- /dev/null +++ b/AutofacContrib.SolrNet.Tests/AutofacConfigFixture.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Autofac; +using Microsoft.Extensions.Configuration; +using SolrNet; +using SolrNet.Impl; +using Xunit; + +namespace AutofacContrib.SolrNet.Tests +{ + public class AutofacConfigFixture + { + + [Fact] + public void RegistersSolrConnectionWithCoresJsonServerUrl() + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetParent("../../../").FullName) + .AddJsonFile("cores.json") + .Build() + .GetSection("solr:servers"); + + var builder = new ContainerBuilder(); + builder.RegisterModule(new SolrNetModule(configuration.Get>())); + var container = builder.Build(); + var solrConnection = (SolrConnection)container.ResolveNamed("entitySolrNet.Impl.SolrConnection"); + + Assert.Equal("http://localhost:8983/solr/collection1", solrConnection.ServerURL); + } + + + [Fact] + public void CheckParseJsonConfiguration() + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetParent("../../../").FullName) + .AddJsonFile("cores.json") + .Build() + .GetSection("solr:servers"); + + var servers = configuration.Get>(); + + Assert.Equal(3, servers.Count); + Assert.Equal("entity", servers.First().Id); + Assert.Equal("http://localhost:8983/solr/collection1", servers.First().Url); + Assert.Equal("AutofacContrib.SolrNet.Tests.Entity, AutofacContrib.SolrNet.Tests", servers.First().DocumentType); + + Assert.Equal("entity3", servers.Last().Id); + Assert.Equal("http://localhost:8983/solr/core1", servers.Last().Url); + Assert.Equal("AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests", servers.Last().DocumentType); + + } + } +} diff --git a/AutofacContrib.SolrNet.Tests/AutofacContrib.SolrNet.Tests.csproj b/AutofacContrib.SolrNet.Tests/AutofacContrib.SolrNet.Tests.csproj index 76a53bb69..ccd9bdf7b 100644 --- a/AutofacContrib.SolrNet.Tests/AutofacContrib.SolrNet.Tests.csproj +++ b/AutofacContrib.SolrNet.Tests/AutofacContrib.SolrNet.Tests.csproj @@ -1,11 +1,13 @@  - net46 + net462 + + @@ -24,4 +26,9 @@ + + + Always + + \ No newline at end of file diff --git a/AutofacContrib.SolrNet.Tests/AutofacMulticoreFixture.cs b/AutofacContrib.SolrNet.Tests/AutofacMulticoreFixture.cs index 2c943d0cd..25fa6c85a 100644 --- a/AutofacContrib.SolrNet.Tests/AutofacMulticoreFixture.cs +++ b/AutofacContrib.SolrNet.Tests/AutofacMulticoreFixture.cs @@ -211,6 +211,7 @@ public void ResolveSolrOperations_fromConfigSection() } } + public class Entity { } public class Entity1 { } public class Entity2 { } -} \ No newline at end of file +} diff --git a/AutofacContrib.SolrNet.Tests/cores.json b/AutofacContrib.SolrNet.Tests/cores.json new file mode 100644 index 000000000..910089430 --- /dev/null +++ b/AutofacContrib.SolrNet.Tests/cores.json @@ -0,0 +1,21 @@ +{ + "solr": { + "servers": [ + { + "id": "entity", + "url": "http://localhost:8983/solr/collection1", + "documentType": "AutofacContrib.SolrNet.Tests.Entity, AutofacContrib.SolrNet.Tests" + }, + { + "id": "entity2", + "url": "http://localhost:8983/solr/core0", + "documentType": "AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests" + }, + { + "id": "entity3", + "url": "http://localhost:8983/solr/core1", + "documentType": "AutofacContrib.SolrNet.Tests.Entity2, AutofacContrib.SolrNet.Tests" + } + ] + } +} \ No newline at end of file diff --git a/AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj b/AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj index 4c785cb95..9a8f30e49 100644 --- a/AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj +++ b/AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj @@ -1,6 +1,6 @@  - net46 + net46;netstandard2.0 1.0.3 https://github.com/solrnet/solrnet https://raw.githubusercontent.com/SolrNet/SolrNet/master/license.txt @@ -18,9 +18,14 @@ - + - + + + + + + diff --git a/AutofacContrib.SolrNet/Config/SolrConfigurationSection.cs b/AutofacContrib.SolrNet/Config/SolrConfigurationSection.cs index 6f4f30985..247bc6024 100644 --- a/AutofacContrib.SolrNet/Config/SolrConfigurationSection.cs +++ b/AutofacContrib.SolrNet/Config/SolrConfigurationSection.cs @@ -10,4 +10,4 @@ public SolrServers SolrServers get { return (SolrServers)base[""]; } } } -} \ No newline at end of file +} diff --git a/AutofacContrib.SolrNet/Config/SolrServerElement.cs b/AutofacContrib.SolrNet/Config/SolrServerElement.cs index fff3fd7f5..4d1e1d938 100644 --- a/AutofacContrib.SolrNet/Config/SolrServerElement.cs +++ b/AutofacContrib.SolrNet/Config/SolrServerElement.cs @@ -4,7 +4,8 @@ namespace AutofacContrib.SolrNet.Config { /// /// Solr instance or core config /// - public class SolrServerElement : ConfigurationElement { + public class SolrServerElement : ConfigurationElement, ISolrServer + { private const string ID = "id"; private const string URL = "url"; private const string DOCUMENT_TYPE = "documentType"; @@ -40,4 +41,4 @@ public override string ToString() { return string.Format("Id: {0} Url: {1} DocType: {2}", Id, Url, DocumentType); } } -} \ No newline at end of file +} diff --git a/AutofacContrib.SolrNet/Config/SolrServers.cs b/AutofacContrib.SolrNet/Config/SolrServers.cs index 0fc45e1b8..cab15a45c 100644 --- a/AutofacContrib.SolrNet/Config/SolrServers.cs +++ b/AutofacContrib.SolrNet/Config/SolrServers.cs @@ -1,10 +1,11 @@ -using System.Configuration; +using System.Collections.Generic; +using System.Configuration; namespace AutofacContrib.SolrNet.Config { /// /// Solr cores / instances configuration /// - public class SolrServers : ConfigurationElementCollection { + public class SolrServers : ConfigurationElementCollection, IEnumerable { /// /// Adds a new core / instance to the config /// @@ -22,6 +23,14 @@ protected override object GetElementKey(ConfigurationElement element) { return solrServerElement.Url + solrServerElement.DocumentType; } + IEnumerator IEnumerable.GetEnumerator() + { + foreach (SolrServerElement server in this) + { + yield return server; + } + } + public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } @@ -30,4 +39,4 @@ protected override string ElementName { get { return "server"; } } } -} \ No newline at end of file +} diff --git a/AutofacContrib.SolrNet/ISolrServer.cs b/AutofacContrib.SolrNet/ISolrServer.cs new file mode 100644 index 000000000..cd5fea034 --- /dev/null +++ b/AutofacContrib.SolrNet/ISolrServer.cs @@ -0,0 +1,9 @@ +namespace AutofacContrib.SolrNet +{ + public interface ISolrServer + { + string Id { get; set; } + string Url { get; set; } + string DocumentType { get; set; } + } +} diff --git a/AutofacContrib.SolrNet/RegistrationException.cs b/AutofacContrib.SolrNet/RegistrationException.cs new file mode 100644 index 000000000..2b0976ee2 --- /dev/null +++ b/AutofacContrib.SolrNet/RegistrationException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutofacContrib.SolrNet +{ + public class RegistrationException : ApplicationException + { + public RegistrationException(string message, Exception innerException) : base(message, innerException) { } + public RegistrationException(string message) : base(message) {} + } +} diff --git a/AutofacContrib.SolrNet/SolrNetModule.cs b/AutofacContrib.SolrNet/SolrNetModule.cs index da5c3132e..90d30f8bf 100644 --- a/AutofacContrib.SolrNet/SolrNetModule.cs +++ b/AutofacContrib.SolrNet/SolrNetModule.cs @@ -18,10 +18,8 @@ using System; using System.Collections.Generic; -using System.Configuration; using Autofac; using Autofac.Core; -using AutofacContrib.SolrNet.Config; using HttpWebAdapters; using SolrNet; using SolrNet.Impl; @@ -37,25 +35,29 @@ using SolrNet.Schema; using SolrNet.Utils; -namespace AutofacContrib.SolrNet { +namespace AutofacContrib.SolrNet +{ /// /// Configures SolrNet in an Autofac container /// - public class SolrNetModule : Module { - protected override void Load(ContainerBuilder builder) { + public class SolrNetModule : Module + { + protected override void Load(ContainerBuilder builder) + { if (!string.IsNullOrEmpty(ServerUrl)) RegisterSingleCore(builder); else if (solrServers != null) RegisterMultiCore(builder); else - throw new ConfigurationErrorsException("SolrNetModule Configurations Error!"); + throw new RegistrationException("SolrNetModule Configurations Error!"); } /// /// Register a single-core server /// /// - public SolrNetModule(string serverUrl) { + public SolrNetModule(string serverUrl) + { ServerUrl = serverUrl; } @@ -71,14 +73,15 @@ public SolrNetModule(string serverUrl) { /// public IHttpWebRequestFactory HttpWebRequestFactory { get; set; } - private void RegisterCommonComponents(ContainerBuilder builder) { + private void RegisterCommonComponents(ContainerBuilder builder) + { var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager()); builder.RegisterInstance(mapper).As(); // builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); - builder.RegisterGeneric(typeof (SolrDocumentActivator<>)).As(typeof (ISolrDocumentActivator<>)); - builder.RegisterGeneric(typeof (SolrDocumentResponseParser<>)).As(typeof (ISolrDocumentResponseParser<>)); + builder.RegisterGeneric(typeof(SolrDocumentActivator<>)).As(typeof(ISolrDocumentActivator<>)); + builder.RegisterGeneric(typeof(SolrDocumentResponseParser<>)).As(typeof(ISolrDocumentResponseParser<>)); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); @@ -92,12 +95,12 @@ private void RegisterCommonComponents(ContainerBuilder builder) { typeof (UniqueKeyMatchesMappingRule), typeof(MultivaluedMappedToCollectionRule), }) - + builder.RegisterType(p).As(); - builder.RegisterType().As(); + builder.RegisterType().As(); builder.RegisterGeneric(typeof(SolrMoreLikeThisHandlerQueryResultsParser<>)).As(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>)); builder.RegisterGeneric(typeof(SolrQueryExecuter<>)).As(typeof(ISolrQueryExecuter<>)); - builder.RegisterGeneric(typeof (SolrDocumentSerializer<>)).As(typeof (ISolrDocumentSerializer<>)); + builder.RegisterGeneric(typeof(SolrDocumentSerializer<>)).As(typeof(ISolrDocumentSerializer<>)); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); @@ -106,34 +109,38 @@ private void RegisterCommonComponents(ContainerBuilder builder) { builder.RegisterType().As>>(); } - private void RegisterSingleCore(ContainerBuilder builder) { + private void RegisterSingleCore(ContainerBuilder builder) + { RegisterCommonComponents(builder); SolrConnection solrConnection = new SolrConnection(ServerUrl); - if (HttpWebRequestFactory != null) { + if (HttpWebRequestFactory != null) + { solrConnection.HttpWebRequestFactory = HttpWebRequestFactory; } builder.RegisterInstance(solrConnection).As(); - builder.RegisterGeneric(typeof (SolrBasicServer<>)) - .As(typeof (ISolrBasicOperations<>), typeof (ISolrBasicReadOnlyOperations<>)) + builder.RegisterGeneric(typeof(SolrBasicServer<>)) + .As(typeof(ISolrBasicOperations<>), typeof(ISolrBasicReadOnlyOperations<>)) .SingleInstance(); - builder.RegisterGeneric(typeof (SolrServer<>)) - .As(typeof (ISolrOperations<>), typeof (ISolrReadOnlyOperations<>)) + builder.RegisterGeneric(typeof(SolrServer<>)) + .As(typeof(ISolrOperations<>), typeof(ISolrReadOnlyOperations<>)) .SingleInstance(); } - private readonly SolrServers solrServers; + private readonly IEnumerable solrServers; /// /// Register multi-core server /// /// - public SolrNetModule(SolrServers solrServers) { + public SolrNetModule(IEnumerable solrServers) + { this.solrServers = solrServers; } - private void RegisterMultiCore(ContainerBuilder builder) { + private void RegisterMultiCore(ContainerBuilder builder) + { RegisterCommonComponents(builder); AddCoresFromConfig(builder); } @@ -142,17 +149,18 @@ private void RegisterMultiCore(ContainerBuilder builder) { /// Registers a new core in the container. /// This method is meant to be used after the facility initialization /// - private static void RegisterCore(SolrCore core, ContainerBuilder builder) { - var coreConnectionId = core.Id + typeof (SolrConnection); + private static void RegisterCore(SolrCore core, ContainerBuilder builder) + { + var coreConnectionId = core.Id + typeof(SolrConnection); - builder.RegisterType(typeof (SolrConnection)) - .Named(coreConnectionId, typeof (ISolrConnection)) + builder.RegisterType(typeof(SolrConnection)) + .Named(coreConnectionId, typeof(ISolrConnection)) .WithParameters(new[] { new NamedParameter("serverURL", core.Url) }); - var ISolrQueryExecuter = typeof (ISolrQueryExecuter<>).MakeGenericType(core.DocumentType); - var SolrQueryExecuter = typeof (SolrQueryExecuter<>).MakeGenericType(core.DocumentType); + var ISolrQueryExecuter = typeof(ISolrQueryExecuter<>).MakeGenericType(core.DocumentType); + var SolrQueryExecuter = typeof(SolrQueryExecuter<>).MakeGenericType(core.DocumentType); builder.RegisterType(SolrQueryExecuter) .Named(core.Id + SolrQueryExecuter, ISolrQueryExecuter) @@ -160,9 +168,9 @@ private static void RegisterCore(SolrCore core, ContainerBuilder builder) { new ResolvedParameter((p, c) => p.Name == "connection", (p, c) => c.ResolveNamed(coreConnectionId, typeof (ISolrConnection))), }); - var ISolrBasicOperations = typeof (ISolrBasicOperations<>).MakeGenericType(core.DocumentType); - var ISolrBasicReadOnlyOperations = typeof (ISolrBasicReadOnlyOperations<>).MakeGenericType(core.DocumentType); - var SolrBasicServer = typeof (SolrBasicServer<>).MakeGenericType(core.DocumentType); + var ISolrBasicOperations = typeof(ISolrBasicOperations<>).MakeGenericType(core.DocumentType); + var ISolrBasicReadOnlyOperations = typeof(ISolrBasicReadOnlyOperations<>).MakeGenericType(core.DocumentType); + var SolrBasicServer = typeof(SolrBasicServer<>).MakeGenericType(core.DocumentType); builder.RegisterType(SolrBasicServer) .Named(core.Id + SolrBasicServer, ISolrBasicOperations) @@ -178,9 +186,9 @@ private static void RegisterCore(SolrCore core, ContainerBuilder builder) { new ResolvedParameter((p, c) => p.Name == "queryExecuter", (p, c) => c.ResolveNamed(core.Id + SolrQueryExecuter, ISolrQueryExecuter)) }); - var ISolrOperations = typeof (ISolrOperations<>).MakeGenericType(core.DocumentType); - var ISolrReadOnlyOperations = typeof (ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType); - var SolrServer = typeof (SolrServer<>).MakeGenericType(core.DocumentType); + var ISolrOperations = typeof(ISolrOperations<>).MakeGenericType(core.DocumentType); + var ISolrReadOnlyOperations = typeof(ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType); + var SolrServer = typeof(SolrServer<>).MakeGenericType(core.DocumentType); builder.RegisterType(SolrServer) .Named(core.Id, ISolrOperations) @@ -197,17 +205,20 @@ private static void RegisterCore(SolrCore core, ContainerBuilder builder) { }); } - private void AddCoresFromConfig(ContainerBuilder builder) { + private void AddCoresFromConfig(ContainerBuilder builder) + { if (solrServers == null) return; - foreach (SolrServerElement server in solrServers) { + foreach (ISolrServer server in solrServers) + { var solrCore = GetCoreFrom(server); RegisterCore(solrCore, builder); } } - private static SolrCore GetCoreFrom(SolrServerElement server) { + private static SolrCore GetCoreFrom(ISolrServer server) + { var id = server.Id ?? Guid.NewGuid().ToString(); var documentType = GetCoreDocumentType(server); var coreUrl = GetCoreUrl(server); @@ -215,31 +226,36 @@ private static SolrCore GetCoreFrom(SolrServerElement server) { return new SolrCore(id, documentType, coreUrl); } - private static string GetCoreUrl(SolrServerElement server) { + private static string GetCoreUrl(ISolrServer server) + { var url = server.Url; if (string.IsNullOrEmpty(url)) - throw new ConfigurationErrorsException("Core url missing in SolrNet core configuration"); + throw new RegistrationException("Core url missing in SolrNet core configuration"); return url; } - private static Type GetCoreDocumentType(SolrServerElement server) { + private static Type GetCoreDocumentType(ISolrServer server) + { var documentType = server.DocumentType; if (string.IsNullOrEmpty(documentType)) - throw new ConfigurationErrorsException("Document type missing in SolrNet core configuration"); + throw new RegistrationException("Document type missing in SolrNet core configuration"); Type type; - try { + try + { type = Type.GetType(documentType); - } catch (Exception e) { - throw new ConfigurationErrorsException(string.Format("Error getting document type '{0}'", documentType), e); + } + catch (Exception e) + { + throw new RegistrationException(string.Format("Error getting document type '{0}'", documentType), e); } if (type == null) - throw new ConfigurationErrorsException(string.Format("Error getting document type '{0}'", documentType)); + throw new RegistrationException(string.Format("Error getting document type '{0}'", documentType)); return type; } } -} \ No newline at end of file +} diff --git a/AutofacContrib.SolrNet/SolrServer.cs b/AutofacContrib.SolrNet/SolrServer.cs new file mode 100644 index 000000000..ead43ed12 --- /dev/null +++ b/AutofacContrib.SolrNet/SolrServer.cs @@ -0,0 +1,22 @@ +namespace AutofacContrib.SolrNet +{ + public class SolrServer : ISolrServer + { + public SolrServer() { } + + public SolrServer(string id, string url, string documentType) + { + this.Id = id; + this.Url = url; + this.DocumentType = documentType; + } + + public string Id { get; set; } + + public string Url { get; set; } + + public string DocumentType { get; set; } + + public override string ToString() => $"Id: {Id} Url: {Url} DocType: {DocumentType}"; + } +} diff --git a/README.md b/README.md index 7ca58b63b..4a4229678 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ The easiest way to get going is to use our NuGet packages: |[SolrNet.StructureMap](https://www.nuget.org/packages/SolrNet.StructureMap/)|[StructureMap](http://structuremap.github.io/) | 4.6 | 2.0 | |[SolrNet.Ninject](https://www.nuget.org/packages/SolrNet.Ninject/)| [Ninject](http://www.ninject.org/) | 4.6 | 2.0 | |[SolrNet.Unity](https://www.nuget.org/packages/SolrNet.Unity/)| [Unity](https://github.com/unitycontainer/unity) | 4.6 | - | -|[SolrNet.Autofac](https://www.nuget.org/packages/SolrNet.Autofac/)| [Autofac](https://autofac.org/) | 4.6 | - | +|[SolrNet.Autofac](https://www.nuget.org/packages/SolrNet.Autofac/)| [Autofac](https://autofac.org/) | 4.6 | 2.0 | |[SolrNet.NHibernate](https://www.nuget.org/packages/SolrNet.NHibernate/)| [NHibernate](http://nhibernate.info/) | 4.6 | - | #### SolrCloud diff --git a/changelog.md b/changelog.md index 5cc087588..91778362b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,9 +1,10 @@ # Change Log ## 1.0.4 -- Add `netstandard 2.0`support to `Ninject.Integration.SolrNet` +- Add `netstandard 2.0` support to `Ninject.Integration.SolrNet` +- Add `netstandard 2.0` support to `AutofacContrib.SolrNet` -## 1.0.3 +## 1.0.3 (2018-02-05) - Add `netstandard 2.0` support to `StructureMap.SolrNetIntegration` #376 (@ciprianmo) - Fix: Attempting to add duplicate SolrField should give a more helpful error #380