Why not use static/shared methods?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Re: Why not use static/shared methods?

    I'm a fan of static functions. I think they are particularly well suited in
    the case of asp.net where the general lifetime of objects is a single page
    view (except for objects placed in the viewstate, session, application
    scope). I think in the case of your DAL, this is particularly true.

    A couple points:
    -If you look at IBuySpy (microsoft's best approach guideline), you'll see
    that their DAL is not implemented using static methods -though there has
    been dicussion on this, and I've always seen a majority of people agree that
    static methods would be ideal.
    -It should be noted, as far as I know, vb.net methods are limited to 32
    parameters...this could prove to be a problem with your shared methods,
    whereas with an instance of an object you could set well over 32 properties
    (this maybe a scenario for advanced searches?).
    -Static methods have less overhead and are quicker

    I'd do it using static methods...the person actually writing our DAL has
    decided to use instance methods...i think the fact that we don't have a
    standard is a bigger problem than doing it one way vs another.

    Karl



    <DanR@REMOVETHISTOGETTOME-warshawgroup.com> wrote in message
    news:uitzjsIRDHA.2676@TK2MSFTNGP10.phx.gbl...
    > When I implement my DB classes/DAL layer, why shouldn't I make the methods
    > (in VB) 'shared'?
    > Forget the other questions I have right now about whether to put in that
    > finally block, etc. Why not make the function shared? Why bother have the
    > class be instatiated and then the method invoked- this method could be
    > shared, it really has no state, right?
    > -Dan R
    >
    >
    > For example, let's say I've got the following method of the DAL class
    > (you've seen something like this)
    >
    > Public Function GetSqlDataReader(ByVal strSQL As String) As SqlDataReader
    > Dim con As SqlConnection = New
    SqlConnection(DBAccess.SQL_CONN_STR)
    > Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
    > Dim dr As SqlDataReader
    > Try
    > con.Open()
    > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) 'We do
    > this so that a .close on the SqlDataReader will close the connection.
    > Return dr
    > Catch ex As Exception
    > Throw ex 'Rethrows it. I don't need to put the ex, I don't
    > think, incidentally
    > Finally 'From
    > [url]http://www.dotnet247.com/247reference/msgs/8/40569.aspx[/url] is this overkill?
    > If (Not dr Is Nothing) Then 'It was left open
    > dr.Close()
    > End If
    > End Try
    > End Function
    >
    >

    Karl Seguin Guest

  2. Similar Questions and Discussions

    1. #39713 [NEW]: Static variables defined in static methods duplicated in derived classes
      From: paul at digitalbacon dot us Operating system: Linux, Mac OS X 10.4, Windows XP PHP version: 5.2.0 PHP Bug Type: ...
    2. #39177 [NEW]: Need mechanism to identifed calling class in static methods
      From: acarheden at gmail dot com Operating system: Windows 2K3 Server PHP version: 5.1.6 PHP Bug Type: Feature/Change Request...
    3. #39048 [NEW]: Static variables and instantiated classes. private static doesn't work.
      From: matti at nitro dot fi Operating system: * PHP version: 5.1.6 PHP Bug Type: Scripting Engine problem Bug description: ...
    4. #38783 [NEW]: Call to non-static as static E_STRICT thrown when error reporting set to E_ALL
      From: dmb27 at cornell dot edu Operating system: Redhat AS 4 PHP version: 5.1.6 PHP Bug Type: Class/Object related Bug...
    5. Static shared data connection provider?
      I like the drag-and-drop accessibility of dragging a table to a Web Forms designer and seeing a SqlDataAdapter automatically created for me.. being...
  3. #2

    Default Re: Why not use static/shared methods in my DAL?

    I was worried about threading issues if the class had static methods- that
    two different ASP sessions could access the same static class member. That's
    a bit confusing to me- I wasn't sure if I would have to use a mutex or
    something in Static methods to ensure their thread safety. Can anyone who
    knows what I'm talking about tell me what it is that I'm talking about? :)

    "Karl Seguin" <kseguin##crea.ca> wrote in message
    news:OReGV3IRDHA.2424@tk2msftngp13.phx.gbl...
    > I'm a fan of static functions. I think they are particularly well suited
    in
    > the case of asp.net where the general lifetime of objects is a single page
    > view (except for objects placed in the viewstate, session, application
    > scope). I think in the case of your DAL, this is particularly true.
    >
    > A couple points:
    > -If you look at IBuySpy (microsoft's best approach guideline), you'll see
    > that their DAL is not implemented using static methods -though there has
    > been dicussion on this, and I've always seen a majority of people agree
    that
    > static methods would be ideal.
    > -It should be noted, as far as I know, vb.net methods are limited to 32
    > parameters...this could prove to be a problem with your shared methods,
    > whereas with an instance of an object you could set well over 32
    properties
    > (this maybe a scenario for advanced searches?).
    > -Static methods have less overhead and are quicker
    >
    > I'd do it using static methods...the person actually writing our DAL has
    > decided to use instance methods...i think the fact that we don't have a
    > standard is a bigger problem than doing it one way vs another.
    >
    > Karl
    >
    >
    >
    > <DanR@REMOVETHISTOGETTOME-warshawgroup.com> wrote in message
    > news:uitzjsIRDHA.2676@TK2MSFTNGP10.phx.gbl...
    > > When I implement my DB classes/DAL layer, why shouldn't I make the
    methods
    > > (in VB) 'shared'?
    > > Forget the other questions I have right now about whether to put in
    that
    > > finally block, etc. Why not make the function shared? Why bother have
    the
    > > class be instatiated and then the method invoked- this method could be
    > > shared, it really has no state, right?
    > > -Dan R
    > >
    > >
    > > For example, let's say I've got the following method of the DAL class
    > > (you've seen something like this)
    > >
    > > Public Function GetSqlDataReader(ByVal strSQL As String) As
    SqlDataReader
    > > Dim con As SqlConnection = New
    > SqlConnection(DBAccess.SQL_CONN_STR)
    > > Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
    > > Dim dr As SqlDataReader
    > > Try
    > > con.Open()
    > > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) 'We
    do
    > > this so that a .close on the SqlDataReader will close the connection.
    > > Return dr
    > > Catch ex As Exception
    > > Throw ex 'Rethrows it. I don't need to put the ex, I don't
    > > think, incidentally
    > > Finally 'From
    > > [url]http://www.dotnet247.com/247reference/msgs/8/40569.aspx[/url] is this
    overkill?
    > > If (Not dr Is Nothing) Then 'It was left open
    > > dr.Close()
    > > End If
    > > End Try
    > > End Function
    > >
    > >
    >
    >

    Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139