diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs new file mode 100644 index 0000000..bc5242f --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs @@ -0,0 +1,27 @@ +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Interface; +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Abstraction +{ + public abstract class RequestHandler : IRequestHandler + { + private IRequestHandler _nextHandler; + + public void SetNextHandler(IRequestHandler handler) + { + _nextHandler = handler; + } + + public void HandleRequest(Request request) + { + ProcessRequest(request); + + if (_nextHandler != null) + { + _nextHandler.HandleRequest(request); + } + } + + protected abstract void ProcessRequest(Request request); + } +} diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Interface/IRequestHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Interface/IRequestHandler.cs new file mode 100644 index 0000000..0209fdb --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Interface/IRequestHandler.cs @@ -0,0 +1,10 @@ +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Interface +{ + public interface IRequestHandler + { + void SetNextHandler(IRequestHandler handler); + void HandleRequest(Request request); + } +} diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Model/Request.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Model/Request.cs new file mode 100644 index 0000000..04ebe16 --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Model/Request.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model +{ + public class Request + { + public string Username { get; set; } + public string Password { get; set; } + + // Other properties + } +} diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Program.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Program.cs index 3751555..df085ee 100644 --- a/Behavioral/ChainOfResponsibility/RuleEngine/Program.cs +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Program.cs @@ -1,2 +1,26 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Providers; + + +// Create the request handlers +var validationHandler = new ValidationHandler(); +var authenticationHandler = new AuthenticationHandler(); +var authorizationHandler = new AuthorizationHandler(); + + + +// Set the chain of responsibility +validationHandler.SetNextHandler(authenticationHandler); +authenticationHandler.SetNextHandler(authorizationHandler); + + +// Create a request +var request = new Request { Username = "hwpro" , Password = "123"}; + + +// Process the request +validationHandler.HandleRequest(request); + + +Console.ReadLine(); diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthenticationHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthenticationHandler.cs new file mode 100644 index 0000000..97bf080 --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthenticationHandler.cs @@ -0,0 +1,14 @@ +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Abstraction; +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Providers +{ + public class AuthenticationHandler : RequestHandler + { + protected override void ProcessRequest(Request request) + { + Console.WriteLine("Authentication handler processing request"); + // Perform authentication logic + } + } +} diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs new file mode 100644 index 0000000..3e71d93 --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs @@ -0,0 +1,15 @@ +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Abstraction; +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Providers +{ + public class AuthorizationHandler : RequestHandler + { + protected override void ProcessRequest(Request request) + { + Console.WriteLine("Authorization handler processing request"); + // Perform authorization logic + } + + } +} diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Providers/ValidationHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/ValidationHandler.cs new file mode 100644 index 0000000..91e2315 --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/ValidationHandler.cs @@ -0,0 +1,14 @@ +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Abstraction; +using Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Model; + +namespace Thisisnabi.DesignPattern.Behavioral.ChainOfResponsibility.RuleEngine.Providers +{ + public class ValidationHandler : RequestHandler + { + protected override void ProcessRequest(Request request) + { + Console.WriteLine("Validation handler processing request"); + // Perform validation logic + } + } +}