public IWeatherServiceAdapter CreateServiceAdapter(string serviceName)
        {
            try
            {
                var adapterType =
                    _adapterTypes.Where(t => Attribute.IsDefined(t, typeof(WeatherServiceAdapterAttribute)))
                    .Where(a => (Attribute.GetCustomAttribute(a, typeof(WeatherServiceAdapterAttribute)) as WeatherServiceAdapterAttribute).Name == serviceName)
                    .FirstOrDefault();

                var serviceAdress = Attribute.GetCustomAttribute(adapterType, typeof(WeatherServiceAddressAttribute)) as WeatherServiceAddressAttribute;

                var weatherClient = new HttpWeatherClient(serviceAdress.Address);

                IWeatherServiceAdapter adapterInstance = Activator.CreateInstance(adapterType, weatherClient) as IWeatherServiceAdapter;

                return(adapterInstance);
            }
            catch (Exception ex)
            {
                //Log adapter not found
                throw ex;
            }
        }
Example #2
0
 public WeatherService(IWeatherServiceAdapter serviceAdapter)
 {
     _serviceAdapter = serviceAdapter;
 }