// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; namespace SocialWeather { public class FormatterResolver { private IServiceProvider _serviceProvider; private Dictionary> _formatters = new Dictionary>(); public FormatterResolver(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public void AddFormatter(string formatType) where TFormatterType : IStreamFormatter { Dictionary typeFormatters; if (!_formatters.TryGetValue(formatType, out typeFormatters)) { typeFormatters = _formatters[formatType] = new Dictionary(); } typeFormatters[typeof(T)] = typeof(TFormatterType); } public IStreamFormatter GetFormatter(string formatType) { Dictionary typeFormatters; Type typeFormatterType; if (_formatters.TryGetValue(formatType, out typeFormatters) && typeFormatters.TryGetValue(typeof(T), out typeFormatterType)) { return (IStreamFormatter)_serviceProvider.GetRequiredService(typeFormatterType); } return null; } } }