开发者

Storing subcollections (filtered versions of the main collection) inside the collection as cache?

Is it good practice to store sub-collections of item X inside a parent collection of item X for caching 'filter-queries开发者_开发技巧' which are performed on the parent collection? (They won't be used together (as in color AND type).) Or is it also ok to just Loop over the collection and return the correct entities in a temporary collection?

Class ItemCollection : Inherits Collection(Of Item)
    Private _types as New List(Of ItemCollection) //Cache

    Function FilterByType(type) As ItemCollection
        If Not _types.KeyExists(type) Then
            _types.Add(type, New ItemCollection())
            For Each i in Me
                If i.Type = type Then _types(type).Add(i)
            Next
        End If
        Return _types(type)
    End Function

    //Same for FilterByColor(color)
End Class

Class Item
    Public Color = "Blue"
    Public [Type] = "One"
End Class


I would recommend keeping it simple to start with and then add caching if testing shows that regenerating the filtered lists is a performance problem. Besides that, the filtering code can be greatly simplified by using filtering methods built into List or using LINQ extensions.

filtered = Me.Where(i => i.Type = type)

(I could be off on the exact syntax, I've never done LINQ in VB.NET, I'm a C# guy.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜