更有效/首选的使用会话变量的方式?

时间:2022-10-17 23:40:21

I have an ASP DataGrid and I'm applying sorting to it. Well, as I was looking at an example, they had a function similar in function, different in name, to:

我有一个ASP数据网格,我正在对它进行排序。在我看一个例子的时候,他们有一个功能相似的函数,不同的名字,

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand
    Dim strSortDirection As String = Session("SortDir")
    If strSortDirection = Nothing Then
        strSortDirection = " ASC "
    Else
        If strSortDirection = " ASC " Then
            strSortDirection = " DESC "
        Else
            strSortDirection = " ASC "
        End If
    End If
    Session("SortDir") = strSortDirection
    BindData(e.SortExpression & strSortDirection)
End Sub

Well, me trying to make shortcuts and make things "easier" thought maybe this would be best:

嗯,我试着走捷径,让事情变得“简单”,也许这是最好的:

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand
    If Session("SortDir") = Nothing Then
        Session("SortDir") = " ASC "
    Else
        If Session("SortDir") = " ASC " Then
            Session("SortDir") = " DESC "
        Else
            Session("SortDir") = " ASC "
        End If
    End If
    BindData(e.SortExpression & Session("SortDir"))
End Sub

However, as I was thinking about it, I figured maybe Session("SortDir") has to make a request everytime and it could have some affect or drawbacks. But I wasn't sure. Does anyone have any links that would explain the best or preferred method. Thanks.

然而,当我思考它的时候,我认为会话(“SortDir”)可能每次都必须发出一个请求,它可能有一些影响或缺陷。但我不确定。是否有人有任何链接可以解释最好的或首选的方法。谢谢。

3 个解决方案

#1


1  

Looking up the Session value twice (one read and one write) rather than four times seems clearly better. The performance difference won’t be noticable by the user, but all those redundant lookups would make most programmers very uncomfortable! And there are some occasions where this kind of thing could make a noticable difference, depeding on the amount of data stored in the collection, and the type of lookup performed (hash table, binary search, sequential search, etc.), so it’s probably not a good habit to get into.

查找会话值两次(一次读一次写)而不是四次看起来明显更好。性能差异不会被用户注意到,但是所有这些冗余查找会让大多数程序员感到非常不舒服!在某些情况下,这类东西可能会产生显著的差异,减少存储在集合中的数据量,以及执行的查找类型(哈希表、二进制搜索、顺序搜索等),因此可能不是个好习惯。

#2


1  

This is not a good use of Session in this scenario simply because there are better ways to do it. Sessions should contain specific data about the end user in some way.

在这个场景中,这并不是很好地使用会话,只是因为有更好的方法。会话应该以某种方式包含关于最终用户的特定数据。

Try something along these lines

试试这些方法

Private Property _SortDir As Nullable(Of String) ''# I used "Nullable(Of" because I don't know if you are able to use "String?"
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir.IsNullOrEmpty Or SortDir = "DESC" Then
        SortDir = "ASC"
    Else
        SortDir = "DESC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

I did think of one other reason you might want to retain the sortOrder in a session, however it would have some nasty side affects. If you want to leave the current page and come back later with the same sort order, then leave it in a Session... BUT, what if you have another grid elsewhere on the site, are you going to have a different session variable for it? or are you going to retain your existing sort order on the new grid? These are all things you need to think about when using Session variables.

我确实想到了另一个原因,你可能想在一个会话中保留sortOrder,但是它会有一些不好的副作用。如果您想要离开当前页面并稍后以相同的排序顺序返回,那么请将其保留在会话中……但是,如果你在网站的其他地方有另一个网格,你会有一个不同的会话变量吗?还是在新的网格上保留现有的排序顺序?这些都是在使用会话变量时需要考虑的问题。

If you're staying on the existing page, then use the items that are at your disposal for that page. Heck, you "could" use the _VIEWSTATE to store the sort order as well (though I wouldn't recommend it).

如果您停留在现有页面上,那么请使用该页面中您可以使用的项。见鬼,您“可以”使用_VIEWSTATE来存储排序顺序(尽管我不推荐它)。

Remember, the web is designed to be stateless, so manufacturing a "state" is a little undesirable.

请记住,web设计为无状态,因此制造“状态”有点不可取。

Lastly.

最后。

Another option for you would be to put the sort order in your querystring and forget about everything else (doesn't totally work with AJAX).

另一种选择是将排序顺序放在querystring中,而忘记其他所有东西(不完全使用AJAX)。

#3


0  

Since you want session-level persistence, I'd add two small changes to rockinthesixstring's suggestion:

由于您希望保持会话级别的持久性,所以我将在rockinxstring的建议中添加两个小更改:

(Warning: C# coder trying to write VB!)

(警告:c#程序员试图编写VB!)

Private Property _SortDir As Nullable(Of String)
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        Session("SortDir") = value
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir = "ASC" Then    'I think the outer If block was redundant''
        SortDir = "DESC"
    Else
        SortDir = "ASC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        _SortDir = Session("SortDir")
    End If
End Sub

This code is mostly C&P'd from rockinthesixstring, so if you use this answer, please either accept or upvote his.

这段代码主要是来自rockinxyxstring的C&P,所以如果您使用这个答案,请接受或向上投他的票。

#1


1  

Looking up the Session value twice (one read and one write) rather than four times seems clearly better. The performance difference won’t be noticable by the user, but all those redundant lookups would make most programmers very uncomfortable! And there are some occasions where this kind of thing could make a noticable difference, depeding on the amount of data stored in the collection, and the type of lookup performed (hash table, binary search, sequential search, etc.), so it’s probably not a good habit to get into.

查找会话值两次(一次读一次写)而不是四次看起来明显更好。性能差异不会被用户注意到,但是所有这些冗余查找会让大多数程序员感到非常不舒服!在某些情况下,这类东西可能会产生显著的差异,减少存储在集合中的数据量,以及执行的查找类型(哈希表、二进制搜索、顺序搜索等),因此可能不是个好习惯。

#2


1  

This is not a good use of Session in this scenario simply because there are better ways to do it. Sessions should contain specific data about the end user in some way.

在这个场景中,这并不是很好地使用会话,只是因为有更好的方法。会话应该以某种方式包含关于最终用户的特定数据。

Try something along these lines

试试这些方法

Private Property _SortDir As Nullable(Of String) ''# I used "Nullable(Of" because I don't know if you are able to use "String?"
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir.IsNullOrEmpty Or SortDir = "DESC" Then
        SortDir = "ASC"
    Else
        SortDir = "DESC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

I did think of one other reason you might want to retain the sortOrder in a session, however it would have some nasty side affects. If you want to leave the current page and come back later with the same sort order, then leave it in a Session... BUT, what if you have another grid elsewhere on the site, are you going to have a different session variable for it? or are you going to retain your existing sort order on the new grid? These are all things you need to think about when using Session variables.

我确实想到了另一个原因,你可能想在一个会话中保留sortOrder,但是它会有一些不好的副作用。如果您想要离开当前页面并稍后以相同的排序顺序返回,那么请将其保留在会话中……但是,如果你在网站的其他地方有另一个网格,你会有一个不同的会话变量吗?还是在新的网格上保留现有的排序顺序?这些都是在使用会话变量时需要考虑的问题。

If you're staying on the existing page, then use the items that are at your disposal for that page. Heck, you "could" use the _VIEWSTATE to store the sort order as well (though I wouldn't recommend it).

如果您停留在现有页面上,那么请使用该页面中您可以使用的项。见鬼,您“可以”使用_VIEWSTATE来存储排序顺序(尽管我不推荐它)。

Remember, the web is designed to be stateless, so manufacturing a "state" is a little undesirable.

请记住,web设计为无状态,因此制造“状态”有点不可取。

Lastly.

最后。

Another option for you would be to put the sort order in your querystring and forget about everything else (doesn't totally work with AJAX).

另一种选择是将排序顺序放在querystring中,而忘记其他所有东西(不完全使用AJAX)。

#3


0  

Since you want session-level persistence, I'd add two small changes to rockinthesixstring's suggestion:

由于您希望保持会话级别的持久性,所以我将在rockinxstring的建议中添加两个小更改:

(Warning: C# coder trying to write VB!)

(警告:c#程序员试图编写VB!)

Private Property _SortDir As Nullable(Of String)
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        Session("SortDir") = value
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir = "ASC" Then    'I think the outer If block was redundant''
        SortDir = "DESC"
    Else
        SortDir = "ASC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        _SortDir = Session("SortDir")
    End If
End Sub

This code is mostly C&P'd from rockinthesixstring, so if you use this answer, please either accept or upvote his.

这段代码主要是来自rockinxyxstring的C&P,所以如果您使用这个答案,请接受或向上投他的票。