Skip to content

Instantly share code, notes, and snippets.

@snickroger
Created October 15, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snickroger/637c6a352e412c3341565b201f5288d0 to your computer and use it in GitHub Desktop.
Save snickroger/637c6a352e412c3341565b201f5288d0 to your computer and use it in GitHub Desktop.
DI Demo - Take 1
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using DIDemo.Model;
using DIDemo.Modules.WeatherService;
namespace DIDemo
{
public interface IDIDemoService
{
Task DoWork();
}
public class DIDemoService : IDIDemoService
{
private readonly TextWriter _writer;
private readonly TextReader _reader;
private readonly IWeatherService _weatherService;
public DIDemoService(TextWriter textWriter, TextReader textReader, IWeatherService weatherService)
{
_writer = textWriter;
_reader = textReader;
_weatherService = weatherService;
}
public async Task DoWork()
{
await _writer.WriteAsync("Please enter your postal code: ");
var postalCode = await _reader.ReadLineAsync();
var forecast = await _weatherService.GetForecast(postalCode);
await _writer.WriteLineAsync(GetForecastString(forecast, postalCode));
}
private static string GetForecastString(WeatherForecast forecast, string postalCode)
{
var temp = forecast.TemperatureType == TemperatureType.Celsius
? $"{forecast.Temperature:0.0}° C"
: $"{forecast.Temperature:0}° F";
return $"Today will be {temp} and {forecast.Weather} in {postalCode}.";
}
}
}
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DIDemo.Modules.WeatherService;
using DIDemo.Modules.WeatherService.Api;
using DIDemo.Modules.WeatherService.Parser;
using Microsoft.Extensions.DependencyInjection;
namespace DIDemo
{
class Program
{
static async Task Main(string[] args)
{
var serviceCollection =
new ServiceCollection()
.AddSingleton<Func<string, IWeatherServiceApi>>(WeatherServiceApiFactory)
.AddSingleton<Func<string, IWeatherServiceParser>>(WeatherServiceParserFactory)
.AddSingleton<TextWriter>(Console.Out)
.AddSingleton<TextReader>(Console.In)
.AddSingleton<IWeatherService, WeatherService>()
.AddSingleton<IDIDemoService, DIDemoService>();
var serviceProvider = serviceCollection.BuildServiceProvider();
await serviceProvider.GetService<IDIDemoService>().DoWork();
}
private static Func<IServiceProvider, Func<string, IWeatherServiceApi>> WeatherServiceApiFactory =>
service =>
{
return
postalCode =>
{
if (IsUsZipCode(postalCode))
{
return new UsWeatherServiceApi();
}
if (IsCanadianPostalCode(postalCode))
{
return new CanadaWeatherServiceApi();
}
throw new ArgumentException("Could not determine location");
};
};
private static Func<IServiceProvider, Func<string, IWeatherServiceParser>> WeatherServiceParserFactory =>
service =>
{
return
postalCode =>
{
if (IsUsZipCode(postalCode))
{
return new UsWeatherServiceParser();
}
if (IsCanadianPostalCode(postalCode))
{
return new CanadaWeatherServiceParser();
}
throw new ArgumentException("Could not determine location");
};
};
private static bool IsUsZipCode(string postalCode)
{
return postalCode.Length == 5 && postalCode.All(char.IsDigit);
}
private static bool IsCanadianPostalCode(string postalCode)
{
var canadianRegex = new Regex(@"^[A-VXY][0-9][A-Z]\s[0-9][A-Z][0-9]$");
return canadianRegex.IsMatch(postalCode);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DIDemo.Model;
using DIDemo.Modules.WeatherService.Api;
using DIDemo.Modules.WeatherService.Parser;
namespace DIDemo.Modules.WeatherService
{
public interface IWeatherService
{
Task<WeatherForecast> GetForecast(string postalCode);
}
public class WeatherService : IWeatherService
{
private readonly Func<string, IWeatherServiceApi> _apiFactory;
private readonly Func<string, IWeatherServiceParser> _parserFactory;
public WeatherService(Func<string, IWeatherServiceApi> apiFactory,
Func<string, IWeatherServiceParser> parserFactory)
{
_apiFactory = apiFactory;
_parserFactory = parserFactory;
}
public async Task<WeatherForecast> GetForecast(string postalCode)
{
var api = _apiFactory(postalCode);
var parser = _parserFactory(postalCode);
var apiResponse = await api.GetApiResponse(postalCode);
return parser.GetForecast(apiResponse);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment