Check that all synchronization objects are only used from the namespaces under MyNamespace.Sync

Rule ND9910 Samples of Custom rules / Check Architecture

A sample rule to adapt to your need: enforces that all synchronization objects are used from a particular namespace.

How to fix

This is a sample rule, there is nothing to fix as is.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Check that all synchronization objects are only used from the namespaces under MyNamespace.Sync</Name>
// <Id>ND9910:ExplicitId9910</Id>

warnif count > 0 from n in Application.Namespaces 
  where 
    (n.IsUsing ("System.Threading.Monitor".AllowNoMatch()) ||
     n.IsUsing ("System.Threading.ReaderWriterLock".AllowNoMatch()) ||
     n.IsUsing ("System.Threading.Mutex".AllowNoMatch()) ||
     n.IsUsing ("System.Threading.EventWaitHandle".AllowNoMatch()) ||
     n.IsUsing ("System.Threading.Semaphore".AllowNoMatch()) ||
     n.IsUsing ("System.Threading.Interlocked".AllowNoMatch())) 
  && !n.NameLike (@"^MyNamespace.Sync")
select new {
   n,
   Debt = 10.ToMinutes().ToDebt(),
   Severity = Severity.High
}

//<Expl>
// The *{0}* is using a synchronization and thus, should be declared in *MyNamespace.Sync*.
//</Expl>

//<Desc>
// A *sample rule to adapt to your need*: enforces that all synchronization
// objects are used from a particular namespace.
//</Desc>

//<HowToFix>
// This is a *sample rule*, there is nothing to fix *as is*.
//</HowToFix>

Samples of Custom rules / Check Architecture: all 10 items