屏蔽与视图模型为空的域属性 [英] Shielding nullable domain properties with ViewModel

查看:94
本文介绍了屏蔽与视图模型为空的域属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4.0,并利用POCO对象。当我从数据库填充POCO的对象,我的属性值转换成我自己的领域对象,我们可以叫我的模型。

I am using Entity Framework 4.0, and making use of POCO objects. When I populate POCO objects from the DB, I translate property values to my own Domain objects, which we can call my Model.

必须的,我的模式的场是否是可空取决于它映射到在数据库中的值是否来自一个NULL或NOT NULL列。我不会进入细节,但该值必须在DB可空的,因为用户可以部分发布到公众面前保存对象的草稿。既然如此,我有几个字段都为空。所以我们可以说我的模型是这样的:

Necessarily, whether or not the fields of my Model are Nullable depends on whether the value it maps to in the database comes from a NULL or NOT NULL column. I won't go into detail, but the values must be nullable in the DB, because a user can partially save a draft of the object before publishing it to the public. That being the case, I have several fields that are nullable. So let's say my model looks like:

public class MyModel
{
   public int? Field1 {get; set; }
   public DateTime? Field2 {get; set; }
   public int Field3 {get; set; }
}

如果我使用这个模型在我看来,完全可空场,我开始接收,告诉我,我不能用可空属性在各个地方的价值观,像HTML辅助,等等。我可以说错误的东西像如果(Model.MyBoolField.HasValue&安培;&安培; Model.MyBoolField.Value){//等} ,但是,对于一个视图的感觉笨重

If I use this Model in my View, complete with nullable fields, I begin receiving errors that tell me I cannot use nullable properties as values in various places, like HTML helpers, etc. I could say something like if (Model.MyBoolField.HasValue && Model.MyBoolField.Value) { // etc }, but that feels bulky for a view.

我认为创建一个从我原来的域对象继承并具有,返回一个适当的值我可空字段不可为空的版本,如果基础版视图模型对象为空。因此,像:

I considered creating a ViewModel object that inherits from my original domain object and has new, non-nullable versions of my nullable fields that return an appropriate value if the base version is null. So something like:

public class MyViewModel : MyModel
{
   public new int Field1
   {
      get { return base.Field1 ?? 7; }
   }

   public new DateTime Field2
   {
      get { return base.Field2 ?? DateTime.Now; }
   }
}

我这个问题是我并不总是知道一个良好的默认值显示。如果我抛出异常,在视图模型的getter当基值为null?是贫穷的做法呢?

My problem with this is that I don't always know a good "default" value to display. What if I threw an exception in the View Model's getter when the base value is null? Is that poor practice?

基本上,我在寻找如何在一个视图中显示处理时可空字段的模型,特别是最佳做法。

I'm basically looking for a best practice on how to handle nullable fields in a model, particularly when displaying in a View.

推荐答案

如果你只需要在视图中显示这些字段,你不需要指定或检查是否具有价值或没有。

If you just need to display these fields in a View, you don't need to specify or check whether is has a value or not.

在您查看文件中使用 Model.Field1 就足够了。它会简单的不显示任何东西,也不会抛出例外。您可以随时使用 ?? 来设置默认时,它是有道理的。

Using Model.Field1 in your View file is enough. It will simple not display anything, and it won't throw an exception. You can always use ?? to set a default when it makes sense.

@(Model.Field1 ?? "There is nothing to see here")

这篇关于屏蔽与视图模型为空的域属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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