EF4.2代码优先-处理空实体的属性 [英] EF4.2 Code First - Handling null entity's properties

查看:99
本文介绍了EF4.2代码优先-处理空实体的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用EF4.2和Code First(尽管我不得不处理这个正弦的Linq2SQL),我发现自己一直遇到这样的情况:

Using EF4.2 and Code First (although I''ve had to deal with this sine Linq2SQL), I find myself coming across situations like this all the time:

Label1.Text = MyEvent.EventType.Description;



MyEvent是事件类型,而EventType是导航属性.

除非EventType为null,否则它会很好用,因为尚未定义.这是随时间填写的表单的一部分,因此在此过程中,许多字段和相关属性将为空.

首先,我会写:



MyEvent is of type Event, and EventType is a navigation property.

This works great unless the EventType is null, because it hasn''t been defined yet. This is part of a form that is filled out over time so many fields and related properties will be null during the process.

At first, I''d write:

Label1.Text = MyEvent.EventType == null ?? "" : MyEvent.EventType.Description;



这行得通,但确实使我的应用程序代码混乱不堪(也需要大量输入).

我建议我应该在Getter中加入一些逻辑,但是我不确定Code First将如何处理它,因此我想避免在类定义中造成混乱,方法是通过私有支持将它们炸开变量和全长属性定义(当前使用自动属性,这使事情变得非常整洁和可读).

我当前针对此问题的解决方案是在EventType上定义扩展方法,如下所示:



This works, but does it make an inelegant mess out of my app code (and it''s a lot of typing, too).

It was suggested to me that I should put some logic in the Getter but I''m not sure how Code First will handle that and I''d like to avoid making a mess out of my class definitions by blowing them up with private backing variables and full-length property definitions (currently using auto-properties which makes things very neat and readable).

My current solution to this problem is to define an extension method on the EventType like so:

public static string GetDescription(this EventType ThisEventType)
{
    return (ThisEventType == null ? "" : ThisEventType.Description);
}



这样我就可以:



so that I can:

Label1.Text = MyEvent.EventType.GetDescription();



它很干净,但是.Description和.GetDescription的冗余使我感到困扰,因为它只是要求使用.Description编译时不错,并在Dev/Test中进行了很好的测试,但可能在以后的生产中崩溃.当null滚动通过并且有人忘记使用.GetDescription时.

我曾考虑过在字符串中使用反射和属性名称,但是asp/ado如此!

还有其他人对此问题有一个很好的,干净的(更好的!)解决方案吗?这是一种常见的情况,应该有一种很好的处理方法.



It''s clean but the redundancy of .Description and .GetDescription bothers me since it''s just asking to be the kind of bug where using .Description compiles nice and tests out fine in Dev/Test but may blow up in production later when a null rolls through and someone forgot to use .GetDescription.

I''ve thought about using reflection and property names in strings but that''s so asp/ado!

Does anyone else have a nice, clean (and better!) solution to this problem? This is such a common scenario, there should be a nice way to handle it.

推荐答案

http://tomlev2.wordpress.com/2010/02/21/automating-null-checks-with-linq-expressions/ [ ^ ]

链接了null检查和Maybe monad [ ^ ]

http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c16503 [ ^ ]

希望这些链接能有所帮助.
http://tomlev2.wordpress.com/2010/02/21/automating-null-checks-with-linq-expressions/[^]

Chained null checks and the Maybe monad[^]

http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c16503[^]

Hope these links can help.


这篇关于EF4.2代码优先-处理空实体的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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