2

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?

Community
  • 1
  • 1
Jalal
  • 6,097
  • 9
  • 59
  • 95

3 Answers3

6

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?

Community
  • 1
  • 1
Iain Galloway
  • 16,882
  • 4
  • 50
  • 73
  • 4
    You should remove the field initializer for _db. It's also worth pointing out that the disposal of the context should be made in the class creating the context/DI container. – PHeiberg Jul 15 '11 at 07:39
  • I think this is the good point. With using statement, every time initiating and disposing `DbEntities` are not good, which is a relatively heavy object. – Jin-Wook Chung Jul 15 '11 at 08:58
  • @PHeiberg: Ah! Whoops! Fixed! :) – Iain Galloway Jul 15 '11 at 10:30
2

It's all about freeing up resources.

In a statefull situation objects are more likely to live longer, and therefor a using-statement is better to dispose the context immediately after you used it.

In a stateless situation (ie asp.net) the server receives a request, generates a result, sends it back and disposes used resources (including the context).

If you know almost all methods will probably use a specific context in a stateless situation, it's a bit easier to make it a field and instantiate the context on object creation instead of typing it out in each and every method.

Peanutbag
  • 277
  • 1
  • 4
  • 11
1

The second is better as the using statement will automatically free up resources consumed by the IDisposable ObjectContext when it goed out of scope automatically.

In my opinion the examples on the official asp.net site are often just 'working' rather than good examples of how to build an application.

thekip
  • 3,536
  • 2
  • 18
  • 39