3

If I create a controller that has a method ActionResult UploadFile(HttpPostedFileBase file) Where is the best place to add the code that will verify that this is a good and useable file? The reason I ask, is I will have to do more checking than just the file extension. For instance; check number of columns in spread sheet, check column names, etc..

3 Answers 3

5

The best way i have found to determine when i should move code from the "presentation" to the "service" layer is by asking the question... if i change client (from mvc to winforms), is this code "reusable" by both client?

In your case, for example, the code that does a deeper validation, should be in the service layer, not the controller, so it can be used the same way by multiple clients. In other words, you don't have to rewrite it again. This method works for me and keeps my controllers skinny like an anorexic Victoria's Secret model :)

1
  • This method works for me and keeps my controllers skinny like an anorexic Victoria's Secret model :) This answer would benefit greatly from some pictures. But not of your code... :) Oct 22, 2015 at 13:22
2

Does a method to check the file type being uploaded belong in the controller?

No, business logic belongs in the 'Service' layer.

Rule of Thumb with MVC:

Keep your models fat and your controllers skinny.

For more background, please see this thread.

0

You could read out the file stream in the controller and then pass the file to some business logic later which will handle more complex analysis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.