2011-12-19 9 views
0

私は以下のようにクラスを持つC#のコンポーネントがあります。私はこのコンポーネントを参照していると私は、この同じクラスをインスタンス化する必要がある別の既存のC#アプリケーションでクラス間委任 - ベストプラクティス

namespace SharedComponent{ 
     class TestResult { 
      //several members 
     } 
    } 

をが、追加の識別子を持ちます以下のように。

namespace ClientApplication { 
     class TestResult 
     { 
      //exact same members as above including methods 
      //actually the shared component class was created by gleaming 
      //that from this application! 
      int PersonID; //additional identifier 
        //not suitable to have in the shared component 
     } 
    } 

クライアントアプリケーションには、追加の識別子に依存するいくつかの方法があります。ですからコピーコンストラクタをエミュレートしてこのオブジェクトを作成し、追加パラメータを入力することは私にとって非常に魅力的です。このようにして、既存の関数をそのまま使用することができます。

もう1つの方法は、残りの詳細をクライアント側の実装への参照として追加することです。

namespace ClientApplication { 
    class TestResult { 
     SharedComponent.TestResult trshared = new SharedComponent.TestResult() 
     //but this warrants I have my class methods to delegate 
     //to the sharedcomponent throughout ; example below 

     internal bool IsFollowUp(ClientApplication.TestResult prevTest) 
     { 
     //a similar method is being used 
       //where a function takes the class object as parameter 
       trshared.IsFollowUp(prevTest.trshared); 
     } 

     int PersonID; //additional identifier 

    } 
} 

どのオプションが優れていますか?これに関してベストプラクティスは何ですか?

環境:VS2008、C#の、WinXPの/ Win7の

+0

クライアントアプリケーションクラスはオリジナルから継承できますか? – sq33G

答えて

2

は、それはあなたのClientApplication.TestResultように私に聞こえるがSharedComponent.TestResult "あります"。 SharedComponent.TestResultがシールされていないと仮定すると、そのクラスから拡張することができます。この方法で、貼り付けコードをコピーする必要はありません。 SharedComponent.TestResultも変更できる場合は、メソッドを仮想として宣言し、ClientApplication.TestResultでその動作をオーバーライドします。

class TestResult : SharedComponent.TestResult 
{ 
    int PersonId { get; set; } 

    override bool IsFollowUp(ClientApplication.TestResult prevTest) 
    { 
      // Your own implementation or trivial (base.IsFollowUp(ClientApplication.TestResult.prevTest.trShared) 
    } 
} 

あなたは、あなたが派生クラスで「新しい」キーワードを使用することができ、SharedComponent.TestResultに仮想する方法を変更できない場合。