The Client class, abstracted by the IClient interface, acts as a gateway or façade for the EPiServer Find service. The class covers most operations that you would want to perform, such as indexing objects, getting documents, deleting documents, and searching. The Client class methods do not, however, map directly to the service. These methods do enforce several conventions, most of which can be customized, to provide additional functionality and a more convenient way of working with the service. The Client is further enhanced by several extension methods, most notably the Search method, the starting point for building search requests using a fluent API.
Obtaining an IClient instance
The client class can be instantiated directly, but its constructor requires a number of parameters. As these parameters primarily concern configuration details, the Client class also has a parameter-less static method, CreateFromConfig, that returns an instance of the class based on configuration settings. To use the Client.CreateFromConfig method, you must insert a configuration section of type EPiServer.Find.Configuration into the web.config/app.config file.
Example:
XML
<configuration>
<configSections>
<section
name="episerver.find"
type="EPiServer.Find.Configuration, EPiServer.Find" requirePermission="false"/>
</configSections>
<episerver.find
serviceUrl="http://..."
defaultIndex="myindex"/>
</configuration>
With that in place, you can easily obtain an instance of the Client class.
C#
IClient client = Client.CreateFromConfig();
Managing instances
Creating an instance of the Client class is not a very costly operation but performance may be affected. In many scenarios, you also want to modify the instance prior to usage, meaning you want to instantiate it on application startup. Therefore, it is recommended to only use a single instance per application, or a few long-lived instances with different settings/conventions.
You can accomplish this in several ways, depending on the type of application in which the Client class is being used. In an ASP.NET MVC web site, you can configure the DependencyResolver to automatically inject the instance into controllers. Likewise, an Inversion of Control container can inject the instance into the presenter in a Web Forms site that uses the MVP pattern. In other situations, you can manage the instance in a custom class in a Singleton-like fashion. The following example illustrates how to wire up injection of the Client class into controllers in an ASP.NET site that uses the Autofac MVC integration.
C#
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.Register(x =>
CreateSearchClient()).As<IClient>().SingleInstance();
var container = builder.Build();
DependencyResolver.SetResolver(
new AutofacDependencyResolver(container));
}
private static Client CreateSearchClient()
{
var client = Client.CreateFromConfig();
return client;
}
Do you find this information helpful? Please log in to provide feedback.