开发者

What is the best practice for ReadOnly Properties/Functions in VB.Net?

I'm not too new to VB.Net and the syntax, but I'm not an expert. I'm working on rewriting something in VB that was previously in C# and while I was doing so, I came across a "dilemna." I can have a ReadOnly Property:

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, 开发者_StackOverflowdo 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

And I can have a function that does the same thing:

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

They both essentially do the same thing, but I'm not sure which would be more accepted in the community. Eventually I'd like to write a lot of open source code for people to use (most has probably already been written) but I definitely don't want someone to think what I'm doing is the best practice. Can anyone give me a laymen description as to which is better to use and why? Sorry if this is a duplicate post to someone else's, just curious. Thanks.


That should definitely be a method, not a property.

A property generally contains a light weight operation, like getting the value of a private variable and perhaps do some simple calculation or conversion. Something that pulls data from a database does definitely not match what's generally expected of a property.


Use a method, rather than a property, if the operations is orders of magnitude slower than a field access would be. In particular operations that access the network, or a file system, should likely be methods.

Your code accesses a database: so it should be a method.

From the .NET Framework Design Guidelines 2nd Edition page 135


I can't promise you this is the best practice but I would say go with what feels more natural to you and makes more sense in the real world. I would say MaximumIndenture would make a better property than a function because it seems to me it is more of property of the object or a noun that belongs to it. I would use a function to do more of an opperation or verb that has some result. Those are my 2 cents. Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜