2011-08-13 40 views
1

我已經創建瞭如下所示的接口。 DTO對象是一個具有3個參數的複雜值對象。擴展必須在接口中使用的類

public interface IOperation 
{ 
    DTO Operate(DTO ArchiveAndPurgeDTO); 
} 

我需要使這個接口能夠從原始Value對象繼承並且在需要時擴展它的人。

我的假設是,他們可以簡單地繼承DTO對象,添加(例如)另一個屬性,並使用它在相同的類中實現此接口。

當我嘗試使用擴展值對象時,Visual Studio抱怨說我不再提示接口。

我該如何實現這一功能。

在此先感謝您的任何建議和/或建議。

Gineer

編輯: DTO代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Company.ArchiveAndPurge 
{ 
    public class DTO 
    { 
     public DTO(String FriendlyID) 
     { 
      friendlyId = FriendlyID; 
     } 

     private String friendlyId = String.Empty; 

     public String FriendlyId 
     { 
      get { return friendlyId; } 
      set { friendlyId = value; } 
     } 

     private String internalId = String.Empty; 

     public String InternalyId 
     { 
      get { return internalId; } 
      set { internalId = value; } 
     } 

     private Boolean archivedSuccessfully = false; 

     public Boolean ArchivedSuccessfully 
     { 
      get { return archivedSuccessfully; } 
      set { archivedSuccessfully = value; } 
     } 
    } 
} 

擴展DTO:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Company.MSO.ArchiveAndPurge 
{ 
    public class DTO: Company.ArchiveAndPurge.DTO 
    { 
     private Boolean requiresArchiving = true; 

     public Boolean RequiresArchiving 
     { 
      get { return requiresArchiving; } 
      set { requiresArchiving = value; } 
     } 
    } 
} 

接口實現,其中VS訴說:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Company.ArchiveAndPurge.Contracts; 
using Company.ArchiveAndPurge; 

namespace Company.MSO.ArchiveAndPurge 
{ 
    public class ResolveFriendlyId: IOperation 
    { 
     #region IOperation Members 

     public DTO Operate(DTO ArchiveAndPurgeDTO) 
     { 
      ArchiveAndPurgeDTO.InternalyId = ArchiveAndPurgeDTO.FriendlyId; 
      return ArchiveAndPurgeDTO; 
     } 

     #endregion 
    } 
} 
+0

顯示DTO類代碼和代碼在哪裏VS抱怨錯誤 – sll

回答

2

據我瞭解,你可能有這樣的事情:

public class ExtendedOperation : IOperation 
{ 
    public ExtendedDTO Operate(ExtendedDTO dto) 
    { 
     ... 
    } 
} 

這並不在兩個方面的工作:實現一個接口方法時

  • 你不能改變的返回類型
  • 實現接口

特別是當你不能改變的參數列表,你就不會被執行IOperation在某種程度上這將是與這樣的代碼兼容:

IOperation operation = new ExtendedOperation(); 
operation.Operate(new DTO()); 

我懷疑你可能想使界面通用:

public interface IOperation<T> where T : DTO 
{ 
    T Operate(T dto); 
} 
+0

一如既往,Jon Skeet更快。 ;) – magnattic

1

使用泛型:

public interface IOperation<T> where T : DTO 
{ 
    T Operate(T ArchiveAndPurgeDTO); 
}