3

在我的项目中有一个界面。在接口中有很多方法。我们公司的其他开发人员将接口继承到某些类中,并实现我需要的所有方法天气只实现某些方法。那一次我遇到了一些错误。“不实现接口成员”。我怎么解决这个问题?例如:-

public interface IComplianceRepository
    {
        IList<ComplianceModel> LoadComplianceModel(Guid userId);
        bool CreateCompliance(ComplianceModel complianceModel);
        IList<ComplianceType> LoadComplianceType();
        IList<ComplianceStatu> LoadComplianceStatus();
        IList<UserDetails> LoadUsersBySchoolId(int schoolId);
        Compliance GetComplianceByComplianceId(int complianceId);
        bool UpdateCompliance(ComplianceModel complianceModel);
        UserProfile GetUserProfileDetails(Guid userId);
        FinancialCompliance GetFinancialComplianceByComplianceId(int ComplianceId);
        void GetComplianceModelByComplianceId(ComplianceModel complianceModel, int complianceId);
    }

更多的开发人员使用了上述接口并实现了 all 方法。但我不想实现以下方法

    IList<ComplianceModel> LoadComplianceModel(Guid userId);
    bool CreateCompliance(ComplianceModel complianceModel);

我怎么解决这个问题?

4

6 回答 6

6

你不能。存在的唯一原因Interface是整个契约必须在实现它的类中实现。

于 2013-04-17T13:32:28.183 回答
6

如果您不能或不想更改接口,则应实现这些方法并抛出NotSupportedException. 我建议为此使用显式接口继承,这样虚拟方法就不会出现在类中。

void IList<ComplianceModel> IComplianceRepository.LoadComplianceModel(Guid userId)
{
    throw new NotSupportedException();
}

作为 BCL 中的示例,您可以查看ReadOnlyCollection.ICollection.Add Method。这是一个显式接口实现,它会抛出NotSupportedException. 这也是糟糕设计的一个例子,证明了IReadOnlyList<T>.net 4.0 中缺少接口。


真正的解决方案是重构您的代码。具有您完全实现的较小接口。看一下接口隔离原理

接口隔离原则 (ISP) 指出不应强迫任何客户端依赖它不使用的方法。ISP 将非常大的接口拆分为更小、更具体的接口,以便客户端只需了解他们感兴趣的方法。

于 2013-04-17T13:33:46.723 回答
2

Create two interface, parent and child. Parent will have exactly what you want, and child will have others.

public interface Parent {
    // parent methods here
}

public interface Child : Parent{
    // child methods here
}
于 2013-04-17T13:35:42.967 回答
1

如果一个类实现了一个接口,它必须实现该接口上的所有功能。接口是一种功能契约,因此任何声称满足该契约的东西都必须实际这样做。现在,您不必有意义地实施该合同中的所有内容。例如,对于您知道不会使用的方法,执行此操作的标准方法是:

public void SomeMethodIKnowIWontUse()
{
    throw new NotSupportedException();
}

因此,如果实际使用过该方法,那么它将引发异常。(当您认为它不会被使用时,这表明您错了,您应该实施它。)

请记住,这会很快导致“代码异味”。如果您有很多不需要实现的对象成员,那么显然设计是错误的......

这里的另一种可能性是界面设计不正确。也许它试图为太多人提供太多东西?这可能违反了单一职责原则。以这个接口为例:

public interface CatchAll
{
    void FunctionForOneResponsibility();
    void FunctionForCompletelyDifferentResponsibility();
}

使用非常做作的名称,很明显这个接口有太多的责任。应该是这样的:

public interface OneResponsibilitySatisfier
{
    void FunctionForThisResponsibility();
}

public interface AnotherResponsibilitySatisfier
{
    void FunctionForThisOtherResponsibility();
}

没有规则说你需要有几个接口,或者一个接口需要有很多成员。每个人都应该为其职责提供一个有意义的功能合同,仅此而已。如果碰巧你有一个类可以同时满足这两个职责,它可以实现两个接口:

public class CrossCuttingObject : OneResponsibilitySatisfier, AnotherResponsibilitySatisfier
{
    public void FunctionForThisResponsibility() { }
    public void FunctionForThisOtherResponsibility() { }
}
于 2013-04-17T13:38:45.737 回答
0

Interfaces exist as a contract between implementers and users of the interface. The users/consumers require that all the methods be implemented. First, ask yourself if your implementation without these methods is still useful. If so, ask yourself if you need to inherit from this interface at all.

If after this reflection, you still have valid reasons to implement this interface without implementing all the methods, you can create stub methods:

IList<ComplianceModel> LoadComplianceModel(Guid userId)
{
   throw NotSupportedException();
}

or, more dangerously, but possibly less disruptively:

IList<ComplianceModel> LoadComplianceModel(Guid userId)
{
   return null;
}
于 2013-04-17T13:36:36.553 回答
0

这是接口隔离的理想方案。请在此处参考此设计原则 (oodesign.com/interface-segregation-principle.html) 您需要隔离合约,不要将所有鸡蛋放在一个篮子里。

于 2013-04-17T14:05:33.430 回答