By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Application design and reflection [long]

P: n/a
I'm looking for some feedback about an application I'm writing.

In a nutshell a message is received, parsed, a mainframe transaction
generated, and a response returned.

I am intending to use a combination of Factory and Command patterns
which seems to be a reasonable approach. The Command is able to
determine if it can process the input *before* it is instantiated, ie
it uses a static method to return a boolean value. I've appended an
example that encapsulates what I'm intending to do. The Main()
function represents the Command Factory. New Commands can be added
easily without modifying core code.

Is this a reasonable approach or am I missing something obvious that
avoids some of this complexity. Is this an acceptable use of
Reflection?

Note the static canProcess() method is implemented this way because
abstract methods cannot also be static.

Thanks
Dave

using System;
using System.Text;
using System.Reflection;

namespace Reflect
{
abstract class Command
{
// this method will be invoked if derived class fails to provide a
"new" version
// because of "BindingFlags.FlattenHierarchy", always returns false
public static bool canProcess( string Message)
{
Console.WriteLine("Command.canProcess()");
return false;
}

protected string message;
protected Command( string Message) { message = Message; } // ctor
public abstract string Process();
}

class BalanceCommand: Command
{
// "new" keyword required to override base method
public new static bool canProcess( string Message)
{
Console.WriteLine("BalanceCommand.canProcess()");
if (Message == "BAL")
return true;
else
return false;
}

public BalanceCommand( string Message) : base( Message) { }
public override string Process() { return "BalanceCommand"; }
}

// UnknownCommand is a "Special Case" (Patterns of Enterprise
Application Architecture, p496)
class UnknownCommand: Command
{
// catch all method always returns true
public new static bool canProcess( string Message)
{
Console.WriteLine("UnknownCommand.canProcess()");
return true;
}

public UnknownCommand( string Message) : base( Message) { }
public override string Process() { return "UnknownCommand"; }
}

class BadCommand: Command
{
// BadCommand has no implementation for canProcess, base method will
be invoked
// because of "BindingFlags.FlattenHierarchy" in GetMethod call
public BadCommand( string Message) : base( Message) { }
public override string Process() { return "BadCommand (should never
be called)"; }
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
// UnknownCommand should always be last value in array
string [] commands = {
"BalanceCommand",
"BadCommand",
"UnknownCommand"
};
Type [] paramTypes = { typeof( string) };
Object [] msg = {"BAM"}; // message text to be processed
Type t = null;
foreach (string s in commands)
{
t = Type.GetType( "Reflect." + s);
MethodInfo cmdInfo = t.GetMethod("canProcess",
BindingFlags.Static | BindingFlags.Public |
BindingFlags.FlattenHierarchy,
null,
paramTypes,
null);
if (Convert.ToBoolean( cmdInfo.Invoke( t, msg))) break; // invoke
canProcess()
}

// t is a class that can process command - create instance
Command cmd = t.InvokeMember(null, BindingFlags.CreateInstance,
null, null, msg) as Command;
Console.WriteLine( cmd.Process());
}
}
}
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
(hopefully addressed spacing problem in example code)

I'm looking for some feedback about an application I'm writing.

In a nutshell a message is received, parsed, a mainframe transaction
generated, and a response returned.

I am intending to use a combination of Factory and Command patterns
which seems to be a reasonable approach. The Command is able to
determine if it can process the input *before* it is instantiated, ie
it uses a static method to return a boolean value. I've appended an
example that encapsulates what I'm intending to do. The Main()
function represents the Command Factory. New Commands can be added
easily without modifying core code.

Is this a reasonable approach or am I missing something obvious that
avoids some of this complexity. Is this an acceptable use of
Reflection?

Note the static canProcess() method is implemented this way because
abstract methods cannot also be static.

Thanks
Dave

using System;
using System.Text;
using System.Reflection;

namespace Reflect
{
abstract class Command
{
// this method will be invoked if derived class fails to provide
a
// "new" version because of "BindingFlags.FlattenHierarchy",
always
// returns false
public static bool canProcess( string Message)
{
Console.WriteLine("Command.canProcess()");
return false;
}

protected string message;
protected Command( string Message) { message = Message; } //
ctor
public abstract string Process();
}

class BalanceCommand: Command
{
// "new" keyword required to override base method
public new static bool canProcess( string Message)
{
Console.WriteLine("BalanceCommand.canProcess()");
if (Message == "BAL")
return true;
else
return false;
}

public BalanceCommand( string Message) : base( Message) { }
public override string Process() { return "BalanceCommand"; }
}

// UnknownCommand is a "Special Case" (Patterns of Enterprise
// Application Architecture, p496)
class UnknownCommand: Command
{
// catch all method always returns true
public new static bool canProcess( string Message)
{
Console.WriteLine("UnknownCommand.canProcess()");
return true;
}

public UnknownCommand( string Message) : base( Message) { }
public override string Process() { return "UnknownCommand"; }
}

class BadCommand: Command
{
// BadCommand has no implementation for canProcess, base method
// will be invoked because of "BindingFlags.FlattenHierarchy"
// in GetMethod call
public BadCommand( string Message) : base( Message) { }
public override string Process() { return "BadCommand (should" +
" never be called)"; }
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
// UnknownCommand should always be last value in array
string [] commands = {
"BalanceCommand",
"BadCommand",
"UnknownCommand"
};
Type [] paramTypes = { typeof( string) };
Object [] msg = {"BAM"}; // message text to be processed
Type t = null;
foreach (string s in commands)
{
t = Type.GetType( "Reflect." + s);
MethodInfo cmdInfo = t.GetMethod("canProcess",
BindingFlags.Static | BindingFlags.Public |
BindingFlags.FlattenHierarchy,
null,
paramTypes,
null);
if (Convert.ToBoolean( cmdInfo.Invoke( t, msg)))
break; // invoke canProcess()
}

// t is a class that can process command - create instance
Command cmd = t.InvokeMember(null,
BindingFlags.CreateInstance,
null, null, msg) as Command;
Console.WriteLine( cmd.Process());
}
}
}
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.