我正在開發一個簡單的Web應用程序,我需要將所有類型的實現和特定類型的接口綁定在一起。我的接口有這樣使用Web API和RavenDB進行繼承的自定義模型聯編程序
public interface IContent {
string Id { get;set; }
}
使用這個接口應該是這樣的
public class Article : IContent {
public string Id { get;set; }
public string Heading { get;set; }
}
是乾淨這裏的文章類只是實現IContent這麼爲此許多不同類別的一個
一類常見單一屬性我需要一種通用的方式來存儲和更新這些類型。
所以在我的控制器我有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;
}
}
上面的代碼不起作用,因爲它似乎並沒有得到鐓性能的保持即使如果我使用默認模型聯編程序它正確地綁定標題。
因此,在BindModel方法中,我想我需要從ravendb基於Id加載正確的對象,然後使用某種默認模型綁定器更新複雜對象,或者等等?這是我需要幫助的地方。
@ matt-johnson其實我不知道如何更新模型,但我已經用一些代碼更新了我的問題,但這只是爲了試驗和錯誤。 – Marcus 2013-03-16 16:17:33
@MattJohnson也許有可能使用HttpContent.ReadAsAsync或其他的反序列化json到一個特定的類型? –
Marcus
2013-03-16 16:58:37
@ matt-johnson我用ReadAsAsync更新了我的代碼,但我無法弄清楚爲什麼這樣不起作用? – Marcus 2013-03-16 19:06:05