I'm working on a small project, inspired by the awesome System.IO.Abstractions, aiming to provide a wrapper over System.Diagnostics.Process, to assist testability of anything process-related.
Where System.IO.Abstractions provides a wrapper over the Path, File, Directory types, as well as a wrapper objects over FileInfo and DirectoryInfo, all conveniently accessible from an IFileSystem, mirroring the way those types exist today in the .NET framework, the System.Diagnostics.Process type presents its own challenges when trying to create a convenient wrapper.
Your feedback on the API is, therefore, very much appreciated.
The Process class has several static methods for launching a new process, as well as other stuff:
void EnterDebugMode();
Process GetCurrentProcess();
Process GetProcessById(int processId);
Process GetProcessById(int processId, string machineName);
Process[] GetProcesses();
Process[] GetProcesses(string machineName);
Process[] GetProcessesByName(string processName);
Process[] GetProcessesByName(string processName, string machineName);
void LeaveDebugMode();
Process Start(ProcessStartInfo startInfo);
Process Start(string fileName);
Process Start(string fileName, string arguments);
Process Start(string fileName, string userName, SecureString password, string domain);
Process Start(string fileName, string arguments, string userName, SecureString password, string domain);
In addition, the instance methods of Process also have lots in them.
My initial idea for the API is to create the following:
-
An injectable
IProcessSysteminterface, containing the "static" method signatures (see above). From this interface it would be possible to start a process instance, just as it's possible via the static method:Process process = Process.Start("calc.exe");becomes
IProcessSystem processSystem = ... // get process system wrapper somehow IProcess process = processSystem.Start("calc.exe"); -
In addition, the ability to create processes directly by using a
ProcessWrapperclass, having the same methods as the originalProcessclass:
var process = new Process();
process.StartInfo.FileName = "calc.exe";
process.Start();
becomes
`var process = new ProcessWrapper();`
`process.StartInfo.FileName = "calc.exe";`
`process.Start();`
ProcessWrapper implements IProcess and derives from ProcessBase, which contains abstract definitions of the methods of the System.Diagnostics.Process type.
-
What do you think about the name
IProcessSystemto hold the static methods ofProcess? Can it be named something else? -
Thinking about testability, should this library discourage from creating
ProcessWrapperinstances directly, and using only theIProcessSystemmethods? -
Should I add new API to
IProcessSystem, allowing, for example, create processes without starting them (currently not possible using the staticProcess.Startmethods)?
I agree with Daniel.
IProcessFactory- to create all the thingsIProcessManager- to manage all the thingsIgnore the fact that statics exist on Process, this is a new API (even though you will consume the statics under the hood)
The one problem with
System.IO.Abstractionsis that it tries too hard to mirror the existing full framework API, which means it is still not easily portable to other framework runtimes. Identify all the frameworks you want to support and make that known now.