I have 5 tasks that a step of my application needs to perform.
- Receive a CSV file as part of a user upload.
- Parse each row in the CSV to a canonical representation.
- Create an ORM POCO relating to that representation.
- Save each item to the database.
- Send an email to an email address defined in each POCO.
I have a repository already for the database ORM stuff, and I’ve at least got it figured out that the controller probably shouldn’t be new’ing up an SmtpClient by itself, but how much ‘glue’ goes into the controller? Is exposing any more details than my snippet below a code smell?
public ActionResult Index(HttpPostedFileBase file) {
var result = model.HandleFileUpload (file);
if (result.Success)
{
return SuccessAction("Success");
} else {
return FailureAction("Failure");
}
}
If this does all belong in the model, what’s the type of work I’m doing called, if I’m just “gluing” lower-level things together?
The thin controller you describe looks pretty good to me. It’s easily testable, and makes it easy for you to adhere to Single Responsibility Principle by creating a set of loosely-coupled service classes in your model, each of which handle a different step of your process.
This also allows you to make use of the code outside of the confines of your MVC application, should the need arise.
So, I would envisage you having a
FileHandlerclass implementing yourHandleFileUploadmethod.This would define the steps of the process, perhaps calling out to
FileParser,Repository, andEmailSenderclasses, each of which could be consumed via interfaces and dependency-injected into yourFileHandler.What’s the type of work you’re doing? Developing maintainable software of which you can be proud 🙂