VitaliiTsilnyk/NGettext

Use ConcurrentDictionary instead of Dictionary in Catalog?

Closed this issue · 2 comments

hmqgg commented

The Translation in Catalog.cs is Dictionary<string, string[]>, can we use ConcurrentDicitionary instead in order to make it thread safe to get translation?

public Dictionary<string, string[]> Translations { get; protected set; }

public virtual string[] GetTranslations(string messageId)
{
if (!this.IsTranslationExist(messageId)) return null;
return this.Translations[messageId];
}

In the current implementation Catalog is thread safe if you don't modify the Translations dictionary manually but use the loader in the constructor (and also you should use the ICatalog interface everywhere as it only exposes the translation methods).
Read operations of Dictionary are thread safe unless you concurrently write into it.
I understand that exposing a non thread safe property makes the current implementation unsafe, it has been done to keep backwards compatibility with older versions of the library and in version 1.0 I'm planning to implement the loaders properly without exposing internal stuff.

hmqgg commented

Thanks for your clarification. I noticed that some targeted frameworks don't support ConcurrentDictionary so I can understand why not simply change it.

And I agreed with that ILoader implementation needs to rewrite in order to avoid exposing Translations.

Issue closed.