あなたが達成しようとしていることが可能かどうかはわかりません。少なくとも一緒ではないDelta<TEntity>.Patch(..)
Product
エンティティがあり、PATCH
アクションのどこかにあると仮定すると
[AcceptVerbs("PATCH")]
public void Patch(int productId, Delta<Product> product)
{
var productFromDb = // get product from db by productId
product.Patch(productFromDb);
// some other stuff
}
がproduct
作成されると、内部的にDelta<TEntityType>
コンストラクターを呼び出します。これは次のようになります (パラメーターのないコンストラクターもこれを呼び出し、typeof(TEntityType)
public Delta(Type entityType)
{
this.Initialize(entityType);
}
Initialize
メソッドは次のようになります
private void Initialize(Type entityType)
{
// some argument validation, emitted for the sake of brevity
this._entity = (Activator.CreateInstance(entityType) as TEntityType);
this._changedProperties = new HashSet<string>();
this._entityType = entityType;
this._propertiesThatExist = this.InitializePropertiesThatExist();
}
ここで興味深いのthis._propertiesThatExist
はDictionary<string, PropertyAccessor<TEntityType>>
、 Product タイプのプロパティを保持する です。PropertyAccessor<TEntityType>
プロパティを簡単に操作できる内部型です。
これを呼び出すとproduct.Patch(productFromDb)
、ボンネットの下で起こっていることです
// some argument checks
PropertyAccessor<TEntityType>[] array = (
from s in this.GetChangedPropertyNames()
select this._propertiesThatExist[s]).ToArray<PropertyAccessor<TEntityType>>();
PropertyAccessor<TEntityType>[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
PropertyAccessor<TEntityType> propertyAccessor = array2[i];
propertyAccessor.Copy(this._entity, original);
}
ご覧のとおり、変更されたプロパティを取得し、それらを繰り返し処理して、Patch アクションに渡されたインスタンスの値を、db から取得したインスタンスに設定します。したがって、渡す操作、追加するプロパティ名と値には何も反映されません。
propertyAccessor.Copy(this._entity, original)
メソッドの本体
public void Copy(TEntityType from, TEntityType to)
{
if (from == null)
{
throw Error.ArgumentNull("from");
}
if (to == null)
{
throw Error.ArgumentNull("to");
}
this.SetValue(to, this.GetValue(from));
}