通常的做法是,在数据类中允许空值。 [英] Common practice, allowing null values in data classes.

查看:71
本文介绍了通常的做法是,在数据类中允许空值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建数据类或业务规则的可接受方法是什么?
对象类,其属性允许返回空值?


For示例...我有一个名为CResults的类具有以下属性。


TestID int

QuestionID int

AnswerID int


所以,这是一个简单的例子,但我希望能够知道AnswerID是否是
null。我不想知道它是0或其他任何东西。我想要

能够让它返回null(而不是默认的没有值0)


这不是我的真实世界的例子,这只是一个例子,我正在使用

解释我想做什么。我正在开发一个大型应用程序和

类的属性可以包含null或legalmate值。如果

有一个整数属性我想知道它是0还是null。有一个很大的差异,因为0表示字面上0输入到

数据库中,而null表示数据库中没有输入任何内容。


如何使用VB.NET设计我的数据类来考虑这一点。

VB中的内部数据类型(整数,长整数,字符串等)不能有null

值。


这种情况​​下的常见做法是什么?

解决方案

< blockquote>一种方法是将属性定义为SQL类型,它们有一个IsNull

属性,我不喜欢这样我使用字符串代替,这样我就可以格式化它

然而我想要当我从数据库中取出并放入数据库时​​,字符串是

空我在数据库中放置一个null或者期望该值在null中为null
数据库。它适用于我...

" D Witherspoon"写道:

创建数据类或业务规则的接受方法是什么?具有允许返回空值的属性的对象类?

TestID int
QuestionID int
AnswerID int

所以,这是一个简单的例子,但我希望能够知道AnswerID是否为空。我不想知道它是0或其他任何东西。我希望能够让它返回null(而不是默认的没有值0)

这不是我的真实世界的例子,它只是一个例子我'用来解释我想做什么。我正在开发一个大型应用程序,并且类的属性可以包含null或legalmate值。如果
有一个整数属性我想知道它是0还是null。有一个很大的区别,因为0意味着字面上0输入到
数据库中,null表示数据库中没有输入任何内容。

如何设计我的数据类使用VB.NET来考虑这一点。
VB中的内部数据类型(整数,长整数,字符串等)不能具有null值。

什么在这种情况下是常见的做法吗?



我通常做的是另一个属性,指示值是否为

有效与否。例如,您可以添加属性HasAnswerID;如果有一个有效的(非空)AnswerID值,则返回true,如果

的AnswerID值为空,则返回false。


AnswerID属性总是返回一个int值,如果底层数据为null,则返回一些默认值




另一种选择是返回一个对象如果底层数据

为null,则值设置为null;如果不是,则设置为整数值。问题是你需要

将它作为一个int投射才能使用它。


-

罗杰


< http://www.SequenceDiagramEditor.com>

序列图编辑器 - 一种快速简便的方法来绘制和编辑序列图。

D Witherspoon写道:

什么是创建数据类或业务规则的可接受方法
具有允许的属性的对象类返回空值?

例如......我有一个名为CResults的类,具有以下属性。

TestID int
QuestionID int
AnswerID int

所以,这是一个简单的例子,但我希望能够知道AnswerID是否为空。我不想知道它是0或其他任何东西。我希望能够让它返回null(而不是默认的没有值0)

这不是我的真实世界的例子,它只是一个例子我'用来解释我想做什么。我正在开发一个大型应用程序,并且类的属性可以包含null或legalmate值。如果
有一个整数属性我想知道它是0还是null。有一个很大的区别,因为0意味着字面上0输入到
数据库中,null表示数据库中没有输入任何内容。

如何设计我的数据类使用VB.NET来考虑这一点。
VB中的内部数据类型(整数,长整数,字符串等)不能具有null值。

什么在这种情况下是常见的做法吗?



不要将数据类型定义为''int''。改为使用SQL数据类型,

喜欢SqlInt32

参见
http://msdn.microsoft.com/library/en...taSqlTypes.asp

-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛发表的意见是:我自己的,而不是我的雇主的代表。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

D Witherspoon < DW ********** @ noway.org>在留言中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

什么是接受的创建数据类或业务规则的方法
具有允许返回空值的属性的对象类?

例如......我有一个名为CResults的类,具有以下内容
属性。

TestID int
QuestionID int
AnswerID int

所以,这是一个简单的例子,但我希望能够知道AnswerID是否为空。我不想知道它是0或其他任何东西。我希望能够让它返回null(而不是默认的没有值0)

这不是我的真实世界的例子,它只是一个例子我'用来解释我想做什么。我正在开发一个大型应用程序,并且类的属性可以包含null或legalmate值。如果
有一个整数属性我想知道它是0还是null。有一个巨大的区别,因为0表示字面上0输入到数据库中,而null表示数据库中没有输入任何内容。

如何设计我的数据类使用VB.NET来考虑这一点。
VB中的内部数据类型(整数,长整数,字符串等)不能具有
空值。

什么在这种情况下是常见的做法吗?



What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don''t want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I''m using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?

解决方案

One way would be to define your properties as SQL Types, they have an IsNull
property, I didn''t like this I use strings instead, that way I can format it
however I want when I pull from and put into the database, it the string is
empty I put a null in the database or expect that value to be null in the
database. It works ok for me...
"D Witherspoon" wrote:

What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don''t want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I''m using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?



What I usually do is have another property that indicates whether the value is
valid or not. For example, you could add a property "HasAnswerID" that would
return true if there is a valid (not null) AnswerID value and false if the
AnswerID value is null.

The AnswerID property would always return an int value, with some default value
if the underlying data is null.

Another option is to return an object value set to null if the underlying data
is null and to the integer value if it is not. The problem is that you have to
cast it as an int before you can use it.

--
Rodger

<http://www.SequenceDiagramEditor.com>
Sequence Diagram Editor - A quick and easy way to draw and edit sequence diagrams.

D Witherspoon wrote:

What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don''t want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I''m using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is a
huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have null
values.

What is common practice in this case?



Don''t define the data type to be ''int''. Use the SQL data types instead,
like SqlInt32
See
http://msdn.microsoft.com/library/en...taSqlTypes.asp
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"D Witherspoon" <dw**********@noway.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

What is the accepted method of creating a data class or business rules
object class with properties that will allow the returning of null values?

For example... I have a class named CResults with the following
properties.

TestID int
QuestionID int
AnswerID int

So, this is a simple example, but I want to be able to know if AnswerID is
null. I don''t want to know that it is 0 or anything else. I want to be
able to have it return null (instead of the default "nothing" value of 0)

This is not my real world example, it is just an example I''m using to
explain what I want to do. I am developing a large scale application and
the properties of the classes can contain null or legitmate values. If
there is an integer property I want to know if it was 0 or null. There is
a huge difference, as 0 implies that literally 0 was entered into the
database, and null is that nothing has been entered in the database.

How can I design my data classes using VB.NET to take this into account.
The intrinsic datatypes in VB (integer, long, string, etc) can not have
null values.

What is common practice in this case?



这篇关于通常的做法是,在数据类中允许空值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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