尝试从数据层加载对象时找不到它时的最佳实践 [英] Best Practice when trying to Load object from Data Layer when it doesn't find it
问题描述
如果我要让我的商业图层要求数据层根据我传递给它的关键字段信息加载特定对象
,并且它无法创建对象
因为它不在Db中,如果数据层传回异常或
a null引用?
如果异常,我会想象我应该抓住这个并向用户显示一条消息
?
感谢
TS写道:
如果我要让我的biz层要求数据层加载一个特定的
对象基于传递给它的关键字段信息,它不能创建
对象因为它不在Db中,如果数据层传回
异常或null参考?
如果有异常,我想我应该抓住这个并向用户显示一条消息
?
我更喜欢一些未找到记录的非异常指示。
如果你想返回一个空对象,那就是工作。不要忘记检查
是否为空,因为如果您尝试访问
对象而不先检查空对象引用,则会得到空引用异常。 br />
您可能还会考虑使用其他指标,例如状态代码或
bool值来指示对象是否已成功创建
$来自DB的b $ b。
-
Tom Porterfield
例外情况应该是例外
因此,如果这是您要发生的事情,那么返回null。
>
汤姆波特菲尔德 < tp ****** @ mvps.org写信息
新闻:ek ************** @ TK2MSFTNGP03.phx.gbl ...
TS写道:
如果我要让我的商业层要求数据层加载特定的
对象基于我传递给它的关键字段信息,它不能创建
对象因为它不在Db中,如果数据层传回一个
异常或空引用?
如果有异常,我想我应该抓住这个并显示
消息
给用户?
我更喜欢一些未找到记录的非异常指示。
如果你想返回一个空对象,那就是工作。别忘了
check
for null,因为如果你得到一个空引用异常,如果您尝试访问
对象,而不先检查空对象引用。
您可能还会考虑使用其他指标,例如状态代码
或
bool值表示对象是否成功
创建
来自数据库。
-
Tom Porterfield
来自一个特殊的人。
:-)
-
联合创始人,Eggheadcafe.com开发人员门户网站:
http://www.eggheadc afe.com
UnBlog:
http:/ /petesbloggerama.blogspot.com
" sloan"写道:
>
例外情况应该是例外
因此,如果这是你要发生的事情,然后返回一个null。
汤姆波特菲尔德 < tp ****** @ mvps.org写信息
新闻:ek ************** @ TK2MSFTNGP03.phx.gbl ...
TS写道:
如果我要让我的商业层要求数据层加载特定的
对象基于我传递给它的关键字段信息,它不能创建
对象因为它不在Db中,如果数据层传回一个
异常或空引用?
>
如果有异常,我想我应该抓住这个并显示
消息
给用户?
我更喜欢一些未找到记录的非异常指示。
如果你想返回一个空对象,那就是工作。别忘了
check
for null,因为如果你得到一个空引用异常,如果您尝试访问
对象,而不先检查空对象引用。
您可能还会考虑使用其他指标,例如状态代码
或
bool值表示对象是否成功
创建
来自DB。
-
Tom Porterfield
>
If I was to have my biz layer ask the data layer to load a particular object
based on key field info i pass to it, and it cannot create the object
becaues it isnt'' in the Db, should the data layer pass back an exception or
a null reference?
if an exception, i would imagine i should catch this and display a message
to the user?
thanks
TS wrote:If I was to have my biz layer ask the data layer to load a particular
object based on key field info i pass to it, and it cannot create the
object becaues it isnt'' in the Db, should the data layer pass back an
exception or a null reference?
if an exception, i would imagine i should catch this and display a message
to the user?I would prefer some non-exception indicator that the record was not found.
If you want to return a null object, that would work. Don''t forget to check
for null as you will get a null reference exception if you try to access the
object without checking for a null object reference first.
You might also consider using some other indicator such as a status code or
bool value that indicates whether or not the object was successfully created
from the DB.
--
Tom Porterfield
Exceptions should be "Exceptional"
Thus, if this is something you except to happen, then return a null.
"Tom Porterfield" <tp******@mvps.orgwrote in message
news:ek**************@TK2MSFTNGP03.phx.gbl...TS wrote:If I was to have my biz layer ask the data layer to load a particular
object based on key field info i pass to it, and it cannot create the
object becaues it isnt'' in the Db, should the data layer pass back an
exception or a null reference?
if an exception, i would imagine i should catch this and display a
message
to the user?
I would prefer some non-exception indicator that the record was not found.
If you want to return a null object, that would work. Don''t forget tocheck
for null as you will get a null reference exception if you try to accessthe
object without checking for a null object reference first.
You might also consider using some other indicator such as a status codeor
bool value that indicates whether or not the object was successfullycreated
from the DB.
--
Tom Porterfield
An exceptional comment from an exceptional guy.
:-)
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"sloan" wrote:
>
Exceptions should be "Exceptional"
Thus, if this is something you except to happen, then return a null.
"Tom Porterfield" <tp******@mvps.orgwrote in message
news:ek**************@TK2MSFTNGP03.phx.gbl...TS wrote:If I was to have my biz layer ask the data layer to load a particular
object based on key field info i pass to it, and it cannot create the
object becaues it isnt'' in the Db, should the data layer pass back an
exception or a null reference?
>
if an exception, i would imagine i should catch this and display a
message
to the user?I would prefer some non-exception indicator that the record was not found.
If you want to return a null object, that would work. Don''t forget tocheck
for null as you will get a null reference exception if you try to accessthe
object without checking for a null object reference first.
You might also consider using some other indicator such as a status codeor
bool value that indicates whether or not the object was successfullycreated
from the DB.
--
Tom Porterfield
这篇关于尝试从数据层加载对象时找不到它时的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!