To make it easier to change the external weather service and to make the code more testable, we will create an interface for the service. Here's how we go about it:
- In the Weather project, create a new folder called Services.
- Create a new public interface called IWeatherService.
- Add a method for fetching data based on the location of the user, as shown in the following code. Name the method GetForecast:
public interface IWeatherService
{
Task<Forecast> GetForecast(double latitude, double longitude);
}
When we have an interface, we can create an implementation for it, as follows:
- In the Services folder, create a new class called OpenWeatherMapWeatherService.
- Implement the interface and add the async keyword to the GetForecast method.
- The code should look as follows:
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using...