I have multiple, multilingual "websites" running under a single web application. Each "locale" has its own Solr core (with the same fields across all locales). I'm using SolrNet to perform my search queries.
In order to switch cores on a per-request basis, I am registering a named instance of ISolrOperations<SearchResult>
in my Autofac container for each locale (for those unfamiliar with SolrNet, this is effectively my entry point into the library for query purposes).
I then have an ISearchService
interface, with a concrete SolrSearchService
implementation, whose constructor signature looks like this:
public SolrSearchService(ISolrOperations<SearchResult> solr)
In order to dynamically switch cores on each request, my controller takes an (injected) ISearchServiceFactory
instead of simply an ISearchService
. My SolrSearchServiceFactory
looks like this:
public class SolrSearchServiceFactory : ISearchServiceFactory
{
private readonly IComponentContext _ctx;
public SolrSearchServiceFactory(IComponentContext ctx)
{
_ctx = ctx;
}
public ISearchService Create(string id)
{
var result = _ctx.ResolveNamed<ISolrOperations<SearchResult>>(id);
return new SolrSearchService(result);
}
}
id
is simply the locale identifier. This is the best I've managed to do in order to decouple Autofac from my services/controllers but maintain the ability to switch cores per request (based on logic performed in the controller).
I'm still not very happy with this, however, as the factory itself is still coupled to Autofac. Is there a way around this (either from a SolrNet or Autofac perspective)?
I have looked at using Autofac's factory delegates but there doesn't seem to be a way to apply them in this instance.