API Wrapping to Support Dependency Injection

I interact with an API that has only static functions and cannot be opened and modified.

    public class WindowsNativeGraphAPI
    {
        public static IEnumerable<IGraphData> GetGraphData();
        public static bool DeleteGraphData(IGraphData data);
    }

I would like to be able to pass the API to a function or constructor and do the dependency injection (in case we change the API later).

public void GatherGraphData(IGraphAPI api)
{...}

For this API to be passed as a parameter, I would need to at least abstract the use of the interface to go into the function.

    public interface IGraphAPI
    {
        IEnumerable<IGraphData> GetGraphData();
        bool DeleteGraphData(IGraphData data);
    }

However, I would then need to implement the interface in another class, since I cannot change the original API. This class would be an easy API wrapper that simply calls the corresponding function in the API and returns the same result.

    public class WindowsGraphAPI : IGraphAPI
    {
        public IEnumerable<IGraphData> GetGraphData()
        {
            return WindowsNativeGraphAPI.GetGraphData();
        }

        public bool DeleteGraphData(IGraphData data)
        {
            return WindowsNativeGraphAPI.DeleteGraphData(data)
        }
    }

API. , API, ? , API. API, , .

API, , .

? ?

+3
2

, . API - , - .

+6

, . , , , , , DI. , , API . , , . api excpetion.

0

Source: https://habr.com/ru/post/1754989/


All Articles