internal ExceptionPolicy(IsSatisfied <Exception> satisfiedByException, Func <Exception, Exception> func)
        {
            Guard.NotNull("satisfiedByException", satisfiedByException);
            Guard.NotNull("func", func);

            this.satisfiedByException = satisfiedByException;
            this.func = func;
        }
 public static ExceptionPolicy Catch(this IsSatisfied <Exception> satisfied, Action <Exception> callback)
 {
     return(new ExceptionPolicy(satisfied, exception =>
     {
         callback(exception);
         return null;
     }));
 }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ContractMessage"/> class.
 /// </summary>
 /// <param name="messagePrefix">The message prefix.</param>
 /// <param name="throwException">The throw exception.</param>
 /// <param name="isSatisfied">The is failed.</param>
 /// <param name="getErrorMessage">The get error message.</param>
 public ContractMessage(
     string messagePrefix,
     ThrowException throwException,
     IsSatisfied isSatisfied,
     GetErrorMessage getErrorMessage)
 {
     this.messagePrefix   = messagePrefix;
     this.throwException  = throwException;
     this.isSatisfied     = isSatisfied;
     this.getErrorMessage = getErrorMessage;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ContractMessage"/> class.
        /// </summary>
        /// <param name="messagePrefix">The message prefix.</param>
        /// <param name="throwException">The throw exception.</param>
        /// <param name="isSatisfied">The is failed.</param>
        /// <param name="getErrorMessage">The get error message.</param>
        public ContractMessage(
            string messagePrefix, 
            ThrowException throwException,
            IsSatisfied isSatisfied,
            GetErrorMessage getErrorMessage)
        {
            this.messagePrefix = messagePrefix;
            this.throwException = throwException;
            this.isSatisfied = isSatisfied;
            this.getErrorMessage = getErrorMessage;

        }
        /// <summary>
        ///     Set expires to <paramref name="generateEndDt" /> and reset each try get value by caching key
        /// </summary>
        /// <param name="satisfied">
        ///     See <see cref="IsSatisfied{TInstance}" />
        /// </param>
        /// <param name="generateEndDt"> Date end caching key </param>
        public static CachingPolicy EndSliding(this IsSatisfied <ICacheKey> satisfied, Func <DateTime> generateEndDt)
        {
            Guard.NotNull("generateEndDt", generateEndDt);

            var            endDt = generateEndDt.Invoke();
            IsCacheExpires ret   = key =>
            {
                bool isExpires = DateTime.Now > endDt;
                endDt = generateEndDt.Invoke();                          // Generate next end date time.
                return(isExpires);
            };

            return(new CachingPolicy(satisfied, ret));
        }
Beispiel #6
0
        /// <summary>
        /// Thats the specified verify the constraint.
        /// </summary>
        /// <param name="actual">The actual.</param>
        /// <param name="constraint">The constraint.</param>
        /// <returns></returns>
        public ContractMessage That(object actual, BaseConstraint constraint)
        {
            objectToCheck = actual;

            if (objectToCheck != null)
            {
                messagePrefix = objectToCheck.GetType().Name + " object should";
            }
            else
            {
                messagePrefix = "Object reference should";
            }

            IsSatisfied isSatisfied = delegate { return(constraint.IsSatisfiedBy(actual, appendErrorMessage)); };

            return(new ContractMessage(messagePrefix, throwException, isSatisfied, getErrorMessage));
        }
 public static IsSatisfied <TInstance> Also <TInstance, TFound>(this IsSatisfied <TInstance> source)
 {
     return(instance => source(instance) || instance is TFound);
 }
 public static IsSatisfied <TInstance> Exclude <TInstance, TFound>(this IsSatisfied <TInstance> source)
 {
     return(instance => source(instance) && !(instance is TFound));
 }
Beispiel #9
0
 internal CachingPolicy(IsSatisfied <ICacheKey> satisfied, IsCacheExpires cacheExpires)
 {
     this.satisfied    = satisfied;
     this.cacheExpires = cacheExpires;
 }
 /// <summary>
 ///     Set custom<c>expires</c>s
 /// </summary>
 /// <param name="satisfied"> Filter for caching key </param>
 /// <param name="expires">
 ///     Predicate for <c>expires</c>
 /// </param>
 /// <returns> Instance caching policy </returns>
 public static CachingPolicy SetExpires(this IsSatisfied <ICacheKey> satisfied, IsCacheExpires expires)
 {
     return(new CachingPolicy(satisfied, expires));
 }
 /// <summary>
 ///     Set always expires
 /// </summary>
 /// <param name="satisfied">
 ///     See <see cref="IsSatisfied{TInstance}" />
 /// </param>
 public static CachingPolicy NeverExpires(this IsSatisfied <ICacheKey> satisfied)
 {
     return(new CachingPolicy(satisfied, key => false));
 }
 /// <summary>
 ///     Reset cache each get
 /// </summary>
 public static CachingPolicy AlwaysExpires(this IsSatisfied <ICacheKey> satisfied)
 {
     return(new CachingPolicy(satisfied, key => true));
 }
 public static bool IsSatisfied <TInstance>(this IsSatisfied <TInstance> conditional, TInstance instance)
 {
     return(conditional(instance));
 }
 public static ExceptionPolicy Wrap(this IsSatisfied <Exception> satisfied, Func <Exception, Exception> evaluator)
 {
     return(new ExceptionPolicy(satisfied, evaluator));
 }
 public static ExceptionPolicy ReThrow(this IsSatisfied <Exception> satisfied)
 {
     return(satisfied.Wrap(exception => exception));
 }
 public static ExceptionPolicy Mute(this IsSatisfied <Exception> satisfied)
 {
     return(satisfied.Wrap(exception => null));
 }