Example #1
0
        public async Task <RulesCache> LoadRulesOnStartUp()
        {
            try
            {
                ICollection <RulesModuleModel> modules = (await this._repository.GetItemsAsync <RulesModuleModel, RulesModule>(t => t.Application == this._applicationOptions.ApplicationName));

                return(modules.Aggregate(new RulesCache(new Dictionary <string, RuleEngine>(), new Dictionary <string, string>()), (cache, module) =>
                {
                    string moduleName = module.Name.ToLowerInvariant();
                    RuleSet ruleSet = module.DeserializeRuleSetFile();
                    if (ruleSet == null)
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.invalidRulesetFormat, moduleName));
                    }

                    cache.RuleEngines.Add(moduleName, new RuleEngine(ruleSet, RulesSerializer.GetValidation(ruleSet)));

                    using (IResourceReader reader = new ResourceReader(new MemoryStream(module.ResourceSetFile)))
                    {
                        reader.OfType <DictionaryEntry>()
                        .ToList()
                        .ForEach(entry => cache.ResourceStrings.Add((string)entry.Key, (string)entry.Value));
                    }

                    return cache;
                }));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex, ex.Message);
                return(new RulesCache(new Dictionary <string, RuleEngine>(), new Dictionary <string, string>()));
            }
        }
        public async Task LoadSelectedModules(List <string> modules)
        {
            if (this._rulesCache == null)
            {
                throw new InvalidOperationException(Properties.Resources.rulesCacheCannotBeNull);
            }

            if (modules.Count < 1)
            {
                return;
            }

            List <string> queryItems = modules.Select(item => string.Concat(item, PIPE, this._applicationOptions.ApplicationName)).ToList();
            ICollection <RulesModuleModel> rulesModules = (await this._repository.GetItemsAsync <RulesModuleModel, RulesModule>(item => queryItems.Contains(item.NamePlusApplication)));

            this._rulesCache = rulesModules.Aggregate(this._rulesCache, (cache, next) =>
            {
                string moduleName = next.Name.ToLowerInvariant();
                lock (cache)
                {
                    RuleSet ruleSet = next.DeserializeRuleSetFile();
                    if (ruleSet == null)
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.invalidRulesetFormat, moduleName));
                    }

                    if (cache.RuleEngines.ContainsKey(moduleName))
                    {
                        cache.RuleEngines[moduleName] = new RuleEngine(ruleSet, RulesSerializer.GetValidation(ruleSet));
                    }
                    else
                    {
                        cache.RuleEngines.Add(moduleName, new RuleEngine(ruleSet, RulesSerializer.GetValidation(ruleSet)));
                    }

                    using (IResourceReader reader = new ResourceReader(new MemoryStream(next.ResourceSetFile)))
                    {
                        reader.OfType <DictionaryEntry>()
                        .ToList()
                        .ForEach(entry =>
                        {
                            string resourceKey = (string)entry.Key;
                            if (cache.ResourceStrings.ContainsKey(resourceKey))
                            {
                                cache.ResourceStrings[resourceKey] = (string)entry.Value;
                            }
                            else
                            {
                                cache.ResourceStrings.Add(resourceKey, (string)entry.Value);
                            }
                        });
                    }
                }

                return(cache);
            });
        }