迟了3年,但这就是我这样做的方式:
https://github.com/lukesampson/LinqToSQL-test-extensions/
无需编写包装器或执行大量管道工作,只需将T4模板放在.dbml旁边即可获得:
两者都将自动使用您已在DBML中配置的映射.
所以你可以做的事情
public class ProductRepo {
IExampleDataContext DB { get; set };
public ProductRepo(IExampleDataContext db) {
DB = db;
}
public List<Product> GetProducts() {
return DB.Products.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
你可以用任何一个来调用它
new ProductRepo(new MemoryExampleDataContext()).GetProducts(); // for testing
Run Code Online (Sandbox Code Playgroud)
要么
new ProductRepo(new ExampleDataContext()).GetProducts(); // use the real DB
Run Code Online (Sandbox Code Playgroud)