Skip to content

Commit 7e6809d

Browse files
committed
Implement weather evaluators factory
1 parent e25b40d commit 7e6809d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using RealTimeWeatherMonitoringApp.Domain.Interfaces;
2+
using RealTimeWeatherMonitoringApp.Domain.Models;
3+
using RealTimeWeatherMonitoringApp.Infrastructure.Configuration;
4+
using RealTimeWeatherMonitoringApp.Infrastructure.Evaluators;
5+
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces.Factory;
6+
7+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Factory;
8+
9+
public class WeatherEvaluatorFactory : IEvaluatorFactory<WeatherData>
10+
{
11+
public IEvaluator<WeatherData> CreateEvaluator(ConditionConfiguration config)
12+
{
13+
var value = config.Value;
14+
var comparison = GetComparisonOperator(config.Operator);
15+
return config.Type.ToLower() switch
16+
{
17+
"temperature" => new WeatherTemperatureEvaluator(value, comparison),
18+
"humidity" => new WeatherHumidityEvaluator(value, comparison),
19+
_ => throw new ArgumentException($"Unsupported condition type: {config.Type}")
20+
};
21+
}
22+
23+
private Func<double, double, bool> GetComparisonOperator(string @operator)
24+
{
25+
return @operator switch
26+
{
27+
"greaterThan" => (x, y) => x > y,
28+
"lessThan" => (x, y) => x < y,
29+
_ => throw new ArgumentException($"Unsupported operator: {@operator}")
30+
};
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using RealTimeWeatherMonitoringApp.Domain.Interfaces;
2+
using RealTimeWeatherMonitoringApp.Infrastructure.Configuration;
3+
4+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Interfaces.Factory;
5+
6+
public interface IEvaluatorFactory<in TEvaluated>
7+
{
8+
/// <exception cref="ArgumentException">When the configuration is invalid for the output data type</exception>
9+
IEvaluator<TEvaluated> CreateEvaluator(ConditionConfiguration configuration);
10+
}

0 commit comments

Comments
 (0)