views:

56

answers:

2

I have a number of objects that I need to create and add to an array. However the code below seems dirty and difficult to maintain in the long run. What I'm thinking is, I should store the Name and Value properties in a table and build each comCommand object at runtime.

However, I'm not exactly sure what the best method to about doing this... Reflection, Activator.CreateInstance or some kind of object factory?

Thanks in advance.

 var engine = new comCommand() { commandName = "-e", commandValue = "PNetTNative" };
 var outputFile = new comCommand() { commandName = "-f", commandValue = OutputFile };
 var groupSize = new comCommand() { commandName = "-GroupSizeParamInput1ParamsIn", commandValue = GroupSize };
 var pagesPerSheet = new comCommand() { commandName = "-PagesPerSheetParamInput1ParamsIn", commandValue = PagesPerSheet };
 var outputFileName = new comCommand { commandName = "-OutputFileNameParamInput1ParamsIn", commandValue = OutputFileName };
 var duplex = new comCommand { commandName = "-DuplexParamInput1ParamsIn", commandValue = Duplex };
 var processId = new comCommand { commandName = "-ProcessIDParamInput1ParamsIn", commandValue = ProcessID };

 var request = new comRunWorkFlowReq(); 
 request.command = new[] { engine, outputFile, groupSize, pagesPerSheet, outputFileName, duplex, processId };
A: 

Go for Unity Container -> Unity

anivas
+1  A: 

Create a command constructor (as Kirk suggested) and keep it as you have: multiple comCommand("-e","PNetTNative") etc calls.

The reason for keeping it in code is you get compiler-time type and error checking... Yes, you can do it at runtime (various methods) but for just 7 declarations, it's best to keep it at compile time.

Dekker500
I have much more than that 7 declarations. I only put a small snippet of the over all method. Thanks for the suggestions
Tom Alderman
Well, my preference would still be to keep it in code that can be compiled, as long as the 'list' of items is static (won't change at runtime). Judging by the code you've given so far, it appears to be an interface to a command-line tool, so you'd be pretty safe implementing it directly in the code
Dekker500
You got it... I'm automation a process to run this third party application. I do appreciate the advice.
Tom Alderman