Ejemplo n.º 1
0
        public virtual async Task <SimpleStateCheckerResult <TState> > IsEnabledAsync(TState[] states)
        {
            var result = new SimpleStateCheckerResult <TState>(states);

            using (var scope = ServiceProvider.CreateScope())
            {
                var batchStateCheckers = states.SelectMany(x => x.StateCheckers)
                                         .Where(x => x is ISimpleBatchStateChecker <TState>)
                                         .Cast <ISimpleBatchStateChecker <TState> >()
                                         .GroupBy(x => x)
                                         .Select(x => x.Key);

                foreach (var stateChecker in batchStateCheckers)
                {
                    var context = new SimpleBatchStateCheckerContext <TState>(
                        scope.ServiceProvider.GetRequiredService <ICachedServiceProvider>(),
                        states.Where(x => x.StateCheckers.Contains(stateChecker)).ToArray());

                    foreach (var x in await stateChecker.IsEnabledAsync(context))
                    {
                        result[x.Key] = x.Value;
                    }

                    if (result.Values.All(x => !x))
                    {
                        return(result);
                    }
                }

                foreach (ISimpleBatchStateChecker <TState> globalStateChecker in Options.GlobalStateCheckers
                         .Where(x => typeof(ISimpleBatchStateChecker <TState>).IsAssignableFrom(x))
                         .Select(x => ServiceProvider.GetRequiredService(x)))
                {
                    var context = new SimpleBatchStateCheckerContext <TState>(
                        scope.ServiceProvider.GetRequiredService <ICachedServiceProvider>(),
                        states.Where(x => result.Any(y => y.Key.Equals(x) && y.Value)).ToArray());

                    foreach (var x in await globalStateChecker.IsEnabledAsync(context))
                    {
                        result[x.Key] = x.Value;
                    }
                }

                foreach (var state in states)
                {
                    if (result[state])
                    {
                        result[state] = await InternalIsEnabledAsync(state, false);
                    }
                }

                return(result);
            }
        }
Ejemplo n.º 2
0
            public override Task <SimpleStateCheckerResult <SimpleStateCheckerTestBase.MyStateEntity> > IsEnabledAsync(SimpleBatchStateCheckerContext <SimpleStateCheckerTestBase.MyStateEntity> context)
            {
                foreach (var state in context.States)
                {
                    state.MultipleCheckCount += 1;
                }

                var result = new SimpleStateCheckerResult <SimpleStateCheckerTestBase.MyStateEntity>(context.States);

                foreach (var x in result)
                {
                    result[x.Key] = x.Key.CreationTime > DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture);
                }

                return(Task.FromResult(result));
            }
Ejemplo n.º 3
0
            public override Task <SimpleStateCheckerResult <SimpleStateCheckerTestBase.MyStateEntity> > IsEnabledAsync(SimpleBatchStateCheckerContext <SimpleStateCheckerTestBase.MyStateEntity> context)
            {
                foreach (var state in context.States)
                {
                    state.MultipleGlobalCheckCount += 1;
                }

                var result = new SimpleStateCheckerResult <SimpleStateCheckerTestBase.MyStateEntity>(context.States);

                foreach (var x in result)
                {
                    result[x.Key] = x.Key.LastModificationTime.HasValue;
                }

                return(Task.FromResult(result));
            }