27
loading...
This website collects cookies to deliver better user experience
Microsoft.Extensions.DependencyInjection
// Build configuration
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
// Boostrapping the app
var serviceProvider = new ServiceCollection()
.AddSingleton(configuration)
.AddSingleton<IWeatherstackService, WeatherstackService>()
.AddSingleton<IQuestionService, QuestionService>()
.AddSingleton<IUserInteractionService, UserInteractionService>()
.BuildServiceProvider();
var weatherApp = serviceProvider.GetService<IUserInteractionService>();
// Start the App
weatherApp.Start();
WeatherStackService
will handle the http request to WeatherStack. I've used Flurl
as a http request client because of sugar syntax it provides as well as simplicity. QuestionService
will handle the pre-defined questions, as well as validation or the actual answering of the question. So let's say its a question if "Is it raining?" some sort of that, then this service will try to answer that based on info we will get from WeatherService
.UserInteractionService
, this service will handle the grunt work. It will pretty much will look like thisprivate readonly IQuestionService _questionService;
private readonly IWeatherstackService _weatherstackService;
public UserInteractionService(IWeatherstackService weatherstackService,
IQuestionService questionService)
{
_weatherstackService = weatherstackService;
_questionService = questionService;
}
UserInteractionService
graphviz
or PlantUML
to visualize each of your components.private WeatherStackResponse AskZipCode()
{
....
....
var task = Task.Run(async () => await _weatherstackService.GetWeatherInfo(zipCode));
var result = task.Result;
if (result?.location != null)
{
Console.WriteLine(string.Format(Constants.ZipCodeValid,
result?.location.name, result?.location.country, result?.location.region));
return result;
}else{
....
}
}
private void AskQuestion(WeatherStackResponse weatherInfo)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(Constants.SelectQuestionMessage);
var questions = _questionService.GetQuestions();
var selectedQuestion = UIUtils.PrintQuestion(questions);
// Generate Answer
var result = _questionService.AnswerQuestion(selectedQuestion, weatherInfo);
....
....
}