Hi,
I think it's a design question, and it's been haunting me since I started my project.
I'm working on a project involving a lot IO operations, mainly reading and writing files.
I'm trying to use MVC (Model-View-Controler) architectural strategy, but I found it's very difficult to implement, especially with the exception handling.
- Model: Data, my files to be read or written
- View: The UI interface the users operate on
- Controller: A bridge between the Model and View
I'm not sure whether I am right to use MVC or not. The reason I wanna use MVC is that I'm trying to do somethin like:
if the user wants to update data files (Model) by clicking a button on the UI (View), the function handling this user action has to notify the controller, and then the controller will call the low-level functions to accomplish the task requested, and the controller then reports the final result to UI, no matter the action is accomplished or fails with some exceptions.
Here is the solution/project layout of the simplified version of my project I'm currently working on (currently there is no MVC controller layer involved):
CompanyName.ProjectName.Startup
MainForm.cs
CompanyName.ProjectName.FileProcessing
public class FileCleaning
{
// Function to remove dirty info in all srcFiles and
// save them into new files
public static void CleanFiles(string[] srcFiles,
string[] dstFiles,
CleaningOptions, options);
}
CompanyName.ProjectName.Utilities
(CompanyName.ProjectName.Controller -- not added/implemented currently)
The basic procedure for a user to clean a batch of files can be described:
- User starts the app from CompanyName.ProjectName.Startup;
- User selects a batch of files;
- User choose some cleaning options;
- User clicks a "Clean button";
- MainForm.cs collects the user input (srcFiles, and cleaning options);
- MainForm.cs calls a
backgroundWorker
. the file cleaning is a time-consuming process, I don't wanna freeze the UI. - The backgroundWorker will call
CompanyName.ProjectName.FileProcessing.FileCleaning.CleanFiles(srcFiles, dstFiles, options)
. - The cleaned files will be DIRECTLY saved in the above function, which means no return value.
I understand that it's gonna be difficult to answer a question like "how can I design it". but I do have some questions like below:
- Is my current design poor?
- How to handle exceptions, should I throw exceptions in lower-level functions or higher-level functions?
- Besides IO exception, how about other exceptions?
- The result is directly written to dstFiles, it's kind of haunting me, how can I avoid this?
- How to add the MVC controller layer so that we can handle failure cases, and also separate the IO handling from low-level cleaning functions?