静态存储库...无法使用DI,怎么办?
我遇到的情况是,我们需要修改从第三方开源应用程序 (NopCommerce) 中的静态存储库返回的内容。问题是它们使用静态存储库,所以我不能仅仅继承一个接口并 DI 我自己的存储库。我试图在不修改 NopCommerce 代码库的情况下做到这一点......有什么新想法吗?
编辑:我希望 NopCommerce 使用我的存储库,而不是让我的代码使用他们的代码。
I am in a situation where we need to modify what is being returned from the static repository in a 3rd party open-source application (NopCommerce). The problem is that they use static repositories, so I can't merely inherit an interface and DI my own repository. I'm trying to do this without modifying the NopCommerce code-base... any fresh ideas?
Edit: I want NopCommerce to use my repos, rather than have my code use theirs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过创建自己的接口和委托给 NopCommerce 的类实现来抽象掉他们的东西。然后让您的代码使用该接口,而不是直接访问 NopCommerce 的类。在结果返回到应用程序之前,您可以在类中修改 NopCommerce 的输出。
作为额外的好处,您还可以模拟接口来执行一些不需要成熟的存储库实现的测试。
像这样的代码:
You could abstract away their stuff by creating an interface of your own and a class implementation that delegates to NopCommerce. Then have your code use the interface instead of directly accessing NopCommerce's classes. You can modify the output of NopCommerce inside your class before the result is returned to your application.
And as an added bonus you could also mock the interface to do some tests that didn't require the full-blown repository implementations.
Something like this, in code:
我们目前的期限非常非常紧迫,而且这个问题是没有预见到的。所以我想首先从穷人的静态接口/穷人的 DI 开始,如下所示(这样我就不必修改整个解决方案)。然后稍后,当我们时间不那么紧迫时,改用接口和依赖注入并向 NopCommerce 提交补丁:
有什么想法吗?
We are currently on a really, really tight deadline, and this problem was not forseen. So I am thinking of first starting with a poor man's static interface/poor man's DI like the following (so I don't have to modify the entire solution). Then at a later time, when we are not-so-pressed for time, change over to use an interface and dependency injection and submit a patch to NopCommerce:
Any thoughts?
听起来像是 Facade 的工作。
Sounds like a job for Facade.