C# – Alternatives to Type Casting in Your Domain

cpolymorphismtype casting

In my Domain I have an entity Activity which has a list of ITasks. Each implementation of this task has it's own properties beside the implementation of ITask itself. Now each operation of the Activity entity (e.g. Execute()) only needs to loop over this list and call an ITask method (e.g. ExecuteTask()).

Where I'm having trouble is when a specific tasks' properties needs to be updated, as in configuring the task (not as part of the execute method). How do I get an instance of that task? The options I see are:

  • Get the Activity by Id and cast the task I need. This'll either sprinkle my code with:
    • Tasks.OfType<SpecificTask>().Single(t => t.Id == taskId)
    • or Tasks.Single(t => t.Id == taskId) as SpecificTask
  • Make each task unique in the whole system (make each task an entity), and create a new repository for each ITask implementation

I don't like either option, the first because I don't like casting: I'm using NHibernate and I'm sure this'll come back and bite me when I start using Lazy Loading (NHibernate currently uses proxies to implement this). I don't like the second option because there are/will be dozens of different kind of tasks. Which would mean I'd have to create as many repositories.

Am I missing a third option here? Or are any of my objections to the two options not justified? How have you solved this problem in the past?

Best Answer

It is true that casting is bad when it introduces coupling. But that doesn't mean it's always bad. There's not a lot wrong with

ITask task = Tasks.Single(t => t.Id == taskId);
IArduousTask arduous = task as IArduousTask;

if (arduous != null)
{
    arduous.HaveLunchBreak();
}

task.DoTask();

This way, we're not tied to an implementation of ITask or IArduousTask. And any task can implement IArduousTask (with a different implementation of HaveLunchBreak(), if you choose) and HaveLunchBreak will automatically get called. But tasks that don't implement IArduousTask will not break.

However, in your case, you should probably also reconsider whether your Task objects should be mutable. It might be better to do this:

ITask updated = task.GetUpdated(someData);
activity.UpdateTask(updated);

Where GetUpdated is a new method on ITask and activity.UpdateTask matches on updated.ID and replaces the entire object in the array. This leaves the implementation of GetUpdated to the task type itself.

someData can either be all the data that could possibly be required to perform an update of any type of task, or it could be a dictionary (or dynamic object) containing data from a task-specific UI, passed into a specific task.