Static constructors and resources

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wavemaker

    #1

    Static constructors and resources

    I'm writing a class whose methods can throw exceptions under certain
    circumstances. These exceptions will need string messages. I was
    thinking about placing these messages in a resource instead of hard
    coding them into the class itself.

    The question I'm pondering is where would be the best place to load
    these string resources? The string messages will be the same for
    every instance of the class, so I was thinking about loading the
    resource in a static constructor, retrieving the messages, and
    storing them in class fields. This prevents the resources from being
    repeatedly loaded each time a new instance of the class is
    instantiated.

    Are there any disadvantages to this approach? The only problem I can
    think of is dealing with a situation in which the culture is changed
    at some point during runtime. The resources would need to be
    reloaded at that point. Admittedly, at this point, I am only
    supporting one culture, so this may not be an issue, but this could
    change in the future.


  • codewriter

    #2
    Re: Static constructors and resources

    Another approach, that I think is a bit more flexible, is to use an XML file
    and store you error messages in it. In this case, you will be able to change
    your messages without using resources by simply altering your XML file
    though a notepad or other editor. Also, you can create several XML files for
    each culture, you may check in your code which culture it is and name XML
    file accordingly. In this case you will not need to recompile your code, but
    load an XML file on the fly.

    "Wavemaker" <jabberdabber@N OSPAMhotmail.co m> wrote in message
    news:SFhZa.1085 46$uu5.15570@sc crnsc04...[color=blue]
    > I'm writing a class whose methods can throw exceptions under certain
    > circumstances. These exceptions will need string messages. I was
    > thinking about placing these messages in a resource instead of hard
    > coding them into the class itself.
    >
    > The question I'm pondering is where would be the best place to load
    > these string resources? The string messages will be the same for
    > every instance of the class, so I was thinking about loading the
    > resource in a static constructor, retrieving the messages, and
    > storing them in class fields. This prevents the resources from being
    > repeatedly loaded each time a new instance of the class is
    > instantiated.
    >
    > Are there any disadvantages to this approach? The only problem I can
    > think of is dealing with a situation in which the culture is changed
    > at some point during runtime. The resources would need to be
    > reloaded at that point. Admittedly, at this point, I am only
    > supporting one culture, so this may not be an issue, but this could
    > change in the future.
    >
    >[/color]


    Comment

    • Michael Mayer

      #3
      Re: Static constructors and resources

      I like the XML idea, but look also at ResourceManager and GetString.
      This may be what you had in mind in the original post. I would just
      run the GetString() function as you need it instead of putting it in a
      static constructor. Especially if it's only needed for exceptions
      (which hopefully aren't occurring very often). Note that GetString is
      overloaded to handle diferent CultureInfo's.

      # String Table text file
      Message1 = This was an error.
      Message2 = This was an exceptional error.

      then add the file to the assembly as a resource (see online help for
      more info). The following lines of code will read it out (assuming
      the resource is named StringTable):

      ResourceManager stringResources = new ResourceManager ("StringTabl e",
      Assembly.GetExe cutingAssembly( ));
      string msg = stringResources .GetString("Mes sage1");

      -mike

      "codewriter " <code_writer_20 03@yahoo.com> wrote in message
      news:0OiZa.9856 $pq5.1293045@ne ws20.bellglobal .com...[color=blue]
      > Another approach, that I think is a bit more flexible, is to use an[/color]
      XML file[color=blue]
      > and store you error messages in it. In this case, you will be able[/color]
      to change[color=blue]
      > your messages without using resources by simply altering your XML[/color]
      file[color=blue]
      > though a notepad or other editor. Also, you can create several XML[/color]
      files for[color=blue]
      > each culture, you may check in your code which culture it is and[/color]
      name XML[color=blue]
      > file accordingly. In this case you will not need to recompile your[/color]
      code, but[color=blue]
      > load an XML file on the fly.
      >
      > "Wavemaker" <jabberdabber@N OSPAMhotmail.co m> wrote in message
      > news:SFhZa.1085 46$uu5.15570@sc crnsc04...[color=green]
      > > I'm writing a class whose methods can throw exceptions under[/color][/color]
      certain[color=blue][color=green]
      > > circumstances. These exceptions will need string messages. I was
      > > thinking about placing these messages in a resource instead of[/color][/color]
      hard[color=blue][color=green]
      > > coding them into the class itself.
      > >
      > > The question I'm pondering is where would be the best place to[/color][/color]
      load[color=blue][color=green]
      > > these string resources? The string messages will be the same for
      > > every instance of the class, so I was thinking about loading the
      > > resource in a static constructor, retrieving the messages, and
      > > storing them in class fields. This prevents the resources from[/color][/color]
      being[color=blue][color=green]
      > > repeatedly loaded each time a new instance of the class is
      > > instantiated.
      > >
      > > Are there any disadvantages to this approach? The only problem I[/color][/color]
      can[color=blue][color=green]
      > > think of is dealing with a situation in which the culture is[/color][/color]
      changed[color=blue][color=green]
      > > at some point during runtime. The resources would need to be
      > > reloaded at that point. Admittedly, at this point, I am only
      > > supporting one culture, so this may not be an issue, but this[/color][/color]
      could[color=blue][color=green]
      > > change in the future.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Wavemaker

        #4
        Re: Static constructors and resources


        First of all, thanks to you and codewriter for your reply.

        "Michael Mayer" wrote:[color=blue]
        > I like the XML idea, but look also at ResourceManager and
        > GetString. This may be what you had in mind in the
        > original post. I would just run the GetString() function
        > as you need it instead of putting it in a
        > static constructor. Especially if it's only needed for exceptions
        > (which hopefully aren't occurring very often).[/color]

        I will investigate the XML idea, but the ResourceManager route is
        the way I was thinking about doing it. I was just wondering how
        intensive it is to load the resource and then extract the string
        message. Since it's, as you've pointed out, only for use with
        exceptions, which shouldn't occur very often, maybe this shouldn't
        be of any concern to me.


        Comment

        Working...