开发者

Should I use Interfaces to enforce generics?

If I have the following generic interface:

    public interface IValidator<T>
    {
        Boolean IsValid(T en开发者_JAVA百科tity_);
    }

Can I use it in the following manner? Is this against Object Oriented Programming guidelines?

    public PathValidator : IValidator<String>
    {
    }


Is that use of Interfaces against any sort of best practice?

No, this is fine and common (assuming that your interface is not empty and has a method signature like bool IsValid(T entity)).

What makes you think it should be? If you let us know, we can elaborate.


Of cause not, just take a look at the generic collection interfaces like IList.


If this is actually the body of the interface, it's generally not a good idea to have an interface with no contract (that is, it doesn't require you to implement any methods). Usually this is suggestive of a problem with your design.


An empty interface (Marker interface) is usually bad practice because you should use an Attribute instead.

But if it's only empty because you omitted it to simplify the example then it looks perfectly fine to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜