Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Easy use of Caching

    Steve Smith blogs about the "standard" way of accessing items in the cache.

    I use this all the time.  I usually do it like this so that you can access it like it as  Property (but in VB.NET of course  ;))

    Public Shared ReadOnly Property MyData() As DataTable
        
    Get
             
    Dim Temp As DataTable = DirectCast
    (HttpContext.Current.Cache("MyData"), DataTable)

              If Temp Is Nothing
    Then
                  
    Temp = GetData()
                   HttpContext.Current.Cache.Insert("MyData", Temp, Nothing
    , DateTime.Now.AddHours(1), TimeSpan.Zero)
              End
    If

             
    Return
    Temp
         End
    Get
    End Property

    You could probably setup a macro to whip out these things pretty fast.  This concept of encapsulating things into Properties also works great on appSettings items.

    Public Shared ReadOnly Property MyProperty() As String
        
    Get
             
    Return
    ConfigurationSettings.AppSettings("MyProperty")
         End
    Get
    End Property

    It might be kind of cool too, to see someone create a plug-in for Visual Studio .NET that would generate a class with a bunch of shared methods to reflect the settings in the web.config (or app.config) file every time you build your project.  A new PowerToy?  Maybe integrate it so it generates a "proxy class" for resources too?  Anybody up for the challenge?  ;)

    Comments

    nidhogg at free dot fr said:

    Been there, done that. Check out my weblog, I posted something that is really close to what you described. (http://www.stup.org/PermaLink.aspx/6f23603b-04d4-4b1c-8c8c-0d06e1fb3d10) It generates a strongly typed class that accesses settings in a configuration file. Each time the file is modified, the class is regenerated. I used a custom settings file because I wanted it to be read/write and not interfere with the existing .config related classes but the functionnality is just what you described. And I thought about doing the same for resources too :)
    # July 1, 2003 6:52 AM

    nidhogg@free.fr said:

    Found this while browsing codeguru. http://www.codeguru.com/cs_misc/GenResourceKeys.html. This guy implemented a custom tool that generates a class to access resources.
    # July 4, 2003 11:14 AM

    HumanCompiler said:

    Thanks for the links! :)
    # July 4, 2003 12:32 PM