私が Windows フォーム アプリケーションで使用しているデータベースには、主キーの int ID フィールド、varchar の説明、および ID と説明が使用される別のテーブルとの 1 対 1 の関係で構成される多くのテーブルがあります。
Entity Framework Code First を使用してデータベースにマップされた C# POCO の一例は次のとおりです。
public class OtherClass
{
public int Id { get; set; }
public string Description { get; set; }
}
public class Foo
{
public int Id { get; set; }
public string Description { get; set; }
public OtherClass OtherClassProperty { get; set; }
}
「Foo」などのクラスはすべて同じ構造を共有しているため、このようなすべてのクラスで機能する 1 つの基本フォームを作成しています。
私の問題は、これらすべてのクラスの Id と Description に異なるプロパティ名があることです (たとえば、Name と呼ばれることもあります)。彼らが参照する他のクラスは常に異なることは言うまでもありません。
私が最初に考えたのは、Id、Description、KeyId、KeyDescription という固定名を持つプロパティの実装を必要とするインターフェイスを作成することでした。これらのプロパティは各クラスに実装されますが、「実際のプロパティ」のみを指します。このような:
public class Foo : MyInterface
{
public int Id { get; set; }
public string Description { get; set; }
public OtherClass OtherClassProperty { get; set; }
//Interface implementation
public int CommonId { get { return this.Id; } set { this.Id = value; } }
public int CommonDescription { get { return this.Description; } set { this.Description = value; } }
public int CommonKeyId { get { return this.OtherClassProperty.Id; } set { this.OtherClassProperty.Id = value; } }
public int CommonKeyDescription { get { return this.OtherClassProperty.Description; } set { this.OtherClassProperty.Description = value; } }
}
これにはもっと良い解決策があるかもしれないという印象があります。また、プロパティ属性を実装し、リフレクションを使用して実行時にチェックし、プロパティを取得することも想像できます。
この状況について誰か提案がありますか?
前もって感謝します。