Code Monkey home page Code Monkey logo

chain.of.responsibility.example's People

Contributors

dannyrusnok avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chain.of.responsibility.example's Issues

Exception handling in chain of res

When I convert methods to async/await, if an error occurs, instead of throw an error, it just exits the handler and continues processing the rest of the methods in that service for example this is the handler

using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public abstract class Handler<T>
    {
        private IHandler<T> Next { get; set; }

        public virtual **async Task** Handle(T request)
        {
            Next?.Handle(request);
           
        }

        public IHandler<T> SetNext(IHandler<T> next)
        {
            Next = next;
            return Next;
        }
    }
}


and this is UsernameRequiredValidationHandler


using System;
using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class UsernameRequiredValidationHandler : Handler<User>, IHandler<User>
    {
        public override **async Task** Handle(User user)
        {
            if (string.IsNullOrWhiteSpace(user.Username))
            {
                throw new Exception("Username is required.");
            }
            base.Handle(user);
        }
    }
}

and this is PasswordRequiredValidationHandler

using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class PasswordRequiredValidationHandler : Handler<User>, IHandler<User>
    {
        public override **async Task** Handle(User user)
        {
          
            if (string.IsNullOrWhiteSpace(user.Password))
            {
                throw new Exception("Password is required.");
            }

             base.Handle(user);
        }
    }
}

and this is OnlyAdultValidationHandler


using System;
using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class OnlyAdultValidationHandler : Handler<User>, IHandler<User>
    {
        public override async Task Handle(User user)
        {
            throw new Exception();
            if (user.BirthDate.Year > DateTime.Now.Year - 18)
            {
                throw new Exception("Age under 18 is not allowed.");
            }

            base.Handle(user);
        }
    }
}

and when exception throw in OnlyAdultValidationHandler the exception not catch in try catch block in the Register class

public class ChainPatternRegistrationProcessor
    {
        public async Task Register(User user)
        {
            try
            {
                var handler = new UsernameRequiredValidationHandler();
                handler.SetNext(new PasswordRequiredValidationHandler())
                    .SetNext(new OnlyAdultValidationHandler());

                await handler.Handle(user);
                Console.WriteLine("");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
can we catch this error in Register class?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.