Note that there are some explanatory texts on larger screens.

plurals
  1. PODeclarative thread safety in .NET
    text
    copied!<p>I need to make an existing app thread safe. Due circumstances (see below), I decided to use one single ReaderWriterLock for the entire graph of business objects. All methods/properties must look like these:</p> <pre><code>public int MyReadOperation(string inputParam) { rwLock.AcquireReaderLock(10000); try { // do all read operations ... } finally { rwLock.ReleaseReaderLock(); } } public void MyWriteOperation(string input) { rwLock.AcquireWriterLock(10000); try { // do all write operations ... } finally { rwLock.ReleaseWriterLock(); } } </code></pre> <p>But I have immense amount of methods to cover and I am freaked out from the idea of copy/pasting. Inspired by MethodImplAttribute, I would prefer to have a code like this while behave as the code above:</p> <pre><code>[ReadOperation] public int MyReadOperation(string inputParam) { // do all read operations ... } [WriteOperation] public void MyWriteOperation(string input) { // do all write operations ... } </code></pre> <p>Is there a way to interrupt Thread execution before/after entering into a property or a method and adding the thread-safety precautions? Or somehow utilizing the functional language features of C#, embedding the productive body of the methods into a generic ReaderWriterLock aquiring "frame"?</p> <p>A bit of background:</p> <p>I am working on a project where data carrier business objects are exposed via .NET Remoting. However, these data classes are not serializable but MarshalByRef-s. That means that ALL clients actually read/write the very same business objects. This cannot be changed, it is carved in stone. The hope for thread-safety is that these remoted business objects are read-only in the eyes of the remoting clients (thought they do loop many lists) and all write operations are nicely separated into a dedicated facade. I expect rare writes and frequent reads. The business objects are highly connected, they are very "graphy".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload