Coding Conventions

From Agility
Revision as of 11:51, 24 January 2017 by Sychu (Talk | contribs) (Multithreading)

Jump to: navigation, search


Multithreading

Critical sections should be small and predictable

To avoid dead-locks critical sections should be small enough to prove that dead lock is not possible. Critical section should not cover any complicated business task with access to database (for example business action).

Please AVOID below construction:

lock(typeof(TranslateBO))
{
    //Anti-pattern do not put complicated business functions (like get data by key) inside critical section
    transBO.getDataByKey(phraseKey);

    result = transBO.detailData.syPhrase.Find(phraseKey);
    [...]
}

In above example getDataByKey function is used inside critical section. It is NOT possible to prove that this function placed in critical section will never fall into dead-lock. Above example should be fixed with:

transBO.getDataByKey(phraseKey);
lock(typeof(TranslateBO))
{
    result = transBO.detailData.syPhrase.Find(phraseKey);
    [...]
}


Ensure using thread-safe data containers

DataSets are not thread safe. If dataset is shared between threads and there is action which update dataset than any access to such dataset must be synchronized.

static TranslateDataSet translateCache = new TranslateDataSet()

function void updateCache(DataKey phraseKey, string phrase)
{
    lock(typeof(TranslateBO))
    {
        var phraseRow = translateCache.syPhrase.Find(phraseKey);
        if(phraseRow != null)
        {
             phraseRow.Phrase = phrase;
        }
    }

}

If data set is rarely updated than it make sense to use ReaderWriterLockSlim instead standard lock. For example ReaderWriterLockSlim allows to create sections winch allow parallel reading, but when it came to update than parallel reading will be blocked until section with write access is finished. Please follow ReaderWriteLockSlim Reference for details.

FastNET framework rules

Use translate only for messages intended for end user

Initially we had rule that any string literal should be translated, but it is possible that occasionally translation of internal exception can be more problematic than exception itself. Starting from v8.0 system is more resistant to fail due to translate, but applying above rules will improve system reliability still.

Translation rules:

  1. Translate only messages which are intended to display on user screen
  2. There is no need to translate exceptions which are written only to system event log
  3. Translate should not be used in core functionalities which initialise whole system. At the moment forbidden is use translate in MetaData.cs