in all (Agile) articles i read about this: keep your code and functions small and easy to test.
How should i do this with the 'controller' or 'coordinator' class?
In my situation, i have to import data. In the end i have one object who coordinates this, and i was wondering if there is a way of keeping the coordinator lean(er) and mean(er).
My coordinator now does the followling (pseudocode)
//Write to the log that the import has started
Log.StartImport()
//Get the data in Excel sheet format
result = new Downloader().GetExcelFile()
//Log this step
Log.LogStep(result )
//convert the data to intern objects
result = new Converter().Convertdata(result);
//Log this step
Log.LogStep(result )
//write the data
result = Repository.SaveData(result);
//Log this step
Log.LogStep(result )
Imho, this is one of those 'know all' classes or at least one being 'not lean and mean'? Or, am i taking this lean and mean thing to far and is it impossible to program an import without some kind of 'fat' importer/coordinator?
Michel
EDIT this is actually a two-in-one question: one is how to test it, second is if it's ok to have a 'know all/glue all together' coordinator
I am sure that many people would disagree, but I think your method is in general lean enough. The crucial bit you are missing here though is dependency injection (also known as inversion of control) - so instead of newing Downloader, Converter etc. inside your method, you should define interfaces for these classes and "inject" them in the constructor of your class:
private Downloader downloader;
private Converter converter;
public MyClass(Downloader downloader, Converter converter)
{
this.downloader = downloader;
this.converter = converter;
//same with repository, it can't be a static class anymore
}
then you just use these instances in your method:
//Write to the log that the import has started
Log.StartImport()
//Get the data in Excel sheet format
result = downloader.GetExcelFile()
//Log this step
Log.LogStep(result )
//convert the data to intern objects
result = converter.Convertdata(result);
//Log this step
Log.LogStep(result )
//write the data
result = repository.SaveData(result);
//Log this step
Log.LogStep(result )
now after doing this change, in your test you can use one of the mocking frameworks (I use RhinoMocks) to inject a mocked implementation of the dependencies into your class. This way you can just verify that the correct method was called on the converter and downloader, without reading anything from disk and parsing any spreadsheets.
Examples of how to use RhinoMocks are in their documentation: http://ayende.com/wiki/Rhino+Mocks+Documentation.ashx
Don't hesitate to ask another question if you get stuck :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With