DEV Community

Discussion on: Clean Architecture Essentials

Collapse
 
darianbenito profile image
Darian Benito • Edited

Hi Ivan! I have a new question: What about if we need to consume an external api ? We need to create an interface IExternalService in the Application proyect, but where is the place to implement this interface? Infrastructure ? Thanks!

Collapse
 
ivanpaulovich profile image
Ivan Paulovich

Hi @darianbenito ,

I had to take a long break from laptops last weeks and coming back to routine slowly. Let's see if I could clarify you:

In the Domain you design your Model putting your business logic in priority. Suppose your application gives you suggestions of what to do based on the local weather.

namespace Domain
{
    using System;

    public interface IWeather
    {
        decimal Temperature { get; }
        string GetDailySuggestions();
    }

   public interface IWeatherGateway
   {
        Task<IWeather> GetWeather();
    }

    public abstract class Weather : IWeather
    {
        public abstract decimal Temperature { get; }

        public string GetDailySuggestions()
        {

            //
            // The business logic need to know the temperature outside then you
            // write your code against the interface
           //

            if (this.Temperature < 10)
            {
                return "Stay at home. Watch Netflix.."
            }
            else
            {
                return "Go walk in the forest."
            }
        }
    }
}

Later you write a weather provider:

namespace Infrastructure.TheWeatherChannelProvider
{
    using System;

    public sealed class AccuWeather : Domain.Weather
    {
        public Customer(decimal temperature)
        {
        }

        public override decimal Temperature { get; }
    }

    public sealed class AccuWeatherGateway : Domain.IWeatherGateway
    {
        private readonly _accuWeatherUrl;

        public UserRepository(string accuWeatherUrl)
        {
            this._accuWeatherUrl = _accuWeatherUrl;
        }

        public async Task<IWeather> GetWeather()
        {
            //
            // HTTP client code ommited.
            //

            decimal localTemperature = 42;

            return new Weather(localTemperature);
        }
    }
}

Thanks again for reaching me out, I really appreciate feedback.
I hope this would give you insights.

Best,
Ivan

Collapse
 
darianbenito profile image
Darian Benito

Thanks Ivan!