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 6102743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:00+00:00 2026-05-23T13:39:00+00:00

Am I correct to think that I have to create my controller by passing

  • 0

Am I correct to think that I have to create my controller by passing it an instance of my context AND my Service to make it testable?

For example: new Controller(mycontext,myservice)

I’m thinking this is the way I need to change my code, but I don’t want to if I don’t have to. Since for MVC3 to work out of the box it requires controller constructors to be parameterless, I think this means I’m going to have to go down the path of IoC. Otherwise the code in my Wizard action saves to a real DBContext even during testing.

namespace mvc3test.Controllers
{

    public class WizardController : Controller
    {

        private DR405DBContext db;


        public WizardController(DR405DBContext dbContext)
        {
            db = dbContext;
        }

        public WizardController()
        {
            db = new DR405DBContext();
        }

        public ActionResult Index()
        {
            var model = new WizardViewModel();
            model.Initialize();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index([Deserialize] WizardViewModel wizard)
        {


            //wizard.Steps[wizard.CurrentStepIndex] = step;
            if (ModelState.IsValid)
            {

                    //Always save.
                    var obj = new dr405();

                    //wire up to domain model;
                    foreach (var s in wizard.Steps)
                    {
                        Mapper.Map(s,obj,s.GetType(), typeof(dr405));
                    }

                    using (var service = new DR405Service())
                    {
                        //Do something with a service here.
                        service.Save(db, obj);
                    }


                if (!string.IsNullOrEmpty(Request.QueryString["next"]))
                {
                    wizard.CurrentStepIndex++;
                }
                else if (!string.IsNullOrEmpty(Request.QueryString["prev"]))
                {
                    wizard.CurrentStepIndex--;
                }
                else
                {
                    return View("Review", wizard);

                }
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["prev"]))
            {
                wizard.CurrentStepIndex--;
            }

            return View(wizard);


        }

        public ActionResult Review(int id)
        {
            var service = new DR405Service();

            var dr405 = service.GetDR405ById(db, id);
            var wizard = new WizardViewModel();

            if (dr405 != null)
            {

                wizard.Initialize();

                foreach (var s in wizard.Steps)
                {
                    Mapper.Map(dr405, s, typeof(dr405), s.GetType());
                }
            }

            return View(wizard);
        }


        public ActionResult Transmit()
        {
            return View();

        }

        [HttpPost]
        public String Upload(HttpPostedFileBase FileData)
        {
            var saveLocation = Path.Combine(Server.MapPath("\\"), "returns\\" + DR405Profile.CurrentUser.TaxPayerID);
            System.IO.Directory.CreateDirectory(saveLocation);
            FileData.SaveAs(Path.Combine(saveLocation, FileData.FileName));
            ViewBag.Message = String.Format("File name: {0}, {1}Kb Uploaded Successfully.", FileData.FileName, (int)FileData.ContentLength / 1024);
            return ViewBag.Message;
        }

    }
}
  • 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-05-23T13:39:01+00:00Added an answer on May 23, 2026 at 1:39 pm

    Am I correct to think that I have to create my controller by passing it an instance of my context AND my Service to make it testable?

    Kind of. That’s only half of the work you need to do. The second half is to weaken the coupling between your layers by using abstractions. Your service layer needs to implement an interface which you would inject into the constructor of your controller enforcing the contract between those two and explicitly stating that the controller needs a service layer obeying this contract:

    public WizardController(IMyService service) 
    {
       this._service = service;
    }
    

    Now in your unit test go ahead and mock it using one of the multiple mocking frameworks out there (Rhino Mocks, NSubstitute, Moq, NMock, …).

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

Sidebar

Related Questions

I have a general create function that submits a new user to the database
I'm trying to think of the correct design for a web service. Essentially, this
Excuse the poor title, i can not think of the correct term. I have
I'm currently looking into unit testing for a new application I have to create.
I have a sort of general question but i think that if I tried
I want to verify that my understanding is correct. This kind of thing is
I did this in the past, and can't remember the correct command (I think
What do you think? Is this correct or are there memory leaks? Source: #include
Emacs does not recognize my correct Python path. I think it is a general
there is a situation where a have multiple controllers like a town_center controller, a

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.