0

次のようなクラスを持つC#コンポーネントがあります。

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

別の既存のC#アプリケーションでは、このコンポーネントを参照しています。この同じクラスをインスタンス化する必要がありますが、以下のように追加の識別子を使用します。

    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
      }
  }

クライアントアプリケーションには、追加の識別子に依存するいくつかのメソッドがあります。そのため、コピーコンストラクターをエミュレートしてこのオブジェクトを作成し、追加のパラメーターを入力するのは非常に魅力的です。このようにして、クラスへの変更を最小限に抑えながら、既存の関数をそのまま使用できます。

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

 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

4

1 に答える 1

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 でメソッドを仮想に変更できない場合は、派生クラスでキーワード「new」を使用できます。

于 2011-12-19T23:35:12.993 に答える