Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Chain of Responsibility used in the .NET Framework? [closed]

I am trying to learn more about the Chain of Responsibility design pattern. Every single example I see online gives the simplest example i.e. a Logger that writes a different message to the Console depending on what handler handles the request.

Are there any real life examples in the .NET framework? I have looked at the following links:

  1. What design patterns are used throughout the .NET Framework?
  2. http://www.dofactory.com/net/chain-of-responsibility-design-pattern

  3. https://sourcemaking.com/design_patterns/chain_of_responsibility

like image 446
w0051977 Avatar asked Feb 18 '18 11:02

w0051977


People also ask

Where is chain of responsibility pattern used?

Chain of responsibility pattern is used to achieve loose coupling in software design where a request from client is passed to a chain of objects to process them.

Which is true about chain of responsibility pattern?

A Chain of Responsibility Pattern says that just "avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request". For example, an ATM uses the Chain of Responsibility design pattern in money giving process.

What is Chain of Responsibility in MVC pattern?

Chain of Responsibility is behavioral design pattern that allows passing request along the chain of potential handlers until one of them handles request. The pattern allows multiple objects to handle the request without coupling sender class to the concrete classes of the receivers.

What is the difference between Chain of Responsibility and decorator patterns?

The chain-of-responsibility pattern is structurally nearly identical to the decorator pattern, the difference being that for the decorator, all classes handle the request, while for the chain of responsibility, exactly one of the classes in the chain handles the request.


2 Answers

ASP.NET Core uses this design principle for web request handling. Each web request goes through the chain of handlers (called middleware), and each handler might do something with request and decide if request should go to the next handler in a chain or not. Per documentation:

Middleware is software that's assembled into an application pipeline to handle requests and responses. Each component:

  • Chooses whether to pass the request to the next component in the pipeline.
  • Can perform work before and after the next component in the pipeline is invoked.

Which is, I think, as close to responsibility chain as it can get.

Also, even regular .NET event often works as implementation of this principle. Invoking .NET event goes through the list of subscribers (event handlers) and invokes them one after another. Many events (for example, UI events in WPF, such as mouse click) have Handled boolean flag in their event arguments. Event handler might set this flag to true to notify subsequent handlers in a chain to ignore this event (because it has already been "handled"), or leave it false, notifying next handler in a chain that it might still do something with it. This is also implementation of chain of responsibility.

like image 76
Evk Avatar answered Oct 24 '22 09:10

Evk


Taken directly from book .Net Design patterns where it explained how chain of responsibility used in asp.net request execution pipeline:

Chain of responsibility pattern in ASP.net :

The chain of responsibility pattern is a design pattern consisting of a series of processing objects through which we pass a data stream for filtration or modification. Ultimately, the process terminates when the data stream passes the last processing object at the end of the chain. The ASP.NET pipeline is a wonderful example where the chain of responsibility pattern is leveraged to provide an extensible programming model. The ASP.NET infrastructure implements WebForms API, ASMX Web services, WCF, ASP.NET Web API, and ASP.NET MVC using HTTP modules and handlers. Every request in the pipeline passes through a series of modules (a class that implements IHttpModule) before it reaches its target handler (a class that implements IHttpHandler). Once a module in the pipeline has done its duty, it passes the responsibility of the request processing to the next module in the chain. Finally, it reaches the handler. The following code snippet shows how one can write an object that leverages the chain of responsibility pattern to create a module that filters an incoming request. These filters are configured as chains and will pass the request content to the next filter in the chain by the ASP.net runtime:

public class SimpleHttpModule : IHttpModule 
{ 
  public SimpleHttpModule(){} 
  public String ModuleName 
  { 
    get { return "SimpleHttpModule"; } 
  } 
  public void Init(HttpApplication application) 
  { 
    application.BeginRequest +=  
    (new EventHandler(this.Application_BeginRequest)); 
    application.EndRequest +=  
    (new EventHandler(this.Application_EndRequest)); 
  } 
  private void Application_BeginRequest(Object source,  
  EventArgs e) 
  { 
    HttpApplication application = (HttpApplication)source; 
    HttpContext context = application.Context; 
    context.Response.Write(SomeHtmlString); 
  } 
  private void Application_EndRequest(Object source, EventArgs e) 
  { 
    HttpApplication application =      (HttpApplication)source; 
    HttpContext context = application.Context; 
    context.Response.Write(SomeHtmlString); 
  } 
  public void Dispose(){} 
} 

<configuration> 
  <system.web> 
    <httpModules> 
      <add name=" SimpleHttpModule " type=" SimpleHttpModule "/> 
    </httpModules> 
  </system.web> 
</configuration> 

In the ASP.NET pipeline, a request passes through a series of HTTP modules before it hits a handler. A simple HTTP handler routine is given as follows:

public class SimpleHttpHandler: IHttpHandler 
{ 
  public void ProcessRequest(System.Web.HttpContext context){ 
    context.Response.Write("The page request ->" +          
    context.Request.RawUrl.ToString()); 
  } 
  public bool IsReusable 
  { 
    get{ return true; } 
  } 
} 

We can configure the handler as given next. Whenever we create an ASP.NET resource with the .smp extension, the handler will be SimpleHttpHandler:

<system.web> 
      <httpHandlers> 
        <add verb="*" path="*.smp" type="SimpleHttpHandler"/> 
      </httpHandlers> 
    </system.web> 

The preceding technique of leveraging the chain of responsibility pattern is available in other web technologies such as Java Servlets (called Servlet filters) and also available in IIS as ISAPI filters.

like image 32
rahulaga_dev Avatar answered Oct 24 '22 11:10

rahulaga_dev