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 (VB)

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
	Inherits CommandBase
	Implements ICommand

	Public Function execute(ByVal context As NChain.IContext) As Boolean _
	 Implements NChain.ICommand.execute

		''' check if this command should handle current request
		If DirectCast(context, RequestContext).RequestID.Equals("noise") Then

			DirectCast(context, RequestContext).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

		End If

	End Function

End Class

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

Public Class MakeSandwitch
	Inherits CommandBase
	Implements ICommand

	Public Function execute(ByVal context As NChain.IContext) As Boolean _
	 Implements NChain.ICommand.execute

		''' check if this command should handle current request
		If DirectCast(context, RequestContext).RequestID.Equals("sandwitch") Then

			DirectCast(context, RequestContext).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

		End If

	End Function

End Class


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

Public Class RequestsHandlersChain
	Inherits ChainBase

	Sub New()
		Me.addCommand(New MakeNoise)
		Me.addCommand(New MakeSandwitch)
	End Sub

End Class

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
	Inherits ContextBase

	Public Const REQUEST_ID_KEY = "R_D"
	Public Const REQUEST_HANDLING_RESULT_KEY = "R_H_R_K"

	Public Property RequestID() As String
		Get
			Return CStr(Me(REQUEST_ID_KEY))
		End Get
		Set(ByVal Value As String)
			Me(REQUEST_ID_KEY) = Value
		End Set
	End Property

	Public Property RequestHandlingResult() As String
		Get
			Return CStr(Me(REQUEST_HANDLING_RESULT_KEY))
		End Get
		Set(ByVal Value As String)
			Me(REQUEST_HANDLING_RESULT_KEY) = Value
		End Set
	End Property

End Class

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.

Module Module1

	Dim oRequestHandlersChain As RequestsHandlersChain = _
	  New RequestsHandlersChain

	Dim oRequestContext As RequestContext = _
	 New RequestContext

	Sub Main()

		Dim bRequestResult As Boolean

		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()

	End Sub

	Private Sub SendRequest(ByVal sRequestId As String)

		''' set request id in context
		oRequestContext.RequestID = sRequestId

		''' process the request by passing it to the chain
		Dim bResult As Boolean = _
		 oRequestHandlersChain.execute(oRequestContext)

		''' check processing result
		If bResult.Equals(CommandBase.PROCESSING_COMPLETED) Then
			''' pull the result from context
			System.Console.WriteLine("Request handling result: " & _
			 oRequestContext.RequestHandlingResult)
		Else
			System.Console.WriteLine("This request cannot be handled.")
		End If

	End Sub

End Module

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