为什么静态类不能实现接口?

发布于 2024-08-02 05:43:03 字数 808 浏览 6 评论 0原文

可能的重复:
为什么 C# 不允许静态方法实现接口?

在我的应用程序中,我想使用一个存储库来进行原始数据访问(TestRepositorySqlRepositoryFlatFileRepository 等) 。 因为这样的存储库将在我的应用程序的整个运行时使用,所以对我来说,将其设为静态类似乎是明智的事情,这样我就可以

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-08-09 05:43:03

接口不能有静态方法。实现接口的类需要将它们全部实现为实例方法。静态类不能有实例方法。量子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.

铜锣湾横着走 2024-08-09 05:43:03

也许我们的经验会有所帮助。我们没有使用 SqlRepository 作为静态类,而是使用 AutoFac 进行注入并将容器隐藏在静态类后面。然后每个实体都有一个静态存储库属性:

public class Part : inheritence...
{
    public static IPartRepository Repository
    {
        get { return IoCContainer.GetInstance<IRepository<Part>>(); }
    }
    // ... more part-y stuff
}

这样我们就可以交换实现,调用者总是知道从哪里获取它:

Part p = Part.Repository.Get(id);

在另一个项目中,有一个向容器注册的 PartRepository:

public class PartRepository : IPartRepository
{
    // IPartRepository implementation that talks to injected DAL
}

在另一个项目中,我们有用于测试的模拟,包括存储库预加载已知的整体:

public class MockPartRepository : Dictionary<Part, int>, IPartRepository
{
    // IPartRepository implementation based on dictionary
}

...并且它已注册到容器中以进行单元测试。同样的调用获取存储库:

Part p = Part.Repository.Get(id);

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:

public class Part : inheritence...
{
    public static IPartRepository Repository
    {
        get { return IoCContainer.GetInstance<IRepository<Part>>(); }
    }
    // ... more part-y stuff
}

This way we can swap out the implementation and callers always know where to get it:

Part p = Part.Repository.Get(id);

In another project there is a PartRepository registered with the container:

public class PartRepository : IPartRepository
{
    // IPartRepository implementation that talks to injected DAL
}

In yet another project we have mocks for testing, including repositories pre-loaded with known entires:

public class MockPartRepository : Dictionary<Part, int>, IPartRepository
{
    // IPartRepository implementation based on dictionary
}

...and it is registered with the container for unit testing. The SAME call gets the repository:

Part p = Part.Repository.Get(id);
青朷 2024-08-09 05:43:03

根据定义,接口创建了一个供实例履行的契约。由于无法实例化静态类,因此静态类无法实现接口。

不需要有静态存储库。只需将其设为非静态并在需要时实例化即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文