5

I need to implement the concept of task in my application. In my project, the task is an operation that must be performed and is identified by a name: each task also has input parameters (types and values) and produces one or more outputs.

Having to implement the Task class, how can I express the concept of "operation that has some kind of result"? I could store the name of the operation in a string attribute, but how do I express the type returned by this operation? Similarly, how can I implement a list of input parameters of the operation, when these parameters may also be of different types?

The goal is to serialize the Task object and send it to another node. The receiver analyzes the received Task object, reads the name of the operation and executes the corresponding function.

For example, suppose that node A and node B "have" the following methods...

Service of node A:

- double getAreaOfRectangle(double length, double width);
- double getAreaOfTriangle(double base, double height);
- double getAreaOfParallelogram(double base, double height).

Service of node B:

- double getAreaOfCircle(double radius);
- double getAreaOfTrapezoid(double base1, double base2, double height).

If the node C needs to calculate the area of ​​a rectangle, it knows it must contact the node A and send a message to node A by specifying the operation (double areaOfRectangle) and the inputs (double width and double height). Similarly, if the node C needs to calculate the area of ​​a circle, it must send a message to node B, specifying the operation and the length of the radius. In order to send the inputs, since they could be different types (or even new user-defined types), I could use an array of object...

Some tasks may depend on other tasks, ie, as input parameters may have at least one output of another task, and so I would need to implement also the link between the output of a task and the input of another.

How should I proceed to define the class?

enzom83
  • 8,080
  • 10
  • 68
  • 114
  • 2
    I believe this is similar to the Command Pattern. – John Saunders Oct 16 '11 at 20:21
  • 1
    For the record, you're describing a remote procedure call. – Chris Eberle Oct 16 '11 at 20:22
  • 2
    This describes a delegate. The delegate declaration describes the input arguments. Its BeginInvoke() method runs the delegate target method on a worker thread. The EndInvoke() method provides the result. Otherwise ably wrapped by the Task class. – Hans Passant Oct 16 '11 at 21:44
  • I had also thought about the Command Pattern, and right now I'm using this pattern. However I was wondering if .NET had some best mechanism to achieve the same goal. Instead, about delegates, I go into how to use them and see if they are suitable for my purpose... I added an example to my question! – enzom83 Oct 17 '11 at 16:40
  • Perhaps this article ([link](http://msdn.microsoft.com/it-it/magazine/cc163920%28en-us%29.aspx)) could be helpful to solve my problem... – enzom83 Oct 17 '11 at 21:38

1 Answers1

0

You should consider using Expression Trees in .NET 4.0. See this article for more info

Serializing and Deserializing Expression Trees in C#

Community
  • 1
  • 1
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73