I have the following:
public interface IBehaviour
{
event EventHandler Completed;
void Execute();
}
public interface IBehaviourA : IBehaviour
{
// Some specific stuff here
object A { get; set; }
}
public interface IBehaviourB : IBehaviour
{
// Some specific stuff here
object B {get;set;}
}
public interface IBehaviourQueue
{
void Run();
BehaviourQueueItem<IBehaviour> AddBehaviour<T>() where T : IBehaviour;
}
public class BehaviourQueue : Queue<BehaviourQueueItem<IBehaviour>>, IBehaviourQueue
{
private IBehaviourFactory factory;
public BehaviourQueue(IBehaviourFactory factory)
{
this.factory = factory;
}
public BehaviourQueueItem<IBehaviour> AddBehaviour<T>() where T:IBehaviour
{
T behaviour = factory.GetNew<T>();
var queueItem = new BehaviourQueueItem<IBehaviour>(behaviour);
Enqueue(queueItem);
return queueItem;
}
public void Run()
{
//Run each queue item
}
}
public class BehaviourQueueItem<T>
{
public IBehaviour behaviour;
public BehaviourQueueItem(IBehaviour behaviour)
{
this.behaviour = behaviour;
}
public void WhenComplete(Func<T, bool> action)
{
CompletedAction = action;
}
public BehaviourQueueItem<T> ConfigureFor<Z>(Action<Z> dow) where Z : IBehaviour
{
dow((Z)behaviour);
return this;
}
}
This is how it is used:
var q =new BehaviourQueue(new BehaviourFactory()); // Queue here is IBehaviour
q.AddBehaviour<IBehaviourA>()
.ConfigureFor<IBehaviourA>(x => x.A = "someValueA")
.WhenComplete(x => DoStuffWithSomeProperty(((IBehaviourA)x).A));
q.AddBehaviour<IBehaviourB >()
.ConfigureFor<IBehaviourB >(x => x.B = "someValueB")
.WhenComplete(x => DoStuffWithSomeProperty(((IBehaviourB)x).B));
What I don't really like is that I have to specify which type of IBehaviour
I am referring to every time. I would like to be able to write:
var q =new BehaviourQueue(new BehaviourFactory()); // Queue here is IBehaviour
q.AddBehaviour<IBehaviourA>()
.Configure(x => x.A = "someValueA")
.WhenComplete(x => DoStuffWithSomeProperty(x.A));
q.AddBehaviour<IBehaviourB>()
.Configure(x => x.B = "someValueB")
.WhenComplete(x => DoStuffWithSomeProperty(x.B));
I know that I have to modify this part:
public BehaviourQueueItem<IBehaviour> AddBehaviour<T>() where T:IBehaviour
{
T behaviour = factory.GetNew<T>();
var queueItem = new BehaviourQueueItem<IBehaviour>(behaviour);
Enqueue(queueItem);
return queueItem;
}
to something like
public BehaviourQueueItem<T> AddBehaviour<T>() where T:IBehaviour
{
T behaviour = factory.GetNew<T>();
var queueItem = new BehaviourQueueItem<T>(behaviour);
Enqueue(queueItem); // It won't let me know do it because of Covariance/Contravariance thingy
return queueItem;
}
Do you have any idea what I should write to be able to create a base typed list and add specific items and configuring it fluently?
Keep in mind that I should be able to add any type of IBehaviour
to BehaviourQueue
so the queue can't be a derived type.