InfoQ

News

.NET Chain of Responsibility Library

Posted by Al Tenhundfeld on Sep 18, 2008 02:15 AM

Community
.NET
Topics
.NET Framework ,
Object Oriented Design ,
Loose Coupling
Tags
Mono ,
Design Patterns
Chain.NET (or NChain) is a generic implementation of Chain Of Responsibility design pattern for .NET and Mono platforms. Version 0.1, available on SourceForge, combines standard CoR design pattern with Command design pattern in order to "bring convenience and flexibility in command processing solutions."

NChain is loosely based on the Jakarta's Commons Chain package that is available for Java platform. In general, CoR is a pattern used to promote loose coupling, through separating command objects from a series of processing objects. Each processing object contains code to describe the types of command objects it can accept and will also delegate responsibility for processing incompatible objects to the next processor in the chain.

An example of a simple CoR pattern:

using System;
using System.Collections;
 
namespace Chain_of_responsibility
{
        public interface IChain
        {
                bool Process(object command);
        }
 
        public class Chain
        {
                private ArrayList _list;
 
                public ArrayList List
                {
                        get
                        {
                                return _list;
                        }
                }
 
                public Chain()
                {
                        _list = new ArrayList();
                }
 
                public void Message(object command)
                {
                        foreach ( IChain item in _list )
                        {
                                bool result = item.Process(command);
 
                                if ( result == true ) break;
                        }
                }
 
                public void Add(IChain handler)
                {
                        List.Add(handler);
                }
        }
 
        public class StringHandler : IChain
        {
                public bool Process(object command)
                {
                        if ( command is string )
                        {
                                Console.WriteLine("StringHandler can handle this message

: {0}",(string)command);
 
                                return true;
                        }
 
                        return false;
                }
        }
 
        public class IntegerHandler : IChain
        {
                public bool Process(object command)
                {
                        if ( command is int )
                        {
                                Console.WriteLine("IntegerHandler can handle this message

: {0}",(int)command);
 
                                return true;
                        }
 
                        return false;
                }
        }
 

 
        class TestMain
        {
                static void Main(string[] args)
                {
                        Chain chain = new Chain();
 
                        chain.Add(new StringHandler());                      
                        chain.Add(new IntegerHandler());
 
                        chain.Message("1st string value");
                        chain.Message(100);
                }
        }
}

NChain provides a similar though more robust architecture:
NChain Diagram

NChain requires further testing and performance monitoring to determine how appropriate it is for enterprise application architecture, but the project is open-source and has useful tutorials for getting started quickly. At this early stage, NChain appears to be a viable candidate for any scenario where you are considering using a command pattern but need to provide varying execution contexts for different types of commands.

No comments

Watch Thread Reply

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.