特定のタイプの実装とインターフェイスをすべてバインドする必要がある単純な Web アプリを開発しています。私のインターフェースには、このような単一のプロパティがあります
public interface IContent {
string Id { get;set; }
}
このインターフェースを使用する一般的なクラスは次のようになります
public class Article : IContent {
public string Id { get;set; }
public string Heading { get;set; }
}
ここで明確にするために、article クラスは IContent を実装するさまざまなクラスの 1 つにすぎないため、これらの型を格納および更新する汎用的な方法が必要です。
だから私のコントローラーには、このような put メソッドがあります
public void Put(string id, [System.Web.Http.ModelBinding.ModelBinder(typeof(ContentModelBinder))] IContent value)
{
// Store the updated object in ravendb
}
および ContentBinder
public class ContentModelBinder : System.Web.Http.ModelBinding.IModelBinder {
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {
actionContext.ControllerContext.Request.Content.ReadAsAsync<Article>().ContinueWith(task =>
{
Article model = task.Result;
bindingContext.Model = model;
});
return true;
}
}
デフォルトのモデル バインダーを使用すると、Heading が正しくバインドされますが、Heading プロパティを取得していないように見えるため、上記のコードは機能しません。
したがって、BindModel メソッドでは、ID に基づいて ravendb から正しいオブジェクトをロードし、ある種のデフォルト モデル バインダーなどを使用して複雑なオブジェクトを更新する必要があると思います。ここで助けが必要です。