【发布时间】:2013-05-24 19:23:47
【问题描述】:
我正在尝试在 .NET 4.5 中模拟 Neo4J 的数据访问库。我正在使用接口来定义数据库的每个命令。
给定:
public interface IBaseRequest
{
HttpMethod HttpMethod { get; }
string QueryUriSegment { get; }
}
public interface ICreateNode : IBaseRequest
{
void CreateNode();
}
public interface IBaseNodeActions : ICreateNode,ICreateNodeWProperties //...And many others, all inherit from IBaseRequest
{
}
internal class TestImplClass : IBaseNodeActions {
public TestImplClass() {
}
void ICreateNode.CreateNode() {
throw new NotImplementedException();
}
//Only one copy of the HttpMethod and QueryUriSegment are able to be implemented
DataCommands.HttpHelper.HttpMethod IBaseRequest.HttpMethod {
get {
throw new NotImplementedException();
}
}
string IBaseRequest.QueryUriSegment {
get {
throw new NotImplementedException();
}
}
问题在于从 IBaseRequest 继承的每个接口,我需要为其父级拥有的每个属性(HttpMethod、QueryUriSegment)实现一个属性。
这可能吗?我知道使用显式实现是必要的,但不确定如何将它们推送到实现类中。
这是我希望在我的实现类中看到的内容:
public class TestImplClass : IBaseNodeActions{
public TestImplClass() {
}
void ICreateNode.CreateNode() {
throw new NotImplementedException();
}
HttpMethod ICreateNode.HttpMethod {
get {
throw new NotImplementedException();
}
}
string ICreateNode.QueryUriSegment {
get {
throw new NotImplementedException();
}
}
HttpMethod ICreateNodeWProperties.HttpMethod {
get {
throw new NotImplementedException();
}
}
string ICreateNodeWProperties.QueryUriSegment {
get {
throw new NotImplementedException();
}
}
}
注意 ICreateNode 和 ICreateNodeWProperties 而不是 IBaseRequest。我愿意采取不同的做法,但这似乎是一种模块化、可测试的方法。
我希望这是有道理的!
【问题讨论】:
-
我不明白你想要什么。您的测试类是否实现了所有接口???如果是这样,您希望属性根据您调用的接口给出不同的结果吗?
-
或者想要避免多次实现这些属性???如果是这样,请不要显式地实现这些属性。
标签: c# .net inheritance interface