< Summary

Line coverage
90%
Covered lines: 77
Uncovered lines: 8
Coverable lines: 85
Total lines: 154
Line coverage: 90.5%
Branch coverage
64%
Covered branches: 18
Total branches: 28
Branch coverage: 64.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%1100%
.ctor()100%1100%
get_Instance()100%2100%
get_Root()100%1100%
get_Entities()100%1100%
get_Queries()100%1100%
get_Mappings()100%1100%
get_Mapper()100%1100%
get_Name()100%1100%
get_Parameters()100%1100%
get_Statement()100%1100%
get_Type()100%1100%
get_Name()100%1100%
get_Type()100%1100%
get_PropertyName()100%1100%
get_FieldName()100%1100%
get_DataType()100%1100%
LoadQueries()50%286.66%
InitMapper()57.14%1482.6%
mapper(...)70%1090%

File(s)

/home/rob/github-runner/_work/snacks-bb-g1-weidinger-g1-sinnreich/snacks-bb-g1-weidinger-g1-sinnreich/src/Snacks.DAL/Common/QueryStore.cs

#LineLine coverage
 1using System.Data;
 2using System.Reflection;
 3using System.Text.Json.Serialization;
 4using Newtonsoft.Json;
 5using Snacks.DAL.Common;
 6public class QueryStore
 7{
 48    private static readonly Lazy<QueryStore> instance = new Lazy<QueryStore>(() => new QueryStore());
 29    private bool isInitialized = false;
 10    public static QueryStore Instance
 11    {
 12        get
 34413        {
 34414            if (!instance.Value.isInitialized)
 215            {
 216                instance.Value.InitMapper();
 217            }
 34418            return instance.Value;
 34419        }
 20    }
 21
 122422    public JRoot? Root { get; set; }
 23
 24    public class JRoot
 25    {
 26        [JsonPropertyName("Entities")]
 76627        public Dictionary<string, JEntity>? Entities { get; set; }
 28    }
 29
 30    public class JEntity
 31    {
 32        [JsonPropertyName("Queries")]
 30433        public List<JQuery>? Queries { get; set; }
 34
 35        [JsonPropertyName("Mappings")]
 7236        public JMapping[]? Mappings { get; set; }
 37
 24238        public Delegate? Mapper { get; set; }
 39    }
 40    public class JQuery
 41    {
 42        [JsonPropertyName("Name")]
 79043        public string? Name { get; set; }
 44
 45        [JsonPropertyName("Parameters")]
 57446        public JParameter[]? Parameters { get; set; }
 47
 48        [JsonPropertyName("Statement")]
 82249        public string? Statement { get; set; }
 50
 51        [JsonPropertyName("Type")]
 9652        public string? Type { get; set; }
 53    }
 54    public class JParameter
 55    {
 56        [JsonPropertyName("Name")]
 429657        public string? Name { get; set; }
 58
 59        [JsonPropertyName("Type")]
 33460        public string? Type { get; set; }
 61    }
 62    public class JMapping
 63    {
 64        [JsonPropertyName("PropertyName")]
 201865        public string? PropertyName { get; set; }
 66
 67        [JsonPropertyName("FieldName")]
 297068        public string? FieldName { get; set; }
 69
 70        [JsonPropertyName("DataType")]
 10471        public string? DataType { get; set; }
 72
 73    }
 74
 275    private QueryStore()
 276    {
 277        LoadQueries();
 278    }
 79
 80
 81    private void LoadQueries()
 282    {
 283        var assembly = Assembly.GetExecutingAssembly();
 284        var resourceName = "Snacks.DAL.Common.Queries.json";
 85
 286        using (var stream = assembly.GetManifestResourceStream(resourceName))
 287            if (stream == null)
 088            {
 089                throw new InvalidOperationException($"Die Datei {resourceName} konnte nicht gefunden werden.");
 90            }
 91            else
 292            {
 93
 294                using (var reader = new StreamReader(stream))
 295                {
 296                    string jsonContent = reader.ReadToEnd();
 97
 298                    Root = JsonConvert.DeserializeObject<JRoot>(jsonContent);
 299                }
 2100            }
 2101    }
 102
 103    private void InitMapper()
 2104    {
 2105        isInitialized = true;
 2106        if (Root?.Entities == null)
 0107        {
 0108            throw new InvalidOperationException("Die Entitäten konnten nicht geladen werden.");
 109        }
 110        else
 2111        {
 54112            foreach (var entity in Root.Entities)
 24113            {
 24114                var className = entity.Key.ToString();
 24115                Assembly assembly = Assembly.Load("Snacks.Domain");
 24116                var type = Type.GetType($"Snacks.Domain.Models.{className},{assembly}");
 117
 24118                if (type == null)
 0119                {
 0120                    throw new InvalidOperationException($"Der Typ {className} konnte nicht gefunden werden.");
 121                }
 122
 24123                object? instance = Activator.CreateInstance(type);
 24124                MethodInfo? mapperMethodGeneric = typeof(QueryStore).GetMethod("mapper");
 24125                MethodInfo? mapperMethod = mapperMethodGeneric is null ? throw new InvalidOperationException($"Die Metho
 126
 24127                var mappings = Root.Entities[className].Mappings;
 24128                var func = mappings is null ? throw new InvalidOperationException() : mapperMethod.Invoke(QueryStore.Ins
 24129                Root.Entities[className].Mapper = func is null ? throw new InvalidOperationException() : (Delegate)func;
 24130            }
 2131        }
 2132    }
 133    public RowMapper<T> mapper<T>(JMapping[] mappings)
 24134    {
 24135        return (IDataRecord row) =>
 204136        {
 204137            T instance = Activator.CreateInstance<T>();
 2520138            foreach (var map in mappings)
 954139            {
 954140                if (map.PropertyName is null || map.FieldName is null)
 0141                {
 0142                    throw new InvalidOperationException("Die Mappings konnten nicht geladen werden.");
 24143                }
 954144                var propertyInfo = typeof(T).GetProperty(map.PropertyName);
 954145                if (propertyInfo != null && row[map.FieldName] != DBNull.Value)
 952146                {
 952147                    propertyInfo.SetValue(instance, Convert.ChangeType(row[map.FieldName], propertyInfo.PropertyType));
 952148                }
 954149            }
 24150
 204151            return instance;
 228152        };
 24153    }
 154}