VB.Net中ReadOnly属性/函数的最佳实践是什么?

时间:2010-09-28 12:07:46

标签: vb.net visual-studio-2008 function properties

我对VB.Net和语法不太新,但我不是专家。我正在用VB重写一些以前在C#中的东西,当我这样做的时候,我遇到了一个“困境”。我可以拥有一个ReadOnly属性:

Public ReadOnly Property MaximumIndenture()
    Get
        Try
            'If the connection is closed, open it.'
            If _dbConnection.State = ConnectionState.Closed Then
                _dbConnection.Open()
            End If

            Dim dictFigureIndenture As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
            Using dbReader As IDataReader = ExecuteReader("SELECT PART_FIGURE, MAX(PART_INDENTURE) AS 'MAX_INDENT' FROM PARTS GROUP BY PART_FIGURE")
                While dbReader.Read()
                    dictFigureIndenture.Add(dbReader("PART_FIGURE").ToString(), Convert.ToInt32(dbReader("MAX_INDENT").ToString()))
                End While
            End Using

            'If the connection is open, do what you need to and/or close it.'
            If _dbConnection.State = ConnectionState.Open Then
                _dbConnection.Close()
            End If

            Return dictFigureIndenture
        Catch ex As Exception
            'An exception was thrown.  Show it to the user so they can report it.'
            MessageBox.Show(ex.Message)

            'If the connection is open, do what you need to and/or close it.'
            If _dbConnection.State = ConnectionState.Open Then
                _dbConnection.Close()
            End If

            Return Nothing
        End Try
    End Get
End Property

我可以拥有一个同样的功能:

Public Function MaximumIndenture() As Integer
    Try
        'If the connection is closed, open it.'
        If _dbConnection.State = ConnectionState.Closed Then
            _dbConnection.Open()
        End If

        Dim dictFigureIndenture As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
        Using dbReader As IDataReader = ExecuteReader("SELECT PART_FIGURE, MAX(PART_INDENTURE) AS 'MAX_INDENT' FROM PARTS GROUP BY PART_FIGURE")
            While dbReader.Read()
                dictFigureIndenture.Add(dbReader("PART_FIGURE").ToString(), Convert.ToInt32(dbReader("MAX_INDENT").ToString()))
            End While
        End Using

        'If the connection is open, do what you need to and/or close it.'
        If _dbConnection.State = ConnectionState.Open Then
            _dbConnection.Close()
        End If

        Return dictFigureIndenture
    Catch ex As Exception
        'An exception was thrown.  Show it to the user so they can report it.'
        MessageBox.Show(ex.Message)

        'If the connection is open, do what you need to and/or close it.'
        If _dbConnection.State = ConnectionState.Open Then
            _dbConnection.Close()
        End If

        Return Nothing
    End Try
End Function

他们基本上都做同样的事情,但我不确定哪个会在社区中被接受。最后我想写一些供人们使用的开源代码(大多数可能已经写过),但我绝对不希望有人认为我正在做的是最好的做法。任何人都可以给我一个外行描述,哪个更好用,为什么?对不起,如果这是一个重复的帖子给别人的,只是好奇。感谢。

3 个答案:

答案 0 :(得分:5)

这绝对应该是一种方法,而不是财产。

属性通常包含轻量级操作,例如获取私有变量的值,并且可能进行一些简单的计算或转换。从数据库中提取数据的东西肯定与通常对属性的预期不匹配。

答案 1 :(得分:1)

  

使用方法,而不是属性,   如果操作是订单   幅度比场访问慢   将会。特别是在行动中   访问网络或文件   系统,应该是方法。

您的代码访问数据库:所以它应该是一个方法。

来自。NET Framework Design Guidelines第2版第135页

答案 2 :(得分:0)

我不能保证你这是最好的做法,但我会说你感觉更自然,在现实世界中更有意义。我会说MaximumIndenture会创建一个比函数更好的属性,因为在我看来它更像是对象的属性或属于它的名词。我会使用一个函数来做更多有一些结果的操作或动词。那是我的2美分。希望它有所帮助!