473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Application design and reflection [long]

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
1 1424
(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: assi | last post by:
Hello all We are developing a large dotnet application, which includes ~ 120 assemblies. (total size of all binaries is ~ 20MB). Our application also references the following dotnet assemblies:...
1
by: danielhardman | last post by:
I am implementing a component that helps a developer with som localization tasks at design-time, and provides some related feature at run-time. My problem is that in order to do the design-time...
2
by: assi | last post by:
Hello all We are developing a large dotnet application, which includes ~ 120 assemblies. (total size of all binaries is ~ 20MB). Our application also references the following dotnet assemblies:...
4
by: Andrus | last post by:
I have WinForms MDI application. MDI child forms are created by menustrip click enent handler AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") ); Every MDI child form "childform1"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.