2013-05-24 4 views
0

.NET 4.5에서 Neo4J 용 데이터 액세스 라이브러리를 모의하려고합니다. 나는 각 명령을 데이터베이스에 정의하기 위해 인터페이스를 사용하고있다.C# 부모 인터페이스의 속성 상속

을 감안할 때 :

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 대신 IBaseRequest의 ICreateNodeWProperties. 나는 그것을 다르게하는 것에 개방적이지만 모듈화되고 검증 가능한 접근법처럼 보인다.

나는 그것이 의미가 있기를 바랍니다!

+0

나는 당신이 원하는 것을 이해하지 못합니다. 테스트 클래스가 모든 인터페이스를 구현합니까 ??? 그렇다면 호출 할 인터페이스에 따라 속성이 다른 결과를 제공하기를 원하십니까? –

+0

또는 이러한 속성을 여러 번 구현하지 않으려 고합니다. ??? 그렇다면 명시 적으로 속성을 구현하지 마십시오. –

답변

0

인터페이스로하려는 것을 수행 할 수 없습니다. 이미 보았 듯이, 클래스가 공통 인터페이스를 상속하는 둘 이상의 인터페이스를 구현할 때 공통 인터페이스가 암시 적으로 추가되지만 단 한 번만 - 파생 인터페이스마다 기본 인터페이스의 변형을 얻지 못합니다.

당신은 시도 할 수합니다 (Command pattern 기준) 다음 :

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 : 별도의 속성으로 각 명령을 할 수 TestImplClass에서

interface ICommand { 
    HttpMethod HttpMethod { get; }  
    string QueryUriSegment { get; } 

    void Execute(); 
} 

class abstract BaseCommand : ICommand { 
    public abstract HttMethod { get; } 
    public abstract string QueryUriSegment { get; } 
} 

class CreateNodeCommand : ICommand { 
    public override HttpMethod HttpMethod { get { /* return HttpMethod for "create node" */ } } 
    public override string QueryUriSegment { get { /* return QueryUriString for "create node" */ } } 

    public void Execute() { /* Create node... */ } 
} 

class CreateNodeWithPropertiesCommand : ICommand { 
    public override HttpMethod HttpMethod { get { /* return HttpMethod for "create node with properties" */} } 
    public override string QueryUriSegment { get { /* return QueryUriString for "create node with properties" */ } } 

    public void Execute() { /* Create node with properties ... */ } 
} 

QueryUriSegment 속성을 사용하면 각 명령을 모의 테스트하고 테스트 할 때 TestImplClass의 생성자로 전달할 수 있습니다.

어떤 명령을 사용할 때 Execute()을 적절한 속성 (예 : dataAccess.CreateNode.Execute();)으로 부를 수 있습니다.

+0

이 접근 방식이 내 상황에서 어떻게 작동하는지 확인할 수 있습니다. 내 코드를 발견하면 "노드"명령을 처리합니다. 그러나 내가이 방법으로 설계하려고하는 이유는 동일한 패턴을 사용하는 다른 명령의 3 가지 다른 계층 구조를 수용하기 위해서입니다. 어디에서이 새로운 디자인을 구현하기 위해이 디자인을 사용하지 않을 것을 제안하겠습니까? – Robnauticus

+0

명령의 계층 구조가 무엇을 의미하는지 자세히 설명해 주시겠습니까? – Talon

+0

예제와 동일한 부모 IBaseRequest를 상속하는 다른 노드와 관련없는 인터페이스가 있습니다. 예를 들어 IBaseRelationshipActions에 결합 할 수있는 다른 인터페이스가 있습니다. – Robnauticus

1

추상 BaseRequest 클래스를 정의 할 수 있습니다. 그 중 하나는 IBaseRequest 속성을 구현합니다.

따라서 모든 하위 인터페이스를 구현하는 클래스는 BaseRequest을 상속하며 속성이 자동으로 구현됩니다.

1

내가 당신이 찾고있는 것을 정확히 오해 한 경우 용서하십시오.

당신은 기본 클래스 수 :

public abstract class BaseNodeActions : IBaseNodeActions 
{ 
    public abstract void CreateNode(); 
    public abstract HttpMethod HttpMethod {get;set;} 
    .... 
} 

그런 다음 당신의 TestImplClass 상속하기를 BaseNodeActions

원하는 경우는 (은) 추상 사용하여 무시하고 다음을 수행 할 수 :

public class BaseNodeActions : IBaseNodeActions 
{ 
    public virtual void CreateNode() { throw new NotImplementedException(); } 
    public virtual HttpMethod HttpMethod {get { throw new NotImplementedException(); } 
    .... 
} 

속성을 가상으로 설정하면 실제로 필요한 속성 만 재정의하면됩니다. 예외를 throw하는 것을 제거하고 디자인에서 허용되는 경우 예외를 반환 할 수 있습니다.