Search code examples
c#.netaopaopalliance

AOP can be use to share data among different running threads in application process?


UPDATE: I am not clear in what context AOP should be used? As we can use AOP for logging purpose, security(Authentication), please suggest some other scenarios where we can take benefit of AOP.

Can AOP be used to share data among different running threads in application process?


Solution

  • Aspect orientated programming is ideal when you need to reduce "cross cutting" functionality within your code base. What this means is you have common code (logging, security) that classes need to implement but you cannot abstract that functionality into base classes.

    So, AOP is really taking small these pieces of functionality and embedding them, at runtime or compile time, into your code where the "cross cutting" functionality is present.

    Resources

    Currently, AOP is not built into C# but there the following frameworks can build AOP:

    AOP for thread data

    Generally, using AOP to share data across threads is not the way to go. There are other techniques available for developers to do that:

    • [ThreadStaticAttribute] Append this attribute to fields to dictate to the .NET runtime that the following field will be unique to multiple threads

    • Synchronization (most common technique) Use Mutexes, Semaphores, ReaderWriter locks and EventWaitHandles to synchronize access to local or global data from multiple threads. In C#, the lock statement is syntactic sugar for the Monitor class, which can be used to "lock" access to an object from a single thread.