Skip to content

Commit

Permalink
removing need to name bindings in ninject
Browse files Browse the repository at this point in the history
uses the document type to resolve the ISolrConnection bindings
  • Loading branch information
jerizm committed Apr 18, 2012
1 parent 3d22dc5 commit 003aef2
Showing 1 changed file with 67 additions and 73 deletions.
140 changes: 67 additions & 73 deletions Ninject.Integration.SolrNet/SolrNetModule.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed 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.
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed 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.
#endregion

using System;
using System.Collections.Generic;
using System.Configuration;
using Ninject.Integration.SolrNet.Config;
using Ninject.Modules;
using SolrNet;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
using SolrNet.Impl.FacetQuerySerializers;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;
using SolrNet.Impl.ResponseParsers;
using SolrNet.Mapping;
using SolrNet.Mapping.Validation;
using SolrNet.Mapping.Validation.Rules;
using Ninject.Modules;
using SolrNet;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
using SolrNet.Impl.FacetQuerySerializers;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;
using SolrNet.Impl.ResponseParsers;
using SolrNet.Mapping;
using SolrNet.Mapping.Validation;
using SolrNet.Mapping.Validation.Rules;
using SolrNet.Schema;
using SolrNet.Utils;
using Ninject.Planning.Bindings;

namespace Ninject.Integration.SolrNet {
namespace Ninject.Integration.SolrNet {
/// <summary>
/// Configures SolrNet in a Ninject kernel
/// </summary>
public class SolrNetModule : NinjectModule {
/// </summary>
public class SolrNetModule : NinjectModule {
private readonly string serverURL;
private readonly List<SolrCore> cores = new List<SolrCore>();

private readonly List<SolrCore> cores = new List<SolrCore>();

/// <summary>
/// Optional override for document mapper
/// </summary>
public IReadOnlyMappingManager Mapper { get; set; }

/// </summary>
public IReadOnlyMappingManager Mapper { get; set; }

/// <summary>
/// Configures SolrNet in a Ninject kernel
/// </summary>
/// <param name="serverURL"></param>
/// <param name="serverURL"></param>
public SolrNetModule(string serverURL) {
this.serverURL = serverURL;
}

}

/// <summary>
/// Configures SolrNet in a Ninject kernel with multiple servers/cores
/// </summary>
/// <param name="solrServers"></param>
/// <param name="solrServers"></param>
public SolrNetModule(SolrServers solrServers) {
AddCoresFromConfig(solrServers);
}
Expand Down Expand Up @@ -116,7 +117,6 @@ private static Type GetCoreDocumentType(SolrServerElement server)
}

private void RegisterCore(SolrCore core) {
var coreConnectionId = core.Id + typeof (SolrConnection);
var solrBasicOperations = typeof(ISolrBasicOperations<>).MakeGenericType(core.DocumentType);
var solrOperations = typeof(ISolrOperations<>).MakeGenericType(core.DocumentType);
var solrReadOnlyOperations = typeof(ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType);
Expand All @@ -126,40 +126,34 @@ private void RegisterCore(SolrCore core) {
var iSolrQueryExecuter = typeof(ISolrQueryExecuter<>).MakeGenericType(core.DocumentType);
var solrQueryExecuter = typeof(SolrQueryExecuter<>).MakeGenericType(core.DocumentType);

Bind<ISolrConnection>().ToConstant(new SolrConnection(core.Url)).Named(coreConnectionId);
Bind(solrOperations).To(solrServer).Named(core.Id)
.WithConstructorArgument("connection", Kernel.Get<ISolrConnection>(coreConnectionId));
Bind(solrReadOnlyOperations).To(solrServer).Named(core.Id)
.WithConstructorArgument("connection", Kernel.Get<ISolrConnection>(coreConnectionId));
Bind(solrBasicOperations).To(solrBasicServer).Named(core.Id)
.WithConstructorArgument("connection", Kernel.Get<ISolrConnection>(coreConnectionId));
Bind(solrBasicReadOnlyOperations).To(solrBasicServer).Named(core.Id)
.WithConstructorArgument("connection", Kernel.Get<ISolrConnection>(coreConnectionId));
Bind(iSolrQueryExecuter).To(solrQueryExecuter).Named(core.Id)
.WithConstructorArgument("connection", Kernel.Get<ISolrConnection>(coreConnectionId));
Bind<ISolrConnection>().ToConstant(new SolrConnection(core.Url)).When(x => x.ParentRequest != null && x.ParentRequest.Service != null && x.ParentRequest.Service.IsGenericType && x.ParentRequest.Service.GetGenericArguments()[0].Equals(core.DocumentType));
Bind(solrOperations).To(solrServer);
Bind(solrReadOnlyOperations).To(solrServer);
Bind(solrBasicOperations).To(solrBasicServer);
Bind(solrBasicReadOnlyOperations).To(solrBasicServer);
Bind(iSolrQueryExecuter).To(solrQueryExecuter);
}


public override void Load() {
var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager());
Bind<IReadOnlyMappingManager>().ToConstant(mapper);
//Bind<ISolrCache>().To<HttpRuntimeCache>();
Bind<ISolrDocumentPropertyVisitor>().To<DefaultDocumentVisitor>();
Bind<ISolrFieldParser>().To<DefaultFieldParser>();
Bind(typeof (ISolrDocumentActivator<>)).To(typeof (SolrDocumentActivator<>));
Bind(typeof(ISolrDocumentResponseParser<>)).To(typeof(SolrDocumentResponseParser<>));
Bind<ISolrFieldSerializer>().To<DefaultFieldSerializer>();
Bind<ISolrQuerySerializer>().To<DefaultQuerySerializer>();

public override void Load() {
var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager());
Bind<IReadOnlyMappingManager>().ToConstant(mapper);
//Bind<ISolrCache>().To<HttpRuntimeCache>();
Bind<ISolrDocumentPropertyVisitor>().To<DefaultDocumentVisitor>();
Bind<ISolrFieldParser>().To<DefaultFieldParser>();
Bind(typeof (ISolrDocumentActivator<>)).To(typeof (SolrDocumentActivator<>));
Bind(typeof(ISolrDocumentResponseParser<>)).To(typeof(SolrDocumentResponseParser<>));
Bind<ISolrFieldSerializer>().To<DefaultFieldSerializer>();
Bind<ISolrQuerySerializer>().To<DefaultQuerySerializer>();
Bind<ISolrFacetQuerySerializer>().To<DefaultFacetQuerySerializer>();
Bind(typeof (ISolrAbstractResponseParser<>)).To(typeof (DefaultResponseParser<>));
Bind<ISolrHeaderResponseParser>().To<HeaderResponseParser<string>>();
Bind<ISolrExtractResponseParser>().To<ExtractResponseParser>();
foreach (var p in new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule),
})
Bind<IValidationRule>().To(p);
Bind(typeof (ISolrAbstractResponseParser<>)).To(typeof (DefaultResponseParser<>));
Bind<ISolrHeaderResponseParser>().To<HeaderResponseParser<string>>();
Bind<ISolrExtractResponseParser>().To<ExtractResponseParser>();
foreach (var p in new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule),
})
Bind<IValidationRule>().To(p);
Bind(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>)).To(typeof(SolrMoreLikeThisHandlerQueryResultsParser<>));
Bind(typeof(ISolrDocumentSerializer<>)).To(typeof(SolrDocumentSerializer<>));

Expand All @@ -183,6 +177,6 @@ public override void Load() {
Bind(typeof (ISolrOperations<>)).To(typeof (SolrServer<>));
Bind(typeof (ISolrReadOnlyOperations<>)).To(typeof (SolrServer<>));
}
}
}
}
}
}

0 comments on commit 003aef2

Please sign in to comment.