I have a situation where I am discussing how to archive my controllers.
Consider the following controller:
public class FileSharingController : Controller { private readonly ICommandBus commandBus; public FileSharingController(ICommandBus commandBus) { this.commandBus = commandBus; } [HttpPost] public ActionResult PrepareMetadata(int blocksCount, string fileName, long fileSize) { ... } [HttpPost] public ActionResult ClearFileMetadata(string fileName){ ... } [HttpPost] [ValidateInput(false)]
In this controller, I use the command template and pass the model to my commandBus, which interacts with my domain. The first three [HttpPost] on the controller are designed to handle jQuery ajax calls from the user user interface.
Consider a situation where a user fills out a form (interview) and uploads some files with it. Although the user can upload files before submitting the form, I donโt want the downloaded files to be committed until they submit the form and validate. This is why the last method on the controller is not the http endpoint. Therefore, I have the following controller:
public class InterviewController : Controller { [HttpGet] public ActionResult UserInterview() { InterviewViewModel viewModel = new InterviewViewModel (); return PartialView(viewModel); } [HttpPost] [AllowAnonymous] public ActionResult UserInterview(InterviewViewModel viewModel) { if(ModelState.IsValid) { var fileSharingController = new FileSharingController(); fileSharingController.CommitFileUploads(viewModel.Files); } return PartialView(viewModel); } }
The problem is that I use IoC to enter the commandBus in the FileSharingController, so I cannot just create an instance with the default constructor, as I do.
My options are:
- Create a custom factory controller to allow an instance of my controller anywhere in the code.
- Turn my FileSharingController into a WebAPI controller and see how the service
What is the best design path for this situation? If in the latter case, how can I keep the CommitFileUploads() method private? I do not want it to appear as an endpoint that can be launched without first checking the rest of the form.
source share