使用实体框架查询数据时,哪种防御性检查更可取?

时间:2021-11-02 10:19:42

Which of the following defensive checks is preferable when querying data using the Entity Framework?

使用实体框架查询数据时,以下哪项防御性检查更可取?

1. The null check:

1.空检查:

user = users.SingleOrDefault(u => u.Email.Equals(email));

if(user == null)
{
    throw new Exception("Some critical error!");
}

OperationsOnUser(user);

2. LINQ's Any():

2. LINQ的Any():

if(users.Any(u => u.Email.Equals(email)))
{
    user = users.Single(u => u.Email.Equals(email));
}
else
{
    throw new Exception("Some critical error!");
}

OperationsOnUser(user);

Personally I use the second option because it feels cleaner to me, but am I not performing two database calls instead of one? I'm unsure about the inner mechanics of Entity Framework.

我个人使用第二个选项,因为它对我来说感觉更干净,但是我没有执行两个数据库调用而不是一个?我不确定实体框架的内在机制。

2 个解决方案

#1


2  

I will quote Jon Skeet:

我会引用Jon Skeet的话:

I always advise anyone implementing a LINQ-like operator to only iterate over any input sequence once. There are sequences which are impossible to iterate over multiple times, or which may give different results each time. That's bad news.

我总是建议任何实现LINQ类操作符的人只迭代任何输入序列一次。存在不可能多次迭代的序列,或者每次可能给出不同结果的序列。那是个坏消息。

Keep the habits to iterate only one time.

保持习惯只迭代一次。

For your example, yes, you are accessing twice the database, and that's not a good idea.
In fact, your second option feel to me a lot lot messier than the first option. You need to recalibrate your messy-detector! :)

对于您的示例,是的,您正在访问数据库的两倍,这不是一个好主意。事实上,你的第二个选择对我来说比第一个选项更麻烦。你需要重新校准你的凌乱探测器! :)

#2


1  

AakashM's comment is correct. But to expand on that, (1) is better because you're doing a single operation on the database whilst in the second example you're doing it twice. Once to see it is there, and once to use it. I think the answer is self-evident from my perspective.

AakashM的评论是正确的。但是为了扩展它,(1)更好,因为你在数据库上进行单个操作,而在第二个例子中,你做了两次。一旦看到它就在那里,一旦使用它。我认为从我的角度来看,答案是不言而喻的。

#1


2  

I will quote Jon Skeet:

我会引用Jon Skeet的话:

I always advise anyone implementing a LINQ-like operator to only iterate over any input sequence once. There are sequences which are impossible to iterate over multiple times, or which may give different results each time. That's bad news.

我总是建议任何实现LINQ类操作符的人只迭代任何输入序列一次。存在不可能多次迭代的序列,或者每次可能给出不同结果的序列。那是个坏消息。

Keep the habits to iterate only one time.

保持习惯只迭代一次。

For your example, yes, you are accessing twice the database, and that's not a good idea.
In fact, your second option feel to me a lot lot messier than the first option. You need to recalibrate your messy-detector! :)

对于您的示例,是的,您正在访问数据库的两倍,这不是一个好主意。事实上,你的第二个选择对我来说比第一个选项更麻烦。你需要重新校准你的凌乱探测器! :)

#2


1  

AakashM's comment is correct. But to expand on that, (1) is better because you're doing a single operation on the database whilst in the second example you're doing it twice. Once to see it is there, and once to use it. I think the answer is self-evident from my perspective.

AakashM的评论是正确的。但是为了扩展它,(1)更好,因为你在数据库上进行单个操作,而在第二个例子中,你做了两次。一旦看到它就在那里,一旦使用它。我认为从我的角度来看,答案是不言而喻的。