Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:24:34+00:00 2026-06-18T20:24:34+00:00

I have a situation where I’m debating how to architect my controllers. Consider the

  • 0

I have a situation where I’m debating how to architect 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)] //$.ajax({ data: html5FormDataFileChunk , processData: false ... })
        public ActionResult UploadBlock(string fileName, int blockId){

             var fileUploadCommand = (FileUploadCommand)ExtractFromSessionData(fileName);
             var result = commandBus.Submit(fileUploadCommand);
             ...
        }

        public ActionResult CommitFileUploads(string[] filesToCommit){
             var commitFileUploadCommand = (CommitFileUploadCommand)ExtractFromSessionData(fileName);
             var result = commandBus.Submit(commitFileUploadCommand );
             ...
        }

In this controller, I use the command pattern and pass a model to my commandBus which interfaces with my domain. The first three [HttpPost] methods on the controller are for handling jQuery ajax calls from a responsive file uploading UI.

Consider the situation where a user fills out a form (an interview) and uploads some files along with it. Although the user can upload the files before submitting the form, I don’t want the uploaded files to be committed until AFTER they submit the form and it passes validation. That is why the last method on the controller is not an http endpoint. As such 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 I’m using IoC to inject a commandBus into the FileSharingController so I cannot just instantiate it with default constructor as I am doing.

My options to consider:

  • Create a custom controller factory to allow instantiating my controller anywhere in the code.
  • Turn my FileSharingController in a WebAPI controller and treat as a service

Which is the better design path for this situation? If the latter case, how can I keep the CommitFileUploads() method private? I don’t want it to be exposed as an endpoint that can be triggered without first validating the rest of the form.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:24:36+00:00Added an answer on June 18, 2026 at 8:24 pm

    You can instantiate your controller like this:

    ICommandBus commandBus = DependencyResolver.Current.GetService<ICommandBus>();
    var fileShareController = new FileSharingController(commandBus);
    

    Generic GetService() method is extension method, so make sure that you have “using System.Web.Mvc;” line in the cs file.

    But then, it’s better to have helper class that is responsible for keeping/storing already uploaded files, and call it from both controllers, instead instantiating controllers manually.

    For example:

    public class FileUploadManager
    {
        public FileUploadManager(ICommandBus commandBus, HttpSessionStateBase sessionState)
        {
             //....
        }
    }
    

    and then you call it:

    ICommandBus commandBus = DependencyResolver.Current.GetService<ICommandBus>();
    var fileShareController = new FileUploadManager(commandBus, this.HttpContext.Session);
    

    Or, if you don’t want to use DependencyResolver, you pass ICommandBus to both controller’s constructors, and use that reference to instantiate helper class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation like this: // Object Class class Person_Object { protected $_id; public
i have situation like this: class IData { virtual void get() = 0; virtual
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have situation to extend the Enumerable class in c# to add the new
I have situation where my Java class needs to create a ton of certain
I have this situation where I declare inside my main class a function that
I have a situation where I am sending data to a Controller via jQuery
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.