C#从父接口继承属性
我正试图在.NET4.5中为Neo4J模拟一个数据访问库。我使用接口来定义数据库的每个命令 鉴于:C#从父接口继承属性,c#,.net,inheritance,interface,C#,.net,Inheritance,Interface,我正试图在.NET4.5中为Neo4J模拟一个数据访问库。我使用接口来定义数据库的每个命令 鉴于: public interface IBaseRequest { HttpMethod HttpMethod { get; } string QueryUriSegment { get; } } public interface ICreateNode : IBaseRequest { vo
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和ICreateNodeWProperty,而不是IBaseRequest。我愿意用不同的方式来做,但这似乎是一种模块化的、可测试的方法
我希望这是有道理的 您可以定义一个抽象的BaseRequest类。 实现
IBaseRequest
属性的那个
因此,实现所有子接口的类将继承此
BaseRequest
,并自动实现属性。如果我完全误解了您要查找的内容,请原谅
您可以有一个基类:
public abstract class BaseNodeActions : IBaseNodeActions
{
public abstract void CreateNode();
public abstract HttpMethod HttpMethod {get;set;}
....
}
然后只需让您的TestImplClass
inheritBaseNodeActions
如果需要,还可以忽略使用抽象,并执行以下操作:
public class BaseNodeActions : IBaseNodeActions
{
public virtual void CreateNode() { throw new NotImplementedException(); }
public virtual HttpMethod HttpMethod {get { throw new NotImplementedException(); }
....
}
如果将属性设置为虚拟,则只需覆盖实际需要的属性。如果您的设计允许,您可能希望删除抛出异常并使其返回默认值。不可能对接口执行您想要的操作。正如您所看到的,当一个类实现了两个或多个接口,而这些接口本身继承了一个公共接口时,公共接口被隐式添加,但只添加一次—您不会为每个派生接口获得一个基接口的变体 您可以尝试以下操作(基于以下选项): 在TestImplClass中,可以将每个命令作为单独的属性:
public TestImplClass {
public ICommand CreateNode { get; private set; }
public ICommand CreateNodeWithProperties { get; private set; }
public TestImplClass(ICommand createNode, ICommand createNodeWithProperties) {
this.CreateNode = createNode;
this.CreateNodeWithProperties = createNodeWithProperties;
}
}
然后,每个命令都有自己的HttpMethod和QueryUriSegment属性,您可以模拟每个命令,并在测试时将它们传递到TestImplClass的构造函数中
当涉及到使用任何命令时,您可以在适当的属性上调用
Execute()
,例如:dataAccess.CreateNode.Execute()代码>我不明白你想要什么。您的测试类是否实现了所有接口???如果是这样,您希望属性根据您当时调用的接口给出不同的结果?或者希望避免多次实现这些属性???如果是这样,就不要显式地实现属性。我可以看到这种方法在我的情况下是如何工作的。如果您注意到我的代码,它正在处理“Node”命令。然而,我尝试以这种方式设计它的原因是为了适应使用相同模式的其他命令的至少3个其他层次结构。您建议我将此设计分支到哪里来构建这些新的实现?您能详细说明一下您所说的命令层次结构是什么意思吗?还有其他与节点无关的接口继承了我示例中的同一个父iBasereRequest。然后会有另一个接口,将它们组合成iBaserRelationshipActions作为示例。您可以按照最初的意图,将命令分组到接口中。您可能会发现,如果命令太多,构造函数将无法维护,或者至少难以使用。另一种方法是创建一个数据访问类,其中的操作作为方法实现,例如:CreateNode(Node-Node)
CreateNode(Node-Node,object-parameters)
。如果做得好,这不需要影响可测试性。嗨,塔隆,我正在使用命令模式重新处理我的UML图和你的建议。问题:在上面的代码中,您是否打算使用CreateNodeCommand:BaseCommand而不是CreateNodeCommand:ICommand?看来这就是你想要的。谢谢,顺便说一句!
public TestImplClass {
public ICommand CreateNode { get; private set; }
public ICommand CreateNodeWithProperties { get; private set; }
public TestImplClass(ICommand createNode, ICommand createNodeWithProperties) {
this.CreateNode = createNode;
this.CreateNodeWithProperties = createNodeWithProperties;
}
}