Home > Enterprise >  Create Abstraction layer for Elastic Search NEST client. What should be the return type?
Create Abstraction layer for Elastic Search NEST client. What should be the return type?

Time:12-30

I am trying to create an abstraction layer over the NEST Elastic search client as I do not want business logic to be dependent upon the client so I can change the client if I have to. But what should be the response type? I am not able to use Generic type as it gives me an error:

public class StorageClient : IStorageClient
    {
        private readonly IElasticSearchClient _elasticSearchClient;
        public StorageClient(IElasticSearchClient elasticSearchClient)
        {
            _elasticSearchClient = elasticSearchClient;
        }
        public async Task<T> AddData<T>(T requestObject, string index) where T : class
        {
            var response = await _elasticSearchClient.addIndex(requestObject, index);

            return response; I get an error here that cannot convert the NEST index response to type T
        }
    }

Here is the Elastic Search Client

 public async Task<IndexResponse> addIndex<T>(T document, string index) where T : class
        {

            _client = CreateElasticClient();

            return await _client.IndexAsync(elasticValues, idx => idx.Index(index.ToLower()));
        }

CodePudding user response:

The signature of

public async Task<T> AddData<T>(T requestObject, string index) where T : class

would need to return a requestObject type, but is returning a task of NEST IndexResponse type.

I would question the value of having both a abstraction around NEST in the form of IElasticSearchClient, and an abstraction around storage in the form of IStorageClient. It seems like a concrete implementation of IStorageClient that uses the NEST client directly would be sufficient.

  • Related