Is it possible to define your own ambient classes?

For example, the TransactionScope class makes itself available to any object which implements IEnlistmentNotification (I think), without any code to explicitly pass a reference of one object to the other.

I'm not sure if I'd ever need to use this, but it would be interesting to understand how it worked (i.e. how I could implement such functionality in my own code). Is this something we can build into our own classes, or was something developed at a lower level to allow TransactionScope to do something beyond what's available at the "regular developer" level?

有帮助吗?

解决方案

Writing new TransactionScope() just sets the static Transaction.Current property.

Other classes can check whether this property is not null and enlist themselves in the transaction.

There is no magic involved.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top