为什么静态类不能实现接口?
可能的重复:
为什么 C# 不允许静态方法实现接口?
在我的应用程序中,我想使用一个存储库来进行原始数据访问(TestRepository
、SqlRepository
、FlatFileRepository
等) 。 因为这样的存储库将在我的应用程序的整个运行时使用,所以对我来说,将其设为静态类似乎是明智的事情,这样我就可以
SqlRepository.GetTheThingById(5);
不必一直重新生成它。 因为我希望我的存储库可以互换,所以我希望它们实现一个通用接口:IRepository
。 但当我尝试这样做时,我得到:
静态类不能实现接口
为什么不能? 那么你建议我如何改变我的设计呢?有我可以使用的模式吗?
更新
五年后:这个问题被访问了 20k+ 次,我了解了存储库模式的缺点,了解了 IoC,并意识到我的问题表述得很糟糕。
我并不是真正在问接口的 C# 规范是什么,而是为什么它故意以这种特定方式限制我。
实际的答案是调用实例或类型上的方法的语法是不同的。但问题已经结束了。
Possible Duplicate:
Why Doesn’t C# Allow Static Methods to Implement an Interface?
In my application I want to use a Repository that will do the raw data access (TestRepository
, SqlRepository
, FlatFileRepository
etc).
Because such a repository would be used throughout the runtime of my application it seemed like a sensible thing to me to make it a static class so I could go
SqlRepository.GetTheThingById(5);
without it having to be regenerated all the time.
Because I want my repositories to be interchangeable, I want them to implement a common interface: IRepository
.
But when I try to do so, I get:
Static classes cannot implement interfaces
Why can't they?
How do you suggest I change my design then? Is there a pattern I could use?
UPDATE
Five years later: this question is visited 20k+ times, I learned about the disadvantages of the repository pattern, learned about IoC and realise my question was poorly formulated.
I wasn't really asking what the C# specification of an interface is, rather why it was deliberately restricting me in this specific way.
The practical answer is that the syntax for calling a method on an instance or on a type are different. But the question is closed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接口不能有静态方法。实现接口的类需要将它们全部实现为实例方法。静态类不能有实例方法。量子ED。
2022 年更新:.NET 7 确实支持静态接口方法。
Interfaces can't have static methods. A class that implements an interface needs to implement them all as instance methods. Static classes can't have instance methods. QED.
Updated in 2022: .NET 7 does support static interface methods.
也许我们的经验会有所帮助。我们没有使用 SqlRepository 作为静态类,而是使用 AutoFac 进行注入并将容器隐藏在静态类后面。然后每个实体都有一个静态存储库属性:
这样我们就可以交换实现,调用者总是知道从哪里获取它:
在另一个项目中,有一个向容器注册的 PartRepository:
在另一个项目中,我们有用于测试的模拟,包括存储库预加载已知的整体:
...并且它已注册到容器中以进行单元测试。同样的调用获取存储库:
Maybe our experience will help. Rather than SqlRepository as a static class, we use AutoFac for injection and hide the container behind a static class. Then each entity has a static repository property:
This way we can swap out the implementation and callers always know where to get it:
In another project there is a PartRepository registered with the container:
In yet another project we have mocks for testing, including repositories pre-loaded with known entires:
...and it is registered with the container for unit testing. The SAME call gets the repository:
根据定义,接口创建了一个供实例履行的契约。由于无法实例化静态类,因此静态类无法实现接口。
不需要有静态存储库。只需将其设为非静态并在需要时实例化即可。
By definition, interfaces create a contract for instances to fulfill. Since you cannot instantiate a static class, static classes cannot implement interfaces.
There is no need to have a static repository. Simply make it non-static and instantiate it when you need it.