Transactions and roll back of data form the core feature of smartcards. These features implied that if tearing or exception occurs during execution of method which was under a transaction then all the modifications made to the application data are discarded or contents should be rolled back to old values. This is all good and fine but sometimes need arise when you would want a particular method to be under transaction & some field/value to be updated irrespective of the fact that tearing occurred or not. The one famous example is that of PIN verification and try counter. PIN verification is under transaction and since this method plays with the try counter decrementation you want that try counter updates to be out of transaction else if card is pulled out during PIN verification, try counter won't decrement and thereby giving the hacker infinite chances to break your card. Mostly (I hope it should be the implementation in other vendor cards) PIN specific libraries take care of this.
In .NET SmartCard we expose this feature ie Out of Transaction counters as an API as we feel that there would be many places where such kind of counters will be required. The class is called TryCounter and have a look at its usage.
public class MyClass
{
// counter which should be out of
// transaction
TryCounter _myOutOfTranCounter;
MyClass(){
// 0 here is the initial value
_myOutOfTranCounter = new TryCounter(0);
}
// MyMethod which is under transaction
[Transaction]
void MyMethod(int paramA,int paramB){
// Update the counter reflecting how
// many times i am called whether or
// not tearing occurs during execution
myOutOfTranCounter.Value++;
}
}
Other methods/properties available in this class are :
Reset() : Reset the counter value to the initial value (value specified as param to the TryCounter ctor).
InitialValue [property] : Get or Set the initial value of the TryCounter.