私は、特定のデータベースフィールドに関する情報を含む属性で再生しています。私は既にFieldAttributeクラスを作成しました。名前、型などのデフォルトの情報を持っています。リフレクト、プロパティ、および属性 - ORMアーキテクチャの学習
私の質問は、この情報を取得してプロパティに組み込む単一のメソッドを作成できますか? 「記入」だけのフィールドは、データベース/ _underlyingObjectからデータをフェッチする属性値を使用していることを、一般的なGetMethodを構築することです私はここで何をしたいのか
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
public class FieldAttribute:Attribute
{
protected string _FieldName;
protected esriFieldType _FieldType;
protected bool _NotNullable;
protected bool _IsPrimary;
protected bool _IsForeign;
protected int _Index;
public virtual string FieldName
{
get { return this._FieldName; }
set { this._FieldName = value; }
}
public virtual esriFieldType FieldType
{
get { return this._FieldType; }
set { this._FieldType = value; }
}
public virtual bool NotNullable
{
get { return this._NotNullable; }
set { this._NotNullable = value; }
}
public virtual int Index
{
get { return this._Index; }
set { this._Index = value; }
}
public FieldAttribute(string fieldName, esriFieldType fieldType,int position)
{
this.FieldName = fieldName;
this.FieldType = fieldType;
_IsPrimary = false;
_IsForeign = false;
Index = position;
}
}
// my main abstract class
public abstract class ObjectWrapper:IObjectWrapper
{
protected int _ObjectId;
protected IObject _UnderlyingObject;
public virtual IObject UnderlyingObject
{
get { return this._UnderlyingObject; }
}
public virtual int ObjectId
{
get { return this._ObjectId; }
}
public virtual void Store()
{
try
{
_UnderlyingObject.Store();
}
catch (COMException comEx)
{
// log com exception
}
catch (Exception ex)
{
// log
}
}
// this method retrieves field information
private FieldAttribute GetFieldAttribute(string propertyName)
{
FieldAttribute propertyAttribute = null;
try
{
PropertyInfo propInfo = this.GetType().GetProperty(propertyName);
object[] attributes = propInfo.GetCustomAttributes(typeof(FieldAttribute), true);
if (attributes.Length == 1)
propertyAttribute = (FieldAttribute)attributes[0];
}
catch (AmbiguousMatchException ambEx)
{
// log
}
catch (ArgumentException argEx)
{
// log
}
return propertyAttribute;
}
}
// my concrete class
public class Foo:ObjectWrapper
{
[Field("FOO_NAME",esriFieldType.esriFieldTypeString,1);
public string FooName { get; set; }
[Field("FOO_AGE",esriFieldType.esriFieldTypeInteger,2);
public int FooAge { get; set; }
}
と:私は、コードとのより良い通信することができると思いますgetterとsetter for me.EG:
私がFoo.FooNameを呼び出すと、getterはGetFieldAttributeメソッドにゲッター名を渡して属性をチェックします。
どうすればこの問題を解決できますか?
小さなフレームワークがある場合、これはデータのための簡単な「ESRIデータベースプロバイダ」になります。
私はこれを正しく説明できないことを本当に残念です。
ありがとうございました。
G.
これはあなた自身の好奇心のためであり、本番システムでは使用しないことを望みます。 –
@qstarin:プロダクションで使用しない理由は何ですか?アイデアは悪いですか?私はこれがすぐに生産に使われると言っているわけではありませんが、すべてのプロジェクトはどこかで始まらなければなりません。ご協力ありがとうございました。 –
あなた自身のものを開発するよりも、既存のORMソリューションを習得して使用することは、はるかに生産的になります。それがあなた自身の学習のためのものなら、価値があります。これが仕事に払われれば、それは怠慢で無責任なようです(それは少し強いですが、それほど遠くはありません)。独自のORMを書くアイデアは反パターンです。 –