algolia / algoliasearch-client-csharp

⚡️ A fully-featured and blazing-fast C# API client to interact with Algolia.
https://www.algolia.com/doc/api-client/getting-started/install/csharp/
MIT License
120 stars 61 forks source link

Consider returning and depending on interfaces in `ISearchClient` #840

Closed AnuBargreen closed 2 months ago

AnuBargreen commented 1 year ago

Description

ISearchClient should depend on interfaces and not concrete types. This breaks Liskov substitution principle when testing this. The concrete type SearchIndex has a internal constructor making it harder to wrap this for testing.

https://github.com/algolia/algoliasearch-client-csharp/blob/0c2faadf050c9c9801500d7bc603fdbc33aad696/src/Algolia.Search/Clients/ISearchClient.cs#L45-L50

Steps To Reproduce

Use Moq to create a Mock of SearchClient and setup responses to various calls:

using Moq;
using Moq.AutoMock;

AutoMocker _mocker = new();
var searchClient = _mocker.GetMock<ISearchClient>();
var searchIndex = _mocker.GetMock<SearchIndex>(); // Can't use `ISearchIndex` here because `InitIndex` returns Concrete type with internal constructor
searchClient.Setup(b => b.InitIndex("test_items")).Returns((SearchIndex)searchIndex.Object);
rmendoza-prescriber commented 1 year ago

Would be nice if this change can be made in the next release, I'm having issues trying to write unit test using this client

PritamSanganiTW commented 1 year ago

+1 to this. It is very difficult to unit test interactions with the ISearchClient. Also related to #745

morganleroi commented 8 months ago

Hello,

We will release in few days a new major version of all Algolia C# Clients. In this version, all clients returns interfaces to enhance testability.

I'll reply in this thread as soon as the client is released.

morganleroi commented 8 months ago

@rmendoza-prescriber @PritamSanganiTW @AnuBargreen

The Alpha version is now released on Nuget.

If you have time, we need customer feedback about this new version. It's still not GA so you may not want to use it in production.

There is no more deps on Newtonsoft, all Algolia APIs are now available (Insights, Recommend, Query Perso, ...) and all models and routes are synced.

To help you migrate, there is a Migration guide.

Happy to help in your migration process !

millotp commented 2 months ago

Hello, in version 7 all clients extends from an interface, closing this issue.