项目作者: tgvishnu

项目描述 :
exceptions exception chain
高级语言: C#
项目地址: git://github.com/tgvishnu/Vishnu.Clauses.git
创建时间: 2019-04-22T03:13:25Z
项目社区:https://github.com/tgvishnu/Vishnu.Clauses

开源协议:

下载


Vishnu.Clause

This package contains

  • ShieldClause (NuGet: Vishnu.ShieldClause)

    A simple package with extensible shield clauses

  • HandleClause (NuGet : Vishnu.HandleClause)

    A package for handling exceptions based on Attributes, Inline and Exception Chaining

Give a Star! :star:

If you like or are using this project please give it a star. Thanks!

1. Vishnu.ShieldClause [ NuGet: Vishnu.ShieldClause ]

A simple package with extensible shield clauses.

Usage

  1. public void ValidateContent(User user, string input)
  2. {
  3. Shield.Against.Null(User, nameof(user));
  4. Shield.Throws.ArugmentNullException<string>((input) => { return input == null ? true : false; }, null, "param1"));
  5. Shield.Throws.ArugmentNullException(Validate(""), null, "param1"));
  6. Shield.Throws.ArgumentException((true || true), "param1"));
  7. }
  8. public bool Validate(string input)
  9. {
  10. if(string.IsNullOrEmpty(input))
  11. {
  12. return true;
  13. }
  14. }

Supported classes

  1. - Shield.Against.NullOrEmpty
  2. - Shield.Against.NullOrWhiteSpace
  3. - Shield.Against.OutOfRange
  4. - Shield.Against.LesserThan<T>
  5. - Shield.Against.GreaterThan<T>
  6. - Shield.Against.IsTypeOf<T>
  7. - Shield.Against.NullOrEmpty<T>
  8. - Shield.Throws.ArgumentException
  9. - Shield.Throws.ArgumentNullException
  10. - Shield.Throws.TimeoutException
  11. - Shield.Throws.NotSupportedException

Extending with your own Guard Clauses

  1. // use extension methods on IShieldClause for adding more functionality in "Against"
  2. namespace Vishnu.ShieldClause
  3. {
  4. public static class ShieldClauseExtension
  5. {
  6. public static void Tst(this IShieldClause shieldClause, int input, string parameterName)
  7. {
  8. }
  9. }
  10. }
  11. // use extension methods on ExceptionContainer for adding more functionality in "Throw"
  12. namespace Vishnu.ShieldClause
  13. {
  14. public static class ShieldClauseExtension
  15. {
  16. public static void FileNotFoundException(this ExceptionContainer container, bool when, int input, string parameterName)
  17. {
  18. }
  19. }
  20. }

2. Vishnu.HandleClause [ NuGet: Vishnu.HandleClause ]

A package for handling exception and invokes specific action based on the exception

  • Handle Attribute
  • Inline
  • Exception chaining

a. Handle Attribute

Description

  1. Attribute Handle, supresses or handles the exceptions raised by any method. Optionally allows to invoke other action if any exception is handled or supressed. Handle attribute allows inheritance.

Usage

  1. Import namespace
  1. using Vishnu.HandleClause;
  1. Decorate the method with Handle attribute and specify the type of exceptions to be handled.
  1. public class Foo
  2. {
  3. // Handles supress only one exception
  4. [Handle(typeof(ArgumentNullException))]
  5. public void Update(string value)
  6. {
  7. var result = Check(value);
  8. }
  9. // Handle multiple exceptions. Any custom exceptions can also be specifieed
  10. [Handle(new Type[] { typeof(ArgumentNullException), typeof(ArgumentException), typeof(CustomException) })]
  11. public int Sycnchorize()
  12. {
  13. return 0;
  14. }
  15. }
  1. Pass the method in Handle.Method
  1. public void Test()
  2. {
  3. Foo foo = new Foo();
  4. // call the specific method to be invoked.
  5. Handle.Method<string>(foo.Update, "value");
  6. Handle.Method(foo.Sycnchorize);
  7. // pass the action to be excecuted if the exception is handled.
  8. Handle.Method(foo.Sycnchorize, ExceptionHandledCallback);
  9. }
  10. // Action invoked if the exception is handled.
  11. private void ExceptionHandledCallback(Exception ex)
  12. {
  13. //
  14. }

Supported Actions

  1. - Handle.Method(Action action, Action<Exception> exceptionHandledAction = null)
  2. - Handle.Method<TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHandledAction = null)
  3. - TResult result = Handle.Method<TResult>(Func<TResult> action, Action<Exception> exceptionHandledAction = null)
  4. - TResult result = Handle.Method<TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHandledAction = null)
  5. - TResult result = Handle.Method<TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHandledAction = null)
  6. - TResult result = Handle.Method<TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHandledAction = null)

b. Inline

Description

Inline exception handling allows to handle or supress only one exception. Optionally, it allows to invoke an action if the exception is handled.

Usage

  1. Declare namespace
  1. using Vishnu.HandleClause;
  1. Use Handle.Inline static method and pass the action to be invoked as parameter. Optionally exception handled call back can also be passed.
  1. public class Boo
  2. {
  3. public void Update()
  4. {
  5. .....
  6. }
  7. public int Update(string value)
  8. {
  9. ....
  10. return 0;
  11. }
  12. }
  13. public void Test()
  14. {
  15. Boo boo = new Boo();
  16. // handles ArgumentException
  17. Handle.Inline<ArgumentException>(boo.Update);
  18. Handle.Inline<ArgumentException, string, int>(boo.Update, "hello");
  19. // Handles ArgumentException and also invokes ExceptionHandledCallback if exception is handled.
  20. Handle.Inline<ArgumentException, string, int>(boo.Update, "hello", this.ExceptionHandledCallback);
  21. }

Supported Actions

  1. - Handle.Inline<TException>(Action action, Action<Exception> exceptionHanldedAction = null) where TException : Exception
  2. - Handle.Inline<TException, TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHanldedAction = null) where TException : Exception
  3. - TResult result = Handle.Inline<TException, TResult>(Func<TResult> action, Action<Exception> exceptionHanldedAction = null) where TException : Exception
  4. - TResult result = Handle.Inline<TException, TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHanldedAction = null) where TException : Exception
  5. - TResult result = Handle.Inline<TException, TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHanldedAction = null) where TException : Exception
  6. - TResult result = Handle.Inline<TException, TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHanldedAction = null) where TException : Exception

c. Exception Chaining

Description

Exception chaining allows to handle multiple exception. Optionally, it allows to invoke an action if the exception is handled.

Usage

  1. Declare namespace
  1. using Vishnu.HandleClause;
  1. Use ‘Case’ to handle specific exception and we can chain multiple exception by using ‘Or’. Finally call Execute to invoke the specific action. Optionally exception handled call back can also be passed.
  1. public class Boo
  2. {
  3. public void Update()
  4. {
  5. .....
  6. }
  7. public int Update(string value)
  8. {
  9. ....
  10. return 0;
  11. }
  12. }
  13. public void Test()
  14. {
  15. int result = Handle
  16. .Case<ArgumentNullException>() // exception
  17. .Or<CustomException>() // chain exception using Or
  18. .Or<ArgumentException>()
  19. .UseSync() // only synch methods allowed
  20. .Execute<string int>(new Boo().Update, "test"); // call the method
  21. // Handles ArgumentException and also invokes ExceptionHandledCallback if exception is handled.
  22. int result = Handle
  23. .Case<ArgumentNullException>() // exception
  24. .UseSync() // only synch methods allowed
  25. .Execute(new Boo().Update, ExceptionHandledCallback); // call the method
  26. }

Supported Actions

Syntax :
[] - Optional

  • Handle
    1. .Case<Exception>
    2. [.Or<Exception>
    3. .Or<Exception>
    4. ....]
    5. .UseSync()
    6. .Execute[<Parameters, ReturnValue>](action, [callback])

Overloaded execute methods

  1. - Execute<TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHanldedAction = null)
  2. - Execute(Action action, Action<Exception> exceptionHanldedAction = null)
  3. - var result = Execute<TResult>(Func<TResult> action, Action<Exception> exceptionHanldedAction = null)
  4. - var result = Execute<TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHanldedAction = null)
  5. - var result = Execute<TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHanldedAction = null)
  6. - var result = Execute<TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHanldedAction = null)

References