2009-06-09 11 views
0

私は次のようにコンストラクタに何かを持っている:C#コンストラクタでより簡潔な構文ですか?

using Microsoft.Data.Extensions; 

public class Complaint 
{ 
    public int Id {get; set;} 
    public int Transcript {get; set;} 
    //... etc. ... Lots more properties 

    public Complaint(int id) 
    { 
    var command = dataContext.CreateStoreCommand(
        "dbo.stp_Complaint_Get", 
        CommandType.StoredProcedure, 
        new SqlParameter("Id", id)); 

    var complaint = command.Materialize(x => 
         new Complaint 
         { 
          Id = x.Field<int>("Id"), 
          Transcript = x.Field<string>("Transcript"); 
          //... etc. ... Lots more fields from db 

         } 

    this.Id = complaint.Id; 
    this.Transcript = complaint.Transcript; 
    //... etc. ... Lots more properties to set 

    } 

} 

は私が1つのステップの代わりに、2で最後の部分を実行できるようになるC#での構文はありますか?つまり、概念的に次のようなものです。

this = command.Materialize(x => 
        new Complaint 
        { 
         Id = x.Field<int>("Id"), 
         Transcript = x.Field<string>("Transcript"); 
        } 

答えて

0

本質的にはありません。あなたは、クラス内に不満オブジェクトを格納し、コンストラクタからすべてを設定するのではなく、すべてのプロパティをポイントすることができます。

例えば

public class Complaint 
{ 
    private readonly {type of complaint} m_Complaint;   

    public int Id 
    { 
     get { return m_Complaint.Id; } 
    } 

    // ...etc 
} 

あなたはm_Complaintのセッター持っていない場合は、より複雑に得ることができる - NULL可能なバッキングフィールド維持し、チェックあなたが

3

まあm_Complaintプロパティにアクセスする前に、あなたができること代わりにコンストラクタの静的メソッドを使用します。

public static Complaint FromId(int id) 
{ 
    var command = dataContext.CreateStoreCommand(
       "dbo.stp_Complaint_Get", 
       CommandType.StoredProcedure, 
       new SqlParameter("Id", id)); 

    return command.Materialize(x => 
        new Complaint 
        { 
         Id = x.Field<int>("Id"), 
         Transcript = x.Field<string>("Transcript"); 
         //... etc. ... Lots more fields from db 

        }); 
} 
+0

である。私の現在のパターンから離れますが、私はそれを行うかもしれないと思います。 – Martin

0

を私はあなたがこのような何かを試すことが信じている:


var currentInstance = this; 
var complaint = command.Materialize(x => 
         new Complaint 
         { 
          Id = currentInstance.Id = x.Field("Id"), 
          Transcript = currentInstance.Transcript = x.Field("Transcript"); 
          //... etc. ... Lots more fields from db 

         }); 

私はクロージャーでこれをキャプチャすることはできないと思うので、そのcurrentInstanceトリックを使用すると助けになるはずですが、それは冗長になることもあります。 さらに、そのようなコードは、現在のソリューションと比較してわかりにくいものです。私はあなたのコードはそれほど素晴らしいと信じています。

+0

これは私の希望を持ちましたが、残念ながら「式ツリーに代入演算子が含まれていない可能性があります」というエラーが発生します。 – Martin

0

これらのプロパティをすべて2回設定しないようにしようとしているのは、構造を変更すると2箇所のプロパティを更新する必要があるからです。あるインスタンスから別のインスタンスにプロパティをコピーするためにリフレクションを使用することもできます。

var complaint = command.Materialize(x => 
         new Complaint 
         { 
          Id = x.Field<int>("Id"), 
          Transcript = x.Field<string>("Transcript"); 
          //... etc. ... Lots more fields from db 

         } 

foreach (var prop in typeof(Complaint).GetProperties()) 
    ... 
+0

はい、それは私の必要性です。複製によるメンテナンスの負担を避けるためです。 Reflectionは、問題のためにかなり重いツールのように思えます。構文トリックが存在することを望んでいました。とにかくありがとう。 – Martin

0

クレームオブジェクトをメンバー変数にして、get/setプロパティが基本となるクレームのプロパティにアクセスしますか?

関連する問題