简体   繁体   English

如何使用语句填充?

[英]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. 停止在任何代码中使用new关键字。 If this SomeEntities is of utmost necessity pass it to the constructor of the class (constructor Injection). 如果此SomeEntities非常必要,则将其传递给类的构造函数(构造函数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). 如果需要重写SomeEntities并且分配不是创建类对象的主要要求,并确保在函数调用(属性注入)之前设置SomeEntities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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