Skip to content

Commit

Permalink
Support named registration via Autofac (useful when using multiple co…
Browse files Browse the repository at this point in the history
…res with the same document type)
  • Loading branch information
spudstuff committed Nov 6, 2012
1 parent f36a934 commit e89b2ff
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
91 changes: 91 additions & 0 deletions AutofacContrib.SolrNet.Tests/AutofacMulticoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using MbUnit.Framework;
using SolrNet;
using SolrNet.Impl;
using System.Collections.Generic;

namespace AutofacContrib.SolrNet.Tests
{
Expand Down Expand Up @@ -102,6 +103,96 @@ public void ResolveSolrOperations_withMultiCore()
Assert.IsTrue(solrOperations2 is SolrServer<Entity2>);
}

[Test]
public void ResolveSolrOperations_viaNamedWithMultiCore()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "entity1",
DocumentType = typeof (Entity1).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity1",
},
new SolrServerElement {
Id = "entity2",
DocumentType = typeof (Entity2).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity2",
},
};

builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();

// Act
var solrOperations1 = container.ResolveNamed<ISolrOperations<Entity1>>("entity1");
var solrOperations2 = container.ResolveNamed<ISolrOperations<Entity2>>("entity2");

// Assert
Assert.IsTrue(solrOperations1 is SolrServer<Entity1>);
Assert.IsTrue(solrOperations2 is SolrServer<Entity2>);
}

[Test]
public void ResolveSolrOperations_viaNamedWithMultiCoreForDictionary()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "dictionary1",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity1",
},
new SolrServerElement {
Id = "dictionary2",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity2",
},
};

builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();

// Act
var solrOperations1 = container.ResolveNamed<ISolrOperations<Dictionary<string, object>>>("dictionary1");
var solrOperations2 = container.ResolveNamed<ISolrOperations<Dictionary<string, object>>>("dictionary2");

// Assert
Assert.IsTrue(solrOperations1 is SolrServer<Dictionary<string, object>>);
Assert.IsTrue(solrOperations2 is SolrServer<Dictionary<string, object>>);
}

[Test]
public void ResolveSolrReadOnlyOperations_viaNamedWithMultiCoreForDictionary()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "dictionary1",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity1",
},
new SolrServerElement {
Id = "dictionary2",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity2",
},
};

builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();

// Act
var solrReadOnlyOperations1 = container.ResolveNamed<ISolrReadOnlyOperations<Dictionary<string, object>>>("dictionary1");
var solrReadOnlyOperations2 = container.ResolveNamed<ISolrReadOnlyOperations<Dictionary<string, object>>>("dictionary2");

// Assert
Assert.IsTrue(solrReadOnlyOperations1 is SolrServer<Dictionary<string, object>>);
Assert.IsTrue(solrReadOnlyOperations2 is SolrServer<Dictionary<string, object>>);
}

[Test]
public void ResolveSolrOperations_fromConfigSection()
{
Expand Down
2 changes: 2 additions & 0 deletions AutofacContrib.SolrNet/SolrNetModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ private static void RegisterCore(SolrCore core, ContainerBuilder builder) {
var SolrServer = typeof (SolrServer<>).MakeGenericType(core.DocumentType);

builder.RegisterType(SolrServer)
.Named(core.Id, ISolrOperations)
.As(ISolrOperations)
.WithParameters(new[] {
new ResolvedParameter((p, c) => p.Name == "basicServer", (p, c) => c.ResolveNamed(core.Id + SolrBasicServer, ISolrBasicOperations)),
});

builder.RegisterType(SolrServer)
.Named(core.Id, ISolrReadOnlyOperations)
.As(ISolrReadOnlyOperations)
.WithParameters(new[] {
new ResolvedParameter((p, c) => p.Name == "basicServer", (p, c) => c.ResolveNamed(core.Id + SolrBasicServer, ISolrBasicOperations)),
Expand Down

0 comments on commit e89b2ff

Please sign in to comment.