Oleg Sh
Oleg Sh

Reputation: 9013

How to implement better

I create a service, which get some information from different APIs. Each API works with language list, some language are common, some are individual for concrete API. I create an Interface with universal methods to call API.

public interface IOCR
{
    Task<OCRResult> GetTextAsync(string fileUrl, Language language);
}

where language is enum like:

public enum Language
{
    Chinese_Simplified,
    Chinese_Traditional,
    Danish,
    Dutch,
    ....
}

but first API has also Russian, second API German for example. How to implement it correctly?

Option 1: Create full list of all languages. But then client application does not know whether the language is supported or not.

Option 2: Pass simple string instead of enum. But then we should know which strings are valid for concrete API.

It would be good with base enum and then pass inherited enum, but enums does not allow inheritance

Upvotes: 2

Views: 62

Answers (1)

dotNET
dotNET

Reputation: 35400

In addition to GetTextAsync, also add another method in your interface named GetLanguages that returns a Language[] so the caller could ask the API about the languages supported by it at runtime. This has the added benefit of easily extending your APIs in the future without making any modifications on the service end.

Upvotes: 2

Related Questions