Linq2Sqlクラスのプロパティに属性を追加したいと思います。このような列は、UIで閲覧可能であるか、UIおよびこれまでのところ読み取り専用です。
テンプレートの使用を考えましたが、使用方法を知っている人はいますか?または何か違う?
一般的に言って、コードで生成されるクラスでこの問題に対処するために何をしますか?
Linq2Sqlクラスのプロパティに属性を追加したいと思います。このような列は、UIで閲覧可能であるか、UIおよびこれまでのところ読み取り専用です。
テンプレートの使用を考えましたが、使用方法を知っている人はいますか?または何か違う?
一般的に言って、コードで生成されるクラスでこの問題に対処するために何をしますか?
System.ComponentModel.DataAnnotations の新しいメタデータ機能を利用して、メタデータを既存のドメイン モデルから分離することができます。
例えば:
[MetadataType (typeof (BookingMetadata))]
public partial class Booking
{
// This is your custom partial class
}
public class BookingMetadata
{
[Required] [StringLength(15)]
public object ClientName { get; set; }
[Range(1, 20)]
public object NumberOfGuests { get; set; }
[Required] [DataType(DataType.Date)]
public object ArrivalDate { get; set; }
}
リクエストに応じて、CustomTypeDescriptor
実行時に を使用して属性を編集する方法を次に示します。ここの例は win-forms ですが、それを WPF に交換して動作するかどうかを確認するのは非常に簡単なはずです...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
// example POCO
class Foo {
static Foo()
{ // initializes the custom provider (the attribute-based approach doesn't allow
// access to the original provider)
TypeDescriptionProvider basic = TypeDescriptor.GetProvider(typeof(Foo));
FooTypeDescriptionProvider custom = new FooTypeDescriptionProvider(basic);
TypeDescriptor.AddProvider(custom, typeof(Foo));
}
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
// example form
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run( new Form {
Controls = {
new DataGridView {
Dock = DockStyle.Fill,
DataSource = new BindingList<Foo> {
new Foo { Name = "Fred", DateOfBirth = DateTime.Today.AddYears(-20) }
}
}
}
});
}
}
class FooTypeDescriptionProvider : TypeDescriptionProvider
{
ICustomTypeDescriptor descriptor;
public FooTypeDescriptionProvider(TypeDescriptionProvider parent) : base(parent) { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{ // swap regular descriptor for bespoke (Foo) descriptor
if (descriptor == null)
{
ICustomTypeDescriptor desc = base.GetTypeDescriptor(typeof(Foo), null);
descriptor = new FooTypeDescriptor(desc);
}
return descriptor;
}
}
class FooTypeDescriptor : CustomTypeDescriptor
{
internal FooTypeDescriptor(ICustomTypeDescriptor parent) : base(parent) { }
public override PropertyDescriptorCollection GetProperties()
{ // wrap the properties
return Wrap(base.GetProperties());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{ // wrap the properties
return Wrap(base.GetProperties(attributes));
}
static PropertyDescriptorCollection Wrap(PropertyDescriptorCollection properties)
{
// here's where we have an opportunity to swap/add/remove properties
// at runtime; we'll swap them for pass-thru properties with
// edited atttibutes
List<PropertyDescriptor> list = new List<PropertyDescriptor>(properties.Count);
foreach (PropertyDescriptor prop in properties)
{
// add custom attributes here...
string displayName = prop.DisplayName;
if (string.IsNullOrEmpty(displayName)) displayName = prop.Name;
list.Add(new ChainedPropertyDescriptor(prop, new DisplayNameAttribute("Foo:" + displayName)));
}
return new PropertyDescriptorCollection(list.ToArray(), true);
}
}
class ChainedPropertyDescriptor : PropertyDescriptor
{
// this passes all requests through to the underlying (parent)
// descriptor, but has custom attributes etc;
// we could also override properties here...
private readonly PropertyDescriptor parent;
public ChainedPropertyDescriptor(PropertyDescriptor parent, params Attribute[] attributes)
: base(parent, attributes)
{
this.parent = parent;
}
public override bool ShouldSerializeValue(object component) { return parent.ShouldSerializeValue(component); }
public override void SetValue(object component, object value) { parent.SetValue(component, value); }
public override object GetValue(object component) { return parent.GetValue(component); }
public override void ResetValue(object component) { parent.ResetValue(component); }
public override Type PropertyType {get { return parent.PropertyType; } }
public override bool IsReadOnly { get { return parent.IsReadOnly; } }
public override bool CanResetValue(object component) {return parent.CanResetValue(component);}
public override Type ComponentType { get { return parent.ComponentType; } }
public override void AddValueChanged(object component, EventHandler handler) {parent.AddValueChanged(component, handler); }
public override void RemoveValueChanged(object component, EventHandler handler) { parent.RemoveValueChanged(component, handler); }
public override bool SupportsChangeEvents { get { return parent.SupportsChangeEvents; } }
}
LinqToSqlにDamienGuardのT4テンプレートを使用することを検討することをお勧めします。彼のテンプレートを変更すると、おそらくあなたが求める結果が得られるでしょう。
お役に立てれば!
これは、コード生成の一般的な問題です。追加の部分クラスを介してメンバーとクラスレベルの属性を追加することはできますが、生成されたメンバーに属性を追加することはできません。これを補うために、一部の属性ベースのメカニズムでは、クラスで属性を指定できます(メンバーに名前を付ける)が、引用する属性は指定できません。
1つのハードコアオプションは、プロパティのTypeDescriptionProvider
カスタム定義を提供するを作成することです。これにより、、などのPropertyDescriptor
UIバインディングツールで使用されるメタデータを完全に制御できます。PropertyGrid
DataGridView
ただし、手動で設定できる場合は、いくつかのUIプロパティを設定するには、これは多分手間がかかりすぎます。しかし、そのオプションを追求することに興味がある場合は、私に知らせてください-それは私にはなじみのある領域ですが、それを望まない場合は例を書くにはコードが多すぎます。
注:を使用している場合PropertyGrid
、プロパティを手動で設定することはできませんが、を書くことはできます。これは、完全な;TypeConverter
よりも少し手間がかかりません。TypeDescriptionProvider
から継承しExpandableObjectConverter
てオーバーライドするだけGetProperties()
です。あなたはまだシムが必要PropertyDescriptor
なので、それでも些細なことではありません...
部分クラスを使用して、エンティティに、エンティティと同じプロパティを宣言するインターフェイスを実装させてから、インターフェイスに属性を配置することができます。
このようにして、インターフェースタイプを使用して、プロパティから属性を取得できます。
この方法で属性を使用できるかどうかはわかりませんが、そのようなことを試すことができます。
例:
public interface IConcept {
long Code { get; set; }
[Unique]
string Name { get; set; }
bool IsDefault { get; set; }
}
public partial class Concept : IConcept { }
[Table(Name="dbo.Concepts")]
public partial class Concept
{
//...
}
デフォルトのMSLinqToSQLGeneratorの代わりに、別のコードジェネレーターを記述/使用することもできます。
開始する1つのオプションは、これです。
必要に応じて、自分で作りました。DBMLデザイナまたはxmlファイルを使用して各プロパティに配置する必要がある属性を示す方法があるかどうかはわかりませんが、この代替手段を使用して問題を解決する方法を見つけることができるかもしれません。