This content is archived. See latest version here.

Last updated: Jun 10 2014

Introduction

The Client class, abstracted by the IClient interface, acts as a gateway or façade for the EPiServer Find service encapsulating most operations that one would want to perform such as indexing objects, getting documents, deleting documents and searching. The methods of the Client class does not however map directly to the service but enforces a number of 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 a number of extension methods, most notably the Search method which is 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 named CreateFromConfig that returns an instance of the class based on configuration settings.

To be able to use the Client.CreateFromConfig method, a configuration section of type EPiServer.Find.Configuration is required in web.config/app.config.

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, an instance of the Client class can easily be obtained.

C#
IClient client = Client.CreateFromConfig();

Managing instances

Creating an instance of the Client class is not a very costly operation, however performance may be affected. In many scenarios you will also want to modify the instance prior to usage, meaning you will probably 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.

This can be accomplished in a number of ways depending on what type of application it is being used in. In an ASP.NET MVC web site the DependencyResolver can be configured 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 the instance can be managed in a custom class in a Singleton-like fashion.

The below 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();
    //Any modifications required goes here    
    return client;
}

Comments