aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Formulation/FormulaResolver.cs
blob: d30d077b81cdefa6fd0416575382f2b7459005b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Integration.Observables;
using Tango.Integration.Printing;
using System.Reflection;

namespace Tango.Integration.Formulation
{
    public static class FormulaResolver
    {
        private static List<KeyValuePair<IdsPackFormulas, IFormulaCalculator>> _calculators { get; set; }

        static FormulaResolver()
        {
            _calculators = new List<KeyValuePair<IdsPackFormulas, IFormulaCalculator>>();

            foreach (var type in typeof(FormulaResolver).Assembly.GetTypes().Where(x => x.GetCustomAttribute<FormulaAttribute>() != null))
            {
                _calculators.Add(new KeyValuePair<IdsPackFormulas, IFormulaCalculator>(type.GetCustomAttribute<FormulaAttribute>().Formula, Activator.CreateInstance(type) as IFormulaCalculator));
            }
        }

        public static IFormulaCalculator Resolve(LiquidVolume liquidVolume)
        {
            return _calculators.SingleOrDefault(x => x.Key == (IdsPackFormulas)liquidVolume.IdsPack.IdsPackFormula.Code).Value;
        }

        public static IFormulaCalculator Resolve(IdsPackFormulas formulaCode)
        {
            return _calculators.SingleOrDefault(x => x.Key == formulaCode).Value;
        }
    }
}