Exemplo n.º 1
0
        static async Task <RulesCache> LoadRules(IRulesLoader rulesLoader)
        {
            RulesCache cache = new RulesCache(new ConcurrentDictionary <string, RuleEngine>(), new ConcurrentDictionary <string, string>());

            var assembly = IntrospectionExtensions.GetTypeInfo(typeof(FlowActivity)).Assembly;

            string[] embeddedResources = GetResourceNames(assembly);

            Dictionary <string, string> rules = embeddedResources
                                                .Where(f => f.EndsWith(".module"))
                                                .ToDictionary(f => GetKey(f).ToLowerInvariant());

            Dictionary <string, string> resources = embeddedResources
                                                    .Where(f => f.EndsWith(".resources"))
                                                    .ToDictionary(f => GetKey(f).ToLowerInvariant());

            await Task.WhenAll
            (
                rules.Keys.Select
                (
                    key => rulesLoader.LoadRulesOnStartUp
                    (
                        new RulesModuleModel
            {
                Name            = key,
                ResourceSetFile = GetBytes(resources[key], assembly),
                RuleSetFile     = GetBytes(rules[key], assembly)
            },
                        cache
                    )
                )
            );

            return(cache);

            string GetKey(string fullResourceName)
            => Path.GetExtension(Path.GetFileNameWithoutExtension(fullResourceName)).Substring(1);
        }
Exemplo n.º 2
0
        //private readonly ILogger<RulesLoader> _logger;
        #endregion Fields

        #region Methods
        public async Task LoadRulesOnStartUp(RulesModuleModel module, RulesCache cache)
        {
            await Task.Run
            (
                () =>
            {
                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));
                }
            }
            );
        }