/// <summary>
 /// Determines the result of the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns>
 /// A Decision indicating the result of the query.
 /// </returns>
 public async Task<bool> CheckAsync(DecisionContext context)
 {
     return await Task.Run(() =>
         {
             var expression = providers[context.Namespace].Inflate(context);
             return expression(context);
         });
 }
        /// <summary>
        /// Gets the environment with the specified alias asynchronously.
        /// </summary>
        /// <param name="alias">The globally unique alias used to represent a specific environment.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// An environment, likely an instance of a class from an external assembly.
        /// </returns>
        public async Task<dynamic> GetAsync(string alias, DecisionContext context)
        {
            var result = cache.Get(Key(alias, context));

            if (result == null)
            {
                result = await service.GetAsync(alias, context);
                cache.Set(Key(alias, context), result, TimeSpan.FromSeconds(cacheDuration));
            }

            return result;
        }
        /// <summary>
        /// Determines the result of the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A Decision indicating the result of the query.
        /// </returns>
        public async Task<bool> CheckAsync(DecisionContext context)
        {
            var result = cache.Get(Key(context));

            if (result == null)
            {
                result = new Decision { Id = context.Id, Result = await service.CheckAsync(context) };
                cache.Set(Key(context), result, TimeSpan.FromSeconds(cacheDuration));
            }

            return result.Result;
        }
        public async Task<dynamic> GetAsync(string alias, DecisionContext context)
        {
            if (alias == LongAclEnvironment.ALIAS || alias == SimpleCounterEnvironment.ALIAS)
            {
                Thread.Sleep(TimeSpan.FromSeconds(3));
            }

            if (alias == SimpleCounterEnvironment.ALIAS)
            {
                return new SimpleCounterEnvironment(true);
            }

            return await Task.FromResult(environments[alias]);
        }
 public override bool Decide(DecisionContext context)
 {
     var envTask = GetEnvironmentAsync<AclEnvironment>(AclEnvironment.ALIAS, context);
     envTask.Wait();
     return envTask.Result.Entries.Any(a => a.Allow);
 }
 /// <summary>
 /// Gets the environment with the specified key asynchronously.
 /// </summary>
 /// <param name="alias">The key.</param>
 /// <param name="context">The context.</param>
 /// <returns>
 /// An environment, likely an instance of a class from an external assembly.
 /// </returns>
 public async Task<dynamic> GetAsync(string alias, DecisionContext context)
 {
     IEnvironmentProvider provider;
     if (environments.TryGetValue(alias, out provider)) return await provider.GetAsync(alias, context);
     throw new NotSupportedException(alias + " is not part of the supported Environments");
 }
        /// <summary>
        /// Inflates the specified context into an expression that can be executed.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>A delegate that takes a <see cref="DecisionContext"/> as a parameter to execute</returns>
        public Predicate<DecisionContext> Inflate(DecisionContext context)
        {
            if (compiled.ContainsKey(context.Role) == false)
            {
                var expression = Expression.Lambda<Predicate<DecisionContext>>(Parse(expressions[context.Role]), PARAMETER);
                var compiledExpression = expression.Compile();
                compiled.AddOrUpdate(context.Role, compiledExpression, (key, oldValue) => compiledExpression);
            }

            return compiled[context.Role];
        }
 public override Task<dynamic> GetAsync(string alias, DecisionContext context)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Make a decisions on the Decision of this policy for the provided <see cref="DecisionContext" />.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public override bool Decide(DecisionContext context)
 {
     return false;
 }
 /// <summary>
 /// Creates a unique cache key for the alias and context
 /// </summary>
 /// <param name="alias">The alias.</param>
 /// <param name="context">The context.</param>
 /// <returns>A unique string representing the parameters</returns>
 private static string Key(string alias, DecisionContext context)
 {
     return string.Format(CACHE_KEY_FORMAT, alias, context.Id);
 }
 public override bool Decide(DecisionContext context)
 {
     var envTask = GetEnvironmentAsync<CurrentUserEnvironment>(CurrentUserEnvironment.ALIAS, context);
     envTask.Wait();
     return envTask.Result.UserId == MatchUserId;
 }
 /// <summary>
 /// Creates a unique cache key for the context
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns>A unique string representing the parameters</returns>
 private static string Key(DecisionContext context)
 {
     return context.Id;
 }
 /// <summary>
 /// Make a decisions on the Decision of this policy for the provided <see cref="DecisionContext" />.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public override bool Decide(DecisionContext context)
 {
     return context.Target.id == Id;
 }
 /// <summary>
 /// Determines the result of the specified context.
 /// </summary>
 /// <param name="service">The service.</param>
 /// <param name="context">The context.</param>
 /// <returns>
 /// A Decision indicating the result of the query.
 /// </returns>
 public static bool Check(this IDecisionService service, DecisionContext context)
 {
     return Task.Run(() => service.CheckAsync(context)).Result;
 }