これは私が尋ねたより詳細な例の質問です:私は、SQL ServerとC#を使用して、私のORMとしてDapperのを使用していBest Practice with classes and the databaseデータベーステーブルにクラス/クラスにデータベーステーブルを転送
。ここで
は、アプリケーションにComponentテーブルをロードする私のクラスである:
class DBComponent
{
public int ID { get; set; }
public string Component { get; set; }
public int Material_ID { get; set; }
public int Color_ID { get; set; }
}
は、それから私は、実際の値を持つことになります私の他のクラスが必要:
class Component
{
public int ID { get; set; }
public string Component { get; set; }
public string Material { get; set; }
public string Color { get; set; }
public Component(DBComponent C)
{
ID = C.ID;
Component = C.Component;
Material = //queries the Material Table passing in C.Material_ID and returning the Material Value
Color = //queries the Color Table passing in C.Color_ID and returning the Color Value
}
}
私がこれをやっているのは、コントロール(コンボボックス)を使ってWinFormアプリケーションの値を使うことができるからです。その他のニーズ。また、 "DBComponent"クラスには、 "Component"オブジェクトを取得し、新しいレコードとしてデータベースに返送される "DBComponent"オブジェクトを作成するメソッドがあります。
これはこの状況を処理する最善の方法ですか?それとも良い方法がありますか?私の他のポストでは、誰かが2つのクラスを作成する必要はありませんどこにそれを行うことができますdapperは、1クラスだけが必要だと述べた。それはどうですか?
暗黙の演算子を使用してこれを行うことができます。ここをクリックしてください:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit – Egbert
@Egbertここで暗黙的に使用する方法を見ることができますが、私はこの状況でそれを使用しますが、コンセプトが大好きです。プロジェクトの他の部分で暗黙的に使用します。ありがとうございます! – jediderek