我的自定义ASP.NET MVC实体绑定:这是一个好的解决方案吗? [英] My custom ASP.NET MVC entity binding: is it a good solution?

查看:126
本文介绍了我的自定义ASP.NET MVC实体绑定:这是一个好的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想允许选择我们的实体(从下拉等)的网页上,让我们说产品。因此,我可能会收到这样的:

Suppose I want to allow to select our entity (from a dropdown, etc) on a page, let's say Product. As a result I may receive this:

public ActionResult SelectedAction(Guid productId)
{
}

不过,我想用模型粘合剂电源,所以不是我写的模型绑定从储备库中获得我的产品,而是使用

But, I want to use model binders power, so instead I write model binder to get my product from repository and instead use

public ActionResult SelectedAction(Product product)
{
   if (ModelState.IsValid) {} else {}
}

如果产品无效我的模型粘结剂将设置模型的状态为false。
现在,问题是这种方法:

My model binder will set model state to false if product is invalid. Now, there're problems with this approach:


  1. 这并不总是易于使用强类型的方法,如Html.ActionLink(C => c.SelectedAction(ID)),因为我们需要传递的产品,而不是ID。

  2. 这是不好用实体控制器参数,无论如何。

  3. 如果模型状态无效,我想重定向回,并显示错误,我不能preserve所选的产品!由于结合产物没有设置,我的ID是不存在。我想这样做RedirectToAction(C => c.Redisplay(产品)),但当然,这是不可能的。

现在,好像我又回到了使用GUID的productId作为参数。然而,有一个解决方案,我想present和讨论。

Now, seems like I'm back to use "Guid productId" as parameter... However, there's one solution that I'd like to present and discuss.

   public class EntityViewModel<T> where T : BaseEntity
   {
      public EntityViewModel(Guid id)
      {
         this.Id = id;
      }

      public static implicit operator EntityViewModel<T>(T entity)
      {
         return new EntityViewModel<T>(entity.Id);
      }

      public override string ToString()
      {
         return Id.ToString();
      }

      public Guid Id { get; set; }
      public T Instance { get; set; }
   }

现在,如果我用

public ActionResult SelectedAction(EntityViewModel<Product> product)
{
   if (ModelState.IsValid) {} else {}
}

所有的问题都解决了:

all the problems are solved:


  1. 我可以通过EntityViewModel仅标识设置,如果我只有编号。

  2. 我不使用实体参数。此外,我
    可以使用EntityViewModel作为另一个视图模型内财产。

  3. 我可以通过EntityViewModel回RedirectToController,它会保持其Id值,这将是
    重新显示用户与验证消息一起(感谢MVCContrib和ModelStateToTempData / PassParametersDuringRedirect)。

模型绑定将从存储库中获得实例,并将为像在数据库中找不到等模型状态的错误。我可以用的东西一样的ActionLink(C => c.Action(Model.MyProductViewModelProperty))。

The model binder will get Instance from the repository and will set model state errors like "Not found in database" and so on. And I can use things like ActionLink(c => c.Action(Model.MyProductViewModelProperty)).

现在的问题是,是否有什么缺点吗?我看不出什么不好,但我还是新的MVC和可能会错过一些重要的事情。也许有会更好批准的方式?也许这就是为什么每个人都使用实体的ID作为输入参数和属性?

The question is, are there any drawbacks here? I can't see anything bad but I'm still new to MVC and may miss some important things. Maybe there're better and approved ways? Maybe this is why everybody uses entity IDs as input parameters and properties?

推荐答案

总之,看起来像一个好appoach给我...

Overall that looks like a good appoach to me...

作为替代方案,你可以使用POCO为你的视图模型,然后我想所有的三个问题将被自动解决。你见过Automapper项目,允许一个实体DTO的方法吗?这会给你更多的灵活性,通过从EntityModel分离你的ViewModel,但实际上取决于你的应用程序要构建的复杂性。

As an alternative, you could use POCO for your viewmodel then I think all 3 problems would be solved automatically. Have you seen the Automapper project that allows an Entity to DTO approach? This would give you more flexibility by separating you ViewModel from your EntityModel, but really depends on the complexity of you application you are building.

MVC的ViewDataExtensions也可能是有用的,而不是创建自定义的容器,你在2号提举办各种视图模型对象。

MVC's ViewDataExtensions might also be useful instead of creating custom containers to hold various viewmodel objects as you mention in number 2.

MVCContrib的ModelStateToTempData应该对任何序列化对象的工作(必须是序列化的任何进程外的sessionState供应商如:SQL,速度等),因此您可以使用,即使没有包装实体类你能不能?

MVCContrib's ModelStateToTempData should work for any serializable object (must be serializable for any out of process sessionstate providers eg. SQL, Velocity etc.), so you could use that even without wrapping your entity classes couldn't you?

这篇关于我的自定义ASP.NET MVC实体绑定:这是一个好的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆