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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:09:11+00:00 2026-05-23T17:09:11+00:00

I found a tutorial in microsoft asp.net website , that make an instance from

  • 0

I found a tutorial in microsoft asp.net website, that make an instance from context like this:

        public class HomeController : Controller
        {
            private MoviesDBEntities _db = new MoviesDBEntities(); 

            public ActionResult Index()
            {
                return View(_db.MovieSet.ToList());
            }

            ...
        }

this means each time controller instantiate, context will be instantiate too. but Here i was found that correct way to instantiate context is like this:

        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                using(MoviesDBEntities _db = new MoviesDBEntities()){
                    return View(_db.MovieSet.ToList());
                }
            }
            ...
        }

so the question is, Which way is true? or is it important at all?

  • 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-23T17:09:11+00:00Added an answer on May 23, 2026 at 5:09 pm

    Generally, a class should not be responsible for instantiating its own dependencies.

    The best thing to do is to pass the dependency into the constructor:-

    public class HomeController : Controller
    {
      private IMovieRepository _db;
      public HomeController(IMovieRepository db)
      {
        _db = db;
      }
    
      public ActionResult Index()
      {
        return View(_db.MovieSet.ToList());
      }
    }
    

    This is called “Dependency Injection”, and it’s desireable because it reduces boilerplate code, and also allows you to supply other IMovieRepository implementations at runtime (e.g. if you’re unit testing).

    ASP.NET MVC has a hook to allow you to do this (ControllerBuilder), but luckily you don’t need to roll your own, there are pre-made solutions out there. I recommend http://ninject.org/ because it’s very easy to set up.

    That way your IoC Container (whatever is passing the dependency in) can instantiate only one context per HTTP request, and dispose of it at the end of the request.

    Check out e.g. What is Inversion of Control?

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

Sidebar

Related Questions

I've used a tutorial ( http://support.microsoft.com/kb/317535 ) to create a VB.NET Class that exports
I'm running through this tutorial found here: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htm which teaches how to pass a
I have just found this great tutorial as it is something that I need.
I want to create an FAQ page like the one on this website http://www.microsoft.com/windows/windows-7/faq.aspx
I found this tutorial at tizag.com. But It is for displaying entries from different
I found a tutorial online that extends that Apple QuickStart Application which is the
So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title
Below is some html I found in this jquery tooltip tutorial, the contents inside
I found a tutorial from nettuts and it has a source code in it
I found this tutorial about ffmpeg the thing i do not get is how

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.