title: Asp.net mvc EF4.1 DbContext and Service Layer
body: <p>Is it a bad practice to expose DbContext in your service layer?</p>
  
  <p>For Example:</p>
  
  <pre><code>private readonly IRepository&lt;SkillLevels&gt; _repository;
  private readonly IDatabaseFactory _databaseFactory;
  private readonly IUnitOfWork _unitOfWork;
  
  public SkillLevelService(IRepository&lt;SkillLevels&gt; repository, IDatabaseFactory databaseFactory, IUnitOfWork unitOfWork)
  {
      _repository = repository;
      _databaseFactory = databaseFactory;
      _unitOfWork = unitOfWork;
  }
  
  public void InsertSkillLevel(SkillLevels entity)
  {
      _repository.Insert(entity);
      _unitOfWork.Commit();
  }
  </code></pre>
  
  <p>Here, IDatabaseFactory can return my DbContext object. I exposed it here because if i need other entities other than SkillLevels, so that i can use them in this service class. In case you know, for complex queries when you need to select multiple tables to fetch data.</p>
  
  <p>But i'm not sure if it makes a bad practice!!</p>
  
  <p>Any help is appreciated. Thanks.</p>