Question

I have a method similar to the following:

public List<EmployeeReports> MyMethod (int empId, DateTime startDate)
{
  using(SomeEntities se = new SomeEntities())
  {
   List<EmployeeReports> reports = se.EmployeeReports
   .Where(x => x.EmployeeId == empId
   && DateTime.Compare(x.DateEntered, startDate).ToList();
  }

 return reports;
}

This is an entity framework object to a database.

How can the using block be shimmed?

Was it helpful?

Solution

stop using the new keyword in any code. If this SomeEntities is of utmost necessity pass it to the constructor of the class (constructor Injection). If SomeEntities needs to be overriden and assignment is no the prime requirement for creating the object of the class and ensure that SomeEntities are set before the function call (Property Injection).

OTHER TIPS

I would abstract the SomeEntities class through an interface, That way you can inject an alternative implementation of the same interface in your tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top