Why static classes cant implement interfaces?

C#.NetInterfaceStatic Classes

C# Problem Overview


> 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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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);

Solution 3 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoris CallensView Question on Stackoverflow
Solution 1 - C#Joe WhiteView Answer on Stackoverflow
Solution 2 - C#n8wrlView Answer on Stackoverflow
Solution 3 - C#JoshJordanView Answer on Stackoverflow