Application design and reflection [long]

D

Dave Clarke

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());
}
}
}
 
D

Dave Clarke

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top