这个可以吗? [英] Is this OK?

查看:73
本文介绍了这个可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,快问题 - 我需要一点清晰。


如果我有一个只有公共共享功能的公共类,在APS.NET网络应用程序中(对于常用程序)是否安全(因为多个网页,可以随时访问其中的代码)


像这样的共享类的原因就是这样,它将包含我们在一段时间内放在一起的所有有用函数

如FillList,SelectListItem,FindLastItemInList,EmptyList,SortList等等


示例下面 - 填充下拉列表的简单例程。通过APSX页面多次访问是否安全?

忘记代码可能不完美的事实 - 只是我想以这种方式安排我的代码。


我怀疑可能有警告 - 但我需要一个更有见识的知识。< br $>
公共类MyUtils

公共共享函数FillList(ByVal objList As DropDownList,ByVal sSql As String,ByVal sConnection As String)As

Boolean

Dim dr As OleDb.OleDbDataReader,Li As ListItem

Dim cn As New OleDb.OleDbConnection(sConnection)

Dim cmd As OleDb .OleDbCommand

尝试

cn.Open()

cmd =新OleDb.OleDbCommand(sSql,cn)

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

当dr.Read

Li = New ListItem

Li.Text = dr(0 )

Li.Value = dr(1)

objList.Items.Add(Li)

结束时

Catch ex As Exception

停止

返回错误

最后

cmd.Dispose()

如果博士没什么= False然后dr.Close()

cn.Close()

结束尝试

返回True

结束函数


公共功能空列表(...)

...

结束功能


...

...

''其他公共共享功能......

...

...

结束班级


用法...

FillList(cmbListOfNames,AppSettings(" SQL。 PEOPLE"),AppSettings(CONSTRING.M ANAGEMENT))

Hi all, quick question - I need a bit of clarity.

If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it
safe (since multiple web pages, can at any time access the code within)

The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time
like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc

Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by
the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.

I suspect there may be caveats - but I need a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As
Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function

Public Function EmptyList(...)
...
End Function

...
...
''Other Public Shared Functions...
...
...
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))

推荐答案

只是一个建议(每个人都有自己的方式来完成事情)。 ..

但是,一个选项是让特定类继承标准的

''Web.UI.Page''类。例如,您的类可以被称为''PageBase''和

如下所示:


公共类PageBase:System.Web.UI.Page

{


......


}


将所有常用例程(方法)和(属性)放在这个

特定类中。然后,当你想创建一个新的webform时,只需继承这个类而不是普通的''System.Web.UI.Page''类(

默认值)使用Visual Studio为您设置的方法是继承自

Web.UI.Page类,之后只需将它放入您自己的类中)。对于

示例:


公共类DealerLinksList:myNameSpace.PageBase


{

....

}


您可以在PageBase类中保护这些方法。您从该类创建的每个

webform都可以访问这些常用方法

和过程而无需实例化不同的对象/ etc。再说一次,你可以

得到30个人回复和30种不同的方法..我喜欢这个特别的

方法,因为它为我提供了OO类型的能力(继承)和

更改可以简单地发送到''parent''类,只有那些

直接从该父类继承的成员才会受到影响...

" Stevie_mac" <无****** @ please.com>在留言中写道

news:e1 ************** @ TK2MSFTNGP10.phx.gbl ...
Just a suggestion (everyone has their own ways of accomplishing things)...
But, one option would be to have that particular class inherit the standard
''Web.UI.Page'' class. For example, your class could be called ''PageBase'' and
look like the following:

public class PageBase : System.Web.UI.Page
{

......

}

Put all of your common routines (methods) and (properties) in this
particular class. Then, when you want to create a new webform simply
inherit this class instead of of the normal ''System.Web.UI.Page'' class (the
default that gets setup for you with Visual Studio is to inherit from the
Web.UI.Page class, after it does that simply put in your own class). For
example:

public class DealerLinksList : myNameSpace.PageBase

{

....
}

You can make these methods ''protected'' within your ''PageBase'' class. Every
webform you create from that class will have access to these common methods
and procedures without instantiating different objects/etc. Again, you may
get 30 people responding and 30 different methods.. I like this particular
method because it provides me with an OO type capability (inheritence) and
changes can simply be made to the ''parent'' class and only those members that
directly inherit from that parent class are affected...

"Stevie_mac" <no******@please.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
大家好,快速提问 - 我需要一点清晰。

如果我有一个只有公共共享功能的公共类,在一个
APS.NET网络应用程序(用于常见程序)是安全的(因为有多个网页,可以随时访问其中的代码。

像这样的共享类的原因是,它将包含我们放在一起的有用函数的所有
像FillList,SelectListItem,FindLastItemInList,EmptyList,SortList等
等等
下面的示例 - 一个填充下拉列表的简单例程。是否可以通过APSX页面多次访问
安全?忘记代码可能不完美的事实 - 只有
我想以这种方式安排我的代码。
我怀疑可能有警告 - 但我需要一个更有见识的意见。
<公共类MyUtils
公共共享函数FillList(ByVal objList As DropDownList,ByVal
sSql As String,ByVal sConnection As String)As Boolean
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As OleDb.OleDbConnection(sConnection)
Dim cmd作为OleDb.OleDbCommand
尝试
cn.Open()
cmd =新的OleDb.OleDbCommand(sSql) ,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
Li = New ListItem
Li.Text = dr(0)
李.Value = dr(1)
objList.Items.Add(Li)
结束时
Catch ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn.Close()
结束尝试
返回真实
结束功能

公共功能空列表(...)
...
结束功能

...
...
''其他公共共享功能......
...
...
结束课

用法.. 。
FillList(cmbListOfNames,
AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))
Hi all, quick question - I need a bit of clarity.

If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it safe (since multiple web pages, can at any time access the code within)

The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc
Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.
I suspect there may be caveats - but I need a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function

Public Function EmptyList(...)
...
End Function

...
...
''Other Public Shared Functions...
...
...
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))



只是一个建议(每个人都有自己的方式来完成事情)...

但是,一个选择是让特定的类继承标准

' 'Web.UI.Page''类。例如,您的类可以被称为''PageBase''和

如下所示:


公共类PageBase:System.Web.UI.Page

{


......


}


将所有常用例程(方法)和(属性)放在这个

特定类中。然后,当你想创建一个新的webform时,只需继承这个类而不是普通的''System.Web.UI.Page''类(

默认值)使用Visual Studio为您设置的方法是继承自

Web.UI.Page类,之后只需将它放入您自己的类中)。对于

示例:


公共类DealerLinksList:myNameSpace.PageBase


{

....

}


您可以在PageBase类中保护这些方法。您从该类创建的每个

webform都可以访问这些常用方法

和过程而无需实例化不同的对象/ etc。再说一次,你可以

得到30个人回复和30种不同的方法..我喜欢这个特别的

方法,因为它为我提供了OO类型的能力(继承)和

更改可以简单地发送到''parent''类,只有那些

直接从该父类继承的成员才会受到影响...

" Stevie_mac" <无****** @ please.com>在留言中写道

news:e1 ************** @ TK2MSFTNGP10.phx.gbl ...
Just a suggestion (everyone has their own ways of accomplishing things)...
But, one option would be to have that particular class inherit the standard
''Web.UI.Page'' class. For example, your class could be called ''PageBase'' and
look like the following:

public class PageBase : System.Web.UI.Page
{

......

}

Put all of your common routines (methods) and (properties) in this
particular class. Then, when you want to create a new webform simply
inherit this class instead of of the normal ''System.Web.UI.Page'' class (the
default that gets setup for you with Visual Studio is to inherit from the
Web.UI.Page class, after it does that simply put in your own class). For
example:

public class DealerLinksList : myNameSpace.PageBase

{

....
}

You can make these methods ''protected'' within your ''PageBase'' class. Every
webform you create from that class will have access to these common methods
and procedures without instantiating different objects/etc. Again, you may
get 30 people responding and 30 different methods.. I like this particular
method because it provides me with an OO type capability (inheritence) and
changes can simply be made to the ''parent'' class and only those members that
directly inherit from that parent class are affected...

"Stevie_mac" <no******@please.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
大家好,快速提问 - 我需要一点清晰。

如果我有一个只有公共共享功能的公共类,在一个
APS.NET网络应用程序(用于常见程序)是安全的(因为有多个网页,可以随时访问其中的代码。

像这样的共享类的原因是,它将包含我们放在一起的有用函数的所有
像FillList,SelectListItem,FindLastItemInList,EmptyList,SortList等
等等
下面的示例 - 一个填充下拉列表的简单例程。是否可以通过APSX页面多次访问
安全?忘记代码可能不完美的事实 - 只有
我想以这种方式安排我的代码。
我怀疑可能有警告 - 但我需要一个更有见识的意见。
<公共类MyUtils
公共共享函数FillList(ByVal objList As DropDownList,ByVal
sSql As String,ByVal sConnection As String)As Boolean
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As OleDb.OleDbConnection(sConnection)
Dim cmd作为OleDb.OleDbCommand
尝试
cn.Open()
cmd =新的OleDb.OleDbCommand(sSql) ,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
Li = New ListItem
Li.Text = dr(0)
李.Value = dr(1)
objList.Items.Add(Li)
结束时
Catch ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn.Close()
结束尝试
返回真实
结束功能

公共功能空列表(...)
...
结束功能

...
...
''其他公共共享功能......
...
...
结束课

用法.. 。
FillList(cmbListOfNames,
AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))
Hi all, quick question - I need a bit of clarity.

If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it safe (since multiple web pages, can at any time access the code within)

The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc
Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.
I suspect there may be caveats - but I need a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function

Public Function EmptyList(...)
...
End Function

...
...
''Other Public Shared Functions...
...
...
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))



对我来说很好看。只是你可能想要在各种应用程序中使用的辅助函数,而不必实例化一个对象。


-

HTH,

Kevin Spencer

..Net开发人员

Microsoft MVP

大事做成很多小东西



" Stevie_mac" <无****** @ please.com>在留言中写道

news:e1 ************** @ TK2MSFTNGP10.phx.gbl ...
Looks good to me. Just the sort of helper function you might want to use in
a variety of apps without necessarily instantiating an object to do it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Stevie_mac" <no******@please.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
大家好,快速提问 - 我需要一点清晰。

如果我有一个只有公共共享功能的公共类,在一个
APS.NET网络应用程序(用于常见程序)是安全的(因为有多个网页,可以随时访问其中的代码。

像这样的共享类的原因是,它将包含我们放在一起的有用函数的所有
像FillList,SelectListItem,FindLastItemInList,EmptyList,SortList等
等等
下面的示例 - 一个填充下拉列表的简单例程。是否可以通过APSX页面多次访问
安全?忘记代码可能不完美的事实 - 只有
我想以这种方式安排我的代码。
我怀疑可能有警告 - 但我需要一个更有见识的意见。
<公共类MyUtils
公共共享函数FillList(ByVal objList As DropDownList,ByVal
sSql As String,ByVal sConnection As String)As Boolean
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As OleDb.OleDbConnection(sConnection)
Dim cmd作为OleDb.OleDbCommand
尝试
cn.Open()
cmd =新的OleDb.OleDbCommand(sSql) ,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
Li = New ListItem
Li.Text = dr(0)
李.Value = dr(1)
objList.Items.Add(Li)
结束时
Catch ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn.Close()
结束尝试
返回真实
结束功能

公共功能空列表(...)
...
结束功能

...
...
''其他公共共享功能......
...
...
结束课

用法.. 。
FillList(cmbListOfNames,
AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))
Hi all, quick question - I need a bit of clarity.

If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it safe (since multiple web pages, can at any time access the code within)

The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc
Example below - a simple routine that fills a dropdownlist. Would it be safe being accessed at multiple times by the APSX pages? Forget the fact the code might not be perfect - only that I would like to arrange my code this way.
I suspect there may be caveats - but I need a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As DropDownList, ByVal sSql As String, ByVal sConnection As String) As Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function

Public Function EmptyList(...)
...
End Function

...
...
''Other Public Shared Functions...
...
...
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))



这篇关于这个可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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