Skip to content

Instantly share code, notes, and snippets.

@nchaly
Created March 28, 2014 06:24
Show Gist options
  • Select an option

  • Save nchaly/9826496 to your computer and use it in GitHub Desktop.

Select an option

Save nchaly/9826496 to your computer and use it in GitHub Desktop.
PostSolrConnection registration with Unity
<solr>
<server id="collection1" url="http://localhost:8983/solr/c1" documentType="[Type1]" />
<server id="collection2" url="http://localhost:8983/solr/c2" documentType="[Type2]" />
</solr>
class Search
{
public Search(
//usual solr query interface
ISolrBasicReadOnlyOperations<Type1> solrOperations,
//interface which uses Post when query is too long for Get
[Dependency("solr-post")]
ISolrBasicReadOnlyOperations<Type1> solrPostOperations)
{
}
}
public class SolrUnityModule
{
public static void RegisterSolrOperations(UnityContainer container, bool registerPostOperations)
{
//register regular implementation
var solrConfig = (SolrConfigurationSection)ConfigurationManager.GetSection("solr");
new SolrNetContainerConfiguration().ConfigureContainer(solrConfig.SolrServers, container);
//PostConnection registration
if (registerPostOperations)
{
RegisterPostOperations(container, solrConfig);
}
}
private static void RegisterPostOperations(UnityContainer container, SolrConfigurationSection solrConfig)
{
//for each core...
foreach (var server in (IEnumerable<SolrServerElement>)solrConfig.SolrServers)
{
var documentType = Type.GetType(server.DocumentType);
//name for PostConnection registration
var name = server.Id + typeof(PostSolrConnection);
//name for standard connection registration.
//needed as a dependency for PostConnection
var basicName = server.Id + typeof(SolrConnection);
//register named PostConnection
container.RegisterType<ISolrConnection, PostSolrConnection>(
name,
new InjectionConstructor(
new ResolvedParameter(typeof(ISolrConnection), basicName),
server.Url)
);
//register ISolrQueryExecuter<Type> which will consume PostSolrConnection
//registration name is "solr-post"
//TODO: it is the same registration for different "named" cores per type
//TODO: probably more generic way will be server.Id + "solr-post"
//TODO: but dependency consumer should be aware of this specifics
var iSolrQueryExecuter = typeof(ISolrQueryExecuter<>).MakeGenericType(documentType);
var solrQueryExecuter = typeof(SolrQueryExecuter<>).MakeGenericType(documentType);
container.RegisterType(
iSolrQueryExecuter, solrQueryExecuter, "solr-post",
new InjectionConstructor(
new ResolvedParameter(typeof(ISolrAbstractResponseParser<>).MakeGenericType(documentType)),
new ResolvedParameter(typeof(ISolrConnection), name),
new ResolvedParameter(typeof(ISolrQuerySerializer)),
new ResolvedParameter(typeof(ISolrFacetQuerySerializer)),
new ResolvedParameter(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>).MakeGenericType(documentType))));
var injectionParameters = new InjectionConstructor(
new ResolvedParameter(typeof(ISolrConnection), name), //POST connection
new ResolvedParameter(typeof(ISolrQueryExecuter<>).MakeGenericType(documentType), "solr-post"),
new ResolvedParameter(typeof(ISolrDocumentSerializer<>).MakeGenericType(documentType)),
new ResolvedParameter(typeof(ISolrSchemaParser)),
new ResolvedParameter(typeof(ISolrHeaderResponseParser)),
new ResolvedParameter(typeof(ISolrQuerySerializer)),
new ResolvedParameter(typeof(ISolrDIHStatusParser)),
new ResolvedParameter(typeof(ISolrExtractResponseParser)));
//register ISolrBasicReadOnlyOperations<Type> which consumes
// queryexecutor registered above
// and postconnection
container.RegisterType(
typeof(ISolrBasicReadOnlyOperations<>).MakeGenericType(documentType),
typeof(SolrBasicServer<>).MakeGenericType(documentType),
"solr-post",
injectionParameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment