From ff712780a455fd44f896f8adf3be4da0268d42a8 Mon Sep 17 00:00:00 2001 From: hassa Date: Thu, 14 Mar 2024 16:22:09 +0330 Subject: [PATCH 1/2] rule-engine part firstexample fixed --- .../RuleEngine/Abstraction/RequestHandler.cs | 27 ++++++++++++++++++ .../RuleEngine/Interface/IRequestHandler.cs | 10 +++++++ .../RuleEngine/Model/Request.cs | 16 +++++++++++ .../RuleEngine/Program.cs | 28 +++++++++++++++++-- .../Providers/AuthenticationHandler.cs | 14 ++++++++++ .../Providers/AuthorizationHandler.cs | 14 ++++++++++ .../RuleEngine/Providers/ValidationHandler.cs | 14 ++++++++++ 7 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Interface/IRequestHandler.cs create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Model/Request.cs create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthenticationHandler.cs create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs create mode 100644 Behavioral/ChainOfResponsibility/RuleEngine/Providers/ValidationHandler.cs diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs new file mode 100644 index 0000000..e7efd9e --- /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 virtual 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..2e1e4db --- /dev/null +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.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 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 + } + } +} From fa16adb7bd8f71406d6ca997777701101080cc85 Mon Sep 17 00:00:00 2001 From: hassa Date: Thu, 14 Mar 2024 16:34:50 +0330 Subject: [PATCH 2/2] remove virtual from handleRequest method --- .../RuleEngine/Abstraction/RequestHandler.cs | 2 +- .../RuleEngine/Providers/AuthorizationHandler.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs index e7efd9e..bc5242f 100644 --- a/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Abstraction/RequestHandler.cs @@ -12,7 +12,7 @@ public void SetNextHandler(IRequestHandler handler) _nextHandler = handler; } - public virtual void HandleRequest(Request request) + public void HandleRequest(Request request) { ProcessRequest(request); diff --git a/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs index 2e1e4db..3e71d93 100644 --- a/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs +++ b/Behavioral/ChainOfResponsibility/RuleEngine/Providers/AuthorizationHandler.cs @@ -10,5 +10,6 @@ protected override void ProcessRequest(Request request) Console.WriteLine("Authorization handler processing request"); // Perform authorization logic } + } }