Chain.NET(a.k.a. NChain)

Overview
News
Release notes
License
Architecture
API documentation
Five minutes tutorial (VB)
Five minutes tutorial (C#)
Examples
Code repository
Download
Forums
Mailing list
Feature request
Bugs reporting
 
SourceForge.net Logo

Five minutes tutorial (C#)

Imagine that you would like to write application which main task would be handling some requests. Chain.NET library delivers you all base classes that brings such functionality.

Below you can find few steps that allows to accomplish such task using Chain.NET library:

1. Each request handling logic can be implemented as particular command. To do it, simly implement ICommand interface as below and place request handling logic in execute method.

Our first command will handle 'noise' request by making some noise:

public class MakeNoise : CommandBase, ICommand
{
	public bool execute(NChain.IContext context)
	{
		/// check if this command should handle current request
		if( ((RequestContext) context).RequestID.Equals( "noise" ) )
		{
			((RequestContext) context).RequestHandlingResult = 
				"Aaaaaaaaaaa !!!! I'm making nooOOOOOoise !!!";
			/// signalize that request is handled
			return PROCESSING_COMPLETED;
		}
		else
		{
			/// signalize that this command do not care about this request
			return CONTINUE_PROCESSING;
		}
	}
}

Our second command will handle 'sandwitch' request by making a sandwitch:

public class MakeSandwitch : CommandBase, ICommand
{
	public bool execute(NChain.IContext context)
	{
		/// check if this command should handle current request
		if( ((RequestContext) context).RequestID.Equals( "sandwitch" ) )
		{
			((RequestContext) context).RequestHandlingResult = 
				"Yes Master. Your sandwitch is ready.";
			/// signalize that request is handled
			return PROCESSING_COMPLETED;
		}
		else
		{
			/// signalize that this command do not care about this request
			return CONTINUE_PROCESSING;
		}
	}
}

2. Gather your 'requests handlers' (commands) in one chain:

public class RequestsHandlersChain : ChainBase
{
	public RequestsHandlersChain()
	{
		this.addCommand( new MakeNoise() );
		this.addCommand( new MakeSandwitch() );
	}
}

3. Extend standard context available via ContextBase class by adding some properties that will give you type-safe access to context's values:

public class RequestContext : ContextBase
{
	public const string REQUEST_ID_KEY = "R_D";
	public const string REQUEST_HANDLING_RESULT_KEY = "R_H_R_K";

	public string RequestID {
		get {
			return (string)this[REQUEST_ID_KEY];
		}
		set {
			this[REQUEST_ID_KEY] = value;
		}
	}

	public string RequestHandlingResult {
		get {
			return (string)this[REQUEST_HANDLING_RESULT_KEY];
		}
		set {
			this[REQUEST_HANDLING_RESULT_KEY] = value;
		}
	}
}

4. To handle request simply set the request identifier in your context and process that context using your 'request handlers' chain. Pull execution result from context after processing finishes.

class Class1
{
	private static RequestsHandlersChain oRequestHandlersChain = 
		new RequestsHandlersChain();
	private static RequestContext oRequestContext =
		new RequestContext();

	[STAThread]
	static void Main(string[] args)
	{
		
		System.Console.WriteLine( "Sending 'sandwitch' request..." );
		sendRequest( "sandwitch" );
		System.Console.WriteLine( "Sending 'noise' request..." );
		sendRequest( "noise" );
		System.Console.WriteLine( "Sending 'something' request..." );
		sendRequest( "something" );
		System.Console.ReadLine();
		
	}

	private static void sendRequest(string sRequestId)
	{
		
		/// set request id in context
		oRequestContext.RequestID = sRequestId;
		/// process the request by passing it to the chain
		bool bResult = oRequestHandlersChain.execute(oRequestContext);
		/// check processing result
		if (bResult.Equals(CommandBase.PROCESSING_COMPLETED))
		{
			/// pull the result from context
			System.Console.WriteLine("Request handling result: " 
				+ oRequestContext.RequestHandlingResult);
		}
		else
		{
			System.Console.WriteLine("This request cannot be handled.");
		}
	}
	
}

Execution output:

Sending 'sandwitch' request...
Request handling result: Yes Master. Your sandwitch is ready.
Sending 'noise' request...
Request handling result: Aaaaaaaaaaa !!!! I'm making nooOOOOOoise !!!
Sending 'something' request...
This request cannot be handled.
 
Copyright 2007-2008 Pawel Stasiak