Simple Task Classes


S

Shapper

Hello,

I need to create a few Tasks which perform simple actions. For example:

public interface IAddTask {
int Run(int x, int y);
}

public Class AddTask : IAddTask {
public int Run(int x, int y) {
return x + y;
}
}

Each task has a Run method but the arguments can be different.

I would like all tasks to be seen as something "similar".

I mean, I would inject Tasks in other classes.

I am not sure if making all tasks implement an empty ITask is a good idea.

I am also not sure if this is the best way to create small snips of code ...

My example was simplified ... Usually tasks code are a little bit longer.

Any suggestion?
 
Ad

Advertisements

A

Arne Vajhøj

I need to create a few Tasks which perform simple actions. For example:

public interface IAddTask {
int Run(int x, int y);
}

public Class AddTask : IAddTask {
public int Run(int x, int y) {
return x + y;
}
}

Each task has a Run method but the arguments can be different.

I would like all tasks to be seen as something "similar".

I mean, I would inject Tasks in other classes.

I am not sure if making all tasks implement an empty ITask is a good idea.

I am also not sure if this is the best way to create small snips of code ...

My example was simplified ... Usually tasks code are a little bit longer.

Either:

public interface ITask
{
void Run();
}

public Class AddTask : ITask
{
private int X { get; set; }
private int Y { get; set; }
private int Result { get; set; }
public void Run()
{
Result = x + y;
}
}

or:

public interface ITask
{
object Run(object[] args);
}

public Class AddTask : ITask
{
public object Run(object[] args)
{
return (int)x[0] + (int)y[1];
}
}

or the last solution with a List<> or
Dictionary<> instead of object[].

The first is better. Much cleaner.

Arne
 
S

Shapper

I need to create a few Tasks which perform simple actions. For example:

public interface IAddTask {
int Run(int x, int y);
}

public Class AddTask : IAddTask {
public int Run(int x, int y) {
return x + y;
}
}

Each task has a Run method but the arguments can be different.

I would like all tasks to be seen as something "similar".

I mean, I would inject Tasks in other classes.

I am not sure if making all tasks implement an empty ITask is a good idea.

I am also not sure if this is the best way to create small snips of code ...

My example was simplified ... Usually tasks code are a little bit longer.

Either:

public interface ITask
{
void Run();
}

public Class AddTask : ITask
{
private int X { get; set; }
private int Y { get; set; }
private int Result { get; set; }
public void Run()
{
Result = x + y;
}
}

or:

public interface ITask
{
object Run(object[] args);
}

public Class AddTask : ITask
{
public object Run(object[] args)
{
return (int)x[0] + (int)y[1];
}
}

or the last solution with a List<> or
Dictionary<> instead of object[].

The first is better. Much cleaner.

Arne

Hello Arne,

Why do you set the properties as Private?

And if using IoC and having the need for mocking and testing shouldn't I have something like this:

public interface ITask {
void Run();
}

public interface IAddTask : ITask {
int X { get; set; }
int Y { get; set; }
int Result { get; set; }
}

public interface IPowerTask : ITask {
double X { get; set; }
double N { get; set; }
double Result { get; set; }
}

public class AddTask : IAddTask {
public int X { get; set; }
public int Y { get; set; }
public int Result { get; set; }
public void Run() {
Result = X + Y;
}
}

public class PowerTask : IPowerTask {
public double X { get; set; }
public double N { get; set; }
public double Result { get; set; }
public void Run() {
Result = X ^ N;
}
}

public interface ITaskFactory {
ITask Get<T>() where T : ITask;
}

public class TaskFactory : ITaskFactory {
public ITask Get<T>() where T : ITask {
return ObjectFactory.GetInstance<T>();
} // Get
}

public void TestMethod1() {

IAddTask task = _factory.Get<IAddTask>();

task.X = 2;
task.Y = 4;
task.Run();

Assert.AreEqual(task.Result, 6);

}

Thank You,
Miguel
 
Ad

Advertisements

A

Arne Vajhøj

I need to create a few Tasks which perform simple actions. For example:

public interface IAddTask {
int Run(int x, int y);
}

public Class AddTask : IAddTask {
public int Run(int x, int y) {
return x + y;
}
}

Each task has a Run method but the arguments can be different.

I would like all tasks to be seen as something "similar".

I mean, I would inject Tasks in other classes.

I am not sure if making all tasks implement an empty ITask is a good idea.

I am also not sure if this is the best way to create small snips of code ...

My example was simplified ... Usually tasks code are a little bit longer.

Either:

public interface ITask
{
void Run();
}

public Class AddTask : ITask
{
private int X { get; set; }
private int Y { get; set; }
private int Result { get; set; }
public void Run()
{
Result = x + y;
}
}

or:

public interface ITask
{
object Run(object[] args);
}

public Class AddTask : ITask
{
public object Run(object[] args)
{
return (int)x[0] + (int)y[1];
}
}

or the last solution with a List<> or
Dictionary<> instead of object[].

The first is better. Much cleaner.
Why do you set the properties as Private?

Because I am stupid.

They shouldbe public.
And if using IoC and having the need for mocking and testing shouldn't I have something like this:

public interface ITask {
void Run();
}

public interface IAddTask : ITask {
int X { get; set; }
int Y { get; set; }
int Result { get; set; }
}

public interface IPowerTask : ITask {
double X { get; set; }
double N { get; set; }
double Result { get; set; }
}

public class AddTask : IAddTask {
public int X { get; set; }
public int Y { get; set; }
public int Result { get; set; }
public void Run() {
Result = X + Y;
}
}

public class PowerTask : IPowerTask {
public double X { get; set; }
public double N { get; set; }
public double Result { get; set; }
public void Run() {
Result = X ^ N;
}
}

public interface ITaskFactory {
ITask Get<T>() where T : ITask;
}

public class TaskFactory : ITaskFactory {
public ITask Get<T>() where T : ITask {
return ObjectFactory.GetInstance<T>();
} // Get
}

public void TestMethod1() {

IAddTask task = _factory.Get<IAddTask>();

task.X = 2;
task.Y = 4;
task.Run();

Assert.AreEqual(task.Result, 6);

}

You could.

If you really need to be able to mock those tasks.

Neither add nor power makes sense to mock.

Arne
 

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