2010-10-30 84 views
2

爲了簡便起見,可以說我有以下的抽象基控制器類:MVC抽象基控制器覆蓋參數類型modelbinding

public abstract class RESTController : Controller 
{  
    public abstract JsonResult List(); 
    public abstract JsonResult Get(Guid id); 
    public abstract JsonResult Create(object obj); 
    public abstract JsonResult Update(object obj); 
    public abstract JsonResult Delete(Guid Id); 
} 

對於創建&更新方法,我不僅要覆蓋的方法,也是參數的類型。

通常我會使用泛型像這樣:

public abstract JsonResult Create<T>(T obj); 

然而,這是一個MVC的行動,並沒有辦法指定類型參數。

我有什麼選擇?如果我將它作爲(object obj)將MVC模型聯編程序正常工作?

var model = obj as MyViewModel; 

這在任何情況下都不是很乾淨。任何幫助,將不勝感激。

回答

6

如何在電線之間的東西:

public abstract class RESTController<T> : Controller 
{  
    public abstract JsonResult List(); 
    public abstract JsonResult Get(Guid id); 
    public abstract JsonResult Create(T obj); 
    public abstract JsonResult Update(T obj); 
    public abstract JsonResult Delete(Guid Id); 
} 

,並覆蓋時:

public FooController: RESTController<Foo> 
{ 
    ... 
    public override JsonResult Create(Foo obj) 
    { 
     throw new NotImplementedException(); 
    } 
    ... 
} 
+0

哇,我覺得很蠢。謝謝... – 2010-10-30 21:29:49

0

如果你想要一些更充實,你可以嘗試使用一些大意如下的行。我正在使用洋蔥架構和存儲庫模式,IoC和DI。 IEntity只是提供對實體的Id字段的訪問(我假設實體框架代碼在此處爲Entity.Id作爲每個實體的主鍵,而EntityId將在另一個表上指定外鍵)。

這些操作是虛擬的,以允許派生類在必要時覆蓋它們,並將存儲庫設置爲protected,以便派生類也可以從實體的存儲庫中提取。這適用於一個基本的CRUD存儲庫,但可以用一個聚合來代替,以實現更多功能。

using System; 
using System.Web.Mvc; 
using MySolution.Core.Interfaces.EntityFramework; 
using MySolution.Core.Interfaces.Repositories; 

namespace MySolution.Ux.Web.Site.Primitives 
{ 
    /// <summary> 
    ///  Provides mechanisms for performing CRUD operations on entities within a RESTful environment. 
    /// </summary> 
    /// <typeparam name="TEntity">The type of the entity.</typeparam> 
    public abstract class CrudController<TEntity> : Controller 
     where TEntity : class, IEntity, new() 
    { 
     /// <summary> 
     ///  The repository to use for CRUD operations on the entity. Derived classes 
     ///  also have access to this repository so that the virtual actions can be 
     ///  overridden with custom implementations. 
     /// </summary> 
     protected readonly IRepository<TEntity> Repository; 

     /// <summary> 
     ///  Initialises a new instance of the <see cref="CrudController{TEntity}"/> class. 
     /// </summary> 
     /// <param name="repository">The repository.</param> 
     protected CrudController(IRepository<TEntity> repository) 
     { 
      // Instantiate the controller's repository. 
      Repository = repository; 
     } 

     /// <summary> 
     ///  Lists this specified entities within the data store. 
     /// </summary> 
     /// <returns>A JSON formatted list of the entities retrieved.</returns> 
     [HttpGet] 
     public virtual JsonResult List() 
     { 
      try 
      { 
       return Json(Repository.GetAll(), JsonRequestBehavior.AllowGet); 
      } 
      catch (Exception e) 
      { 
       return Json(e.Message, JsonRequestBehavior.AllowGet); 
      } 

     } 

     /// <summary> 
     ///  Gets a specific entity within the data store. 
     /// </summary> 
     /// <returns>A JSON formatted version of the entity retrieved.</returns> 
     [HttpGet] 
     public virtual JsonResult Get(Guid id) 
     { 
      try 
      { 
       return Json(Repository.Get(id), JsonRequestBehavior.AllowGet); 
      } 
      catch (Exception e) 
      { 
       // An error has occured. Handle the exceptions as needed and return feedback via JSON. 
       return Json(e.Message, JsonRequestBehavior.AllowGet); 
      } 

     } 

     /// <summary> 
     ///  Creates a specific entity within the data store. 
     /// </summary> 
     /// <returns>A JSON formatted version of the entity created.</returns> 
     [HttpPost] 
     public virtual JsonResult Create(TEntity entity) 
     { 
      try 
      { 
       Repository.Add(entity); 
       Repository.Save(); 
       return Json(entity); 
      } 
      catch (Exception e) 
      { 
       // An error has occured. Handle the exceptions as needed and return feedback via JSON. 
       return Json(e.Message); 
      } 

     } 

     /// <summary> 
     ///  Updates a specific entity within the data store. 
     /// </summary> 
     /// <returns>A JSON formatted version of the entity updated.</returns> 
     [HttpPut] 
     public virtual JsonResult Update(TEntity entity) 
     { 
      try 
      { 
       Repository.Update(entity); 
       Repository.Save(); 
       return Json(entity); 
      } 
      catch (Exception e) 
      { 
       // An error has occured. Handle the exceptions as needed and return feedback via JSON. 
       return Json(e.Message); 
      } 

     } 

     /// <summary> 
     ///  Deletes a specific entity from the data store. 
     /// </summary> 
     /// <returns>A JSON formatted version of the entity deleted.</returns> 
     [HttpDelete] 
     public virtual JsonResult Delete(Guid id) 
     { 
      try 
      { 
       var entity = Repository.Get(id); 
       Repository.Remove(entity); 
       Repository.Save(); 
       return Json(entity); 
      } 
      catch (Exception e) 
      { 
       // An error has occured. Handle the exceptions as needed and return feedback via JSON. 
       return Json(e.Message); 
      } 

     } 
    } 
} 
+0

不錯,只有一個問題,你將如何路由這些方法讓我們說3個類派生自抽象控制器:產品,客戶,貨幣這是類名。 通過以下途徑: api/v1/products api/v1/customers api/v1/currency。 請注意命名約定 – 2017-12-25 02:31:39