jeudi 10 mars 2022

C# refactoring Facade for API

I create a dll for my application. I am using Facade design pattern for encapsulate API who makes some programmers (not my organization) because they decision is uncomfortable.

Their API works like this:

  1. Initialize object of DiadocApi
  2. Auth for getting token

For initialize DiadocApi object I need developerKey (get for a subscription). For authorization I need login, password.

My decision badly because it's a singleton and I need make unit-tests. That can I change in my code?

//I GET IT FROM NUGET PACKAGE
using Diadoc.Api;

public sealed partial class DiadocApiFacade
{
    private static readonly object _mutex = new object();

    private static DiadocApiFacade _instance;
    private string _token;
    private DiadocApi _api;

    private DiadocApiFacade() { }

    public static string DefaultUrl => "url was here";
    public string DefaultFromBoxId { get; set; }
    public DiadocApi Api { get => _api; private set => _api = value; }
    
    public static DiadocApiFacade GetInstance()
    {
        if (_instance == null)
        {
            lock (_mutex)
            {
                if (_instance == null)
                {
                    _instance = new DiadocApiFacade();
                }
            }
        }
        return _instance;
    }

    public string Authenticate(string login, string password, string privateDeveloperKey)
    {
        if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(developerKey))
            throw new ArgumentNullException();

        Api = new DiadocApi(developerKey, DefaultUrl, new WinApiCrypt());

        return _token = _api.Authenticate(login, password);
    }

    //method for example, >60% methods like that
    public Document GetDocument(string messageId, string documentId, string boxId = null)
    {
        return _api.GetDocument(_token, boxId ?? DefaultFromBoxId, messageId, documentId);
    }
}

Aucun commentaire:

Enregistrer un commentaire