简体   繁体   中英

How to shim using statement?

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?

我将通过一个接口抽象SomeEntities类,这样您就可以在测试中注入同一接口的替代实现。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM