473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on;

namespace Reflect
{
abstract class Command
{
// this method will be invoked if derived class fails to provide a
"new" version
// because of "BindingFlags.F lattenHierarchy ", always returns false
public static bool canProcess( string Message)
{
Console.WriteLi ne("Command.can Process()");
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.WriteLi ne("BalanceComm and.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.WriteLi ne("UnknownComm and.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.F lattenHierarchy " 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("ca nProcess",
BindingFlags.St atic | BindingFlags.Pu blic |
BindingFlags.Fl attenHierarchy,
null,
paramTypes,
null);
if (Convert.ToBool ean( cmdInfo.Invoke( t, msg))) break; // invoke
canProcess()
}

// t is a class that can process command - create instance
Command cmd = t.InvokeMember( null, BindingFlags.Cr eateInstance,
null, null, msg) as Command;
Console.WriteLi ne( cmd.Process());
}
}
}
Nov 15 '05 #1
1 1429
(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.Reflecti on;

namespace Reflect
{
abstract class Command
{
// this method will be invoked if derived class fails to provide
a
// "new" version because of "BindingFlags.F lattenHierarchy ",
always
// returns false
public static bool canProcess( string Message)
{
Console.WriteLi ne("Command.can Process()");
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.WriteLi ne("BalanceComm and.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.WriteLi ne("UnknownComm and.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.F lattenHierarchy "
// 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("ca nProcess",
BindingFlags.St atic | BindingFlags.Pu blic |
BindingFlags.Fl attenHierarchy,
null,
paramTypes,
null);
if (Convert.ToBool ean( cmdInfo.Invoke( t, msg)))
break; // invoke canProcess()
}

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2942
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: System,System.XML, System.Windows.Forms, System.Drawing, System.Data, System.Design. We use dotnet framework 1.1 During initialization, the...
1
2605
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 stuff, I need reference to EnvDTE or EnvDTE80 (I'm using Whidbey beta 2). However, can't ship these .dlls and I don't need them at run-time anyway....
2
474
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: System,System.XML, System.Windows.Forms, System.Drawing, System.Data, System.Design. We use dotnet framework 1.1 During initialization, the...
4
2009
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" may have separate class with different name and different number of parameters. When application in opened, last MDI child form must be opened...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.