Troy,
> Greco Giuseppe wrote:
> > There are also performance issues; actually, the code
> > for getting a localized string is as following:
> >
> > public static string GetString(string name, CultureInfo culture)
> > {
> > if (resourceManager == null) {
> > lock (typeof(ResourceUtils)) {
> > if (resourceManager == null) {
> > Assembly assembly = Assembly.GetCallingAssembly();
> > resourceManager = new ResourceManager(
> > assembly.GetName().Name, assembly);
> > }
> > }
> > }
> >
> > return resourceManager.GetString(name, culture);
> > }
> >
> > In the code above, the lock takes place only once (the first
> > time a string is retrieved), assuring good performance.
>
> In the code above, there are unfortunately also correctness issues.
>
> Ref:
> http://weblogs.asp.net/oldnewthing/archive/2004/05/28/143769.aspx
> http://www.nwcpp.org/Downloads/2004/DCLP_notes.pdf
>
> The article on this page seems to imply that DCL is fixed in .Net,
but
> they still recommend using simple static initialisation.
> http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnbda/html/singletondespatt.asp
>
On .NET the code above just works fine. What's important is to define
resourceManager as following:
private static volatile ResourceManager resourceManager;
Honestly, the only thing I forgot in the original source code was
the "volatile" keyword.
> > Adding the functionality to register additional assemblies,
> > the lock would take place always as demonstrated in the following
> > code chunk:
> >
> > static ResourceUtils()
> > {
> > resourceManagerDictionary = new ResourceManagerDictionary();
> > }
> >
> > public static void RegisterAssembly(Assembly assembly)
> > {
> > lock (resourceManagerDictionary) {
> > resourceManagerDictionary.Add(assembly.GetName().Name,
> > new ResourceManager(assembly.GetName().Name, assembly))
> > }
> > }
> >
> > public static string GetString(string name, CultureInfo culture)
> > {
> > string localizedString = null;
> > ResourceManager resourceManager = null;
> >
> > lock (resourceManagerDictionary) {
> > foreach (DictionaryEntry entry in resourceManagerDictionary) {
> > resourceManager = entry.Value as ResourceManager;
> > localizedString = resourceManager.GetString(name, culture);
> >
> > if (localizedString != null) {
> > break;
> > }
> > }
> > }
> >
> > return localizedString;
> > }
> >
> > The methods RegisterAssembly() and GetString() must be kept
> > synchronized...
> >
> > What do you think about?
>
> Why doesn't GetString in this section restrict itself to the
> ResourceManager for the calling assembly?
Different NAnt assemblies could use the same error/warning
messages and maintaining them consistent might be a hell.
>
>
> On an unrelated note, is there any reason you use a static
initialiser
> block instead of inline initialisation of your static member?
You are right, I could use an inline initialization as well,
but I prefer the static initializer... it's just a style sake.
j3d.
>
>
> Regards,
>
> -- Troy
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real
users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
> _______________________________________________
> nant-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/nant-developers
--
----------------------------------------
Giuseppe Greco
::agamura::
phone: +41 (0)91 604 67 65
mobile: +41 (0)79 602 99 27
email: [EMAIL PROTECTED]
web: www.agamura.com
----------------------------------------
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
nant-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nant-developers