title: WebForms with Entity Framework without DI. How I should use and dispose context?
body: <p>I would like to use the legacy Web Forms technology with Entity Framework.
  I can't use Dependency Injection.
  I would like use 3 layers ( Pages, BL and DAL )
  I'd like to release unmanaged resources.
  So...Does this code make sense ? (I'm just putting here just parts, but I believe it is clear)</p>
  <p>My questions:</p>
  <p>Do I have to use &quot;using&quot; to dispose services and dbContext?
  My page use 2 different services. I want to use one context per request</p>
  <p>Maybe there is a better way?</p>
  <p>2 different services:</p>
  <pre><code>using DomainApp.Services.Contracts;
  using DomainApp.DTOS;
  using System.Linq;
  using System.Data.Entity;
  using System;
  
  namespace DomainApp.Services
  {
      public class UserService : IUserService, IDisposable
      {
          private DomainAppContext _context { get; set; }
          public UserService(DomainAppContext context)
          {
              _context = context;
          }
  
          public User GetUserByName(string userName)
          {
              using (var context = new DomainAppContext())
              {
                  return context.Users
                      .Include(x =&gt; x.UserRoles.Select(y =&gt; y.Role))
                      .FirstOrDefault(x =&gt; x.Name == userName);
              }
          }
  
          /*disposable*/
          private bool disposed = false;
          protected virtual void Dispose(bool disposing)
          {
              if (!this.disposed)
              {
                  if (disposing)
                  {
                      _context.Dispose();
                  }
              }
              this.disposed = true;
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
      }
  }
  </code></pre>
  <p>second service</p>
  <pre><code>    using DomainApp.DTOS;
      using DomainApp.Services.Contracts;
      using System.Linq;
      using System.Collections.Generic;
      using System;
      using System.Data.Entity;
      
      namespace DomainApp.Services
      {
          public class TaskItemService : ITaskItemService, IDisposable
          {
              private DomainAppContext _context { get; set; }
              public TaskItemService(DomainAppContext context)
              {
                  _context = context;
              }
              
              public IList&lt;TaskItem&gt; GetTaskItemsByUserId(int userId)
              {
                  return _context.TaskItems
                      .Include(x =&gt; x.Status)
                      .Include(x =&gt; x.TaskType)
                      .Where(x=&gt;x.UserId==userId).ToList();
              }
      
              /*disposable*/
              private bool disposed = false;
              protected virtual void Dispose(bool disposing)
              {
                  if (!this.disposed)
                  {
                      if (disposing)
                      {
                          _context.Dispose();
                      }
                  }
                  this.disposed = true;
              }
      
              public void Dispose()
              {
                  Dispose(true);
                  GC.SuppressFinalize(this);
              }
          }
  }
  </code></pre>
  <p>page (aspx.cs file )</p>
  <pre><code>using DomainApp;
  using DomainApp.DTOS;
  using DomainApp.Services;
  using DomainApp.Services.Contracts;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Security;
  using System.Web.UI;
  
  namespace AdvancedWebFormApp.Pages
  {
      public partial class Task : Page
      {
          public IList&lt;TaskItem&gt; TaskItems { get; set; }
  
          private IUserService _userService;
  
          protected void Page_Init(object sender, EventArgs e)
          {
              
          }
          protected void Page_Load(object sender, EventArgs e)
          {
              var _appContext = new DomainAppContext();
              using (var _userService = new UserService(_appContext))
              {
                  var cookie = this.Request.Cookies[FormsAuthentication.FormsCookieName];
                  var ticket = FormsAuthentication.Decrypt(cookie.Value);
  
                  var roles = ticket.UserData.Split('|').ToList();
                  if (!roles.Contains(&quot;User&quot;))
                  {
                      this.Response.Redirect(FormsAuthentication.LoginUrl);
                  }
                  else
                  {
                      var user = _userService.GetUserByName(ticket.Name);
                      if (user != null)
                      {
                          using(var _taskItemService = new TaskItemService(_appContext))
                          {
                              TaskItems = _taskItemService.GetTaskItemsByUserId(user.UserId);
                          }     
                      }
                      else
                      {
                          TaskItems = new List&lt;TaskItem&gt;();
                      }
                  }
              }
          }
      }
  }
  </code></pre>