为什么 ObjectContext 类不派生自某些接口?
我认为 MS 的人比我聪明得多。我试图构建/测试一个几乎遵循 此 方法,除了我想松散地耦合存储库内的 ObjectContext 依赖项。我发现为了解耦这个问题,我需要跳很多圈,如 这篇文章。 时,即使这种方法也很难使用
- 当您有来自现有数据库的 edmx
- 您有一个围绕 ObjectContext 接口和 IObjectSet 构建的通用存储库
- 。在进行单元测试时,您希望伪造此对象上下文并将所有操作保留在内存中。考虑测试存储库。
现在真正的问题是,为什么 ObjectContext 的创建者决定不使用 IObjectContext ?
我希望我的问题有意义,如果有人能证明它没有意义并为我指明方向,我会很高兴。
提前致谢!
I consider folks at MS way more smarter than I am. I was trying to build/test a repository which almost follows this approach except that I want to loosely couple the ObjectContext dependency inside the repository. I found out that in order to do decouple this I need to jump a lot of hoops as shown in this article.Even this approach is difficult to work with when
- You have an edmx from an existing database
- You have a generic repository built around the ObjectContext interface and IObjectSet
- While unit testing you want to fake out this object context and keep all the operations in memory. Think testing Repositories.
Now the real question, why did the creators of ObjectContext decide not to have IObjectContext ?
I hope my question makes sense, I will be glad if someone can prove that it doesnt and shows me the way.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于上下文是一个分部类,因此您可以轻松地在单独的文件中向其添加接口:
公共分部类 YourContext : IMyCustomInterface
,您可以在 IMyCustomInterface 中放入您想要从生成的 ObjectContext 使用的任何签名。或者,您可以采用(通常)更推荐的方法,即将 ObjectContext 进一步抽象为存储库,如 这篇博文(整个系列的帖子都很有趣且相关)或这个。
Since the context is a partial class, you can easily add an interface to it in a separate file:
public partial class YourContext : IMyCustomInterface
, and you can put in IMyCustomInterface any signatures you want to use from the generated ObjectContext.Or you could go about the (generally) more recommended way, which is to abstract further than the ObjectContext into Repositories like in this blog post(that entire series of posts is interesting and relevant) or this one.