2010-10-30 42 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