Domanda

I have been thinking of implementing an e-payment system in F#. One critical requirement is transaction management.

I used the TransactionScope with Dapper and it worked well. I could use Tranq to manage DB transactions, but would like to use something like TransactionScope to manage online operations.

Relatively new to F#, I don't know if there is anything that is equivalent to

using (var trans = new TransactionScope())
{
    ...//operations
    trans.Complete();
}

If the line-to-line translating from C# to F# approach is wrong, then is there any more "functional way" of managing transactions?

È stato utile?

Soluzione

F# has a similar using block to C#.

using (new Thing()) (fun x -> x.DoStuff ... )

But there is also a use binding in F#, which works just like let except it calls Dispose when the object goes out of scope. It's not too hard to make a new type that wraps TransactionScope and calls Complete when it's disposed if it hasn't been rolled back by code...

It would also be possible to make a workflow builder take care of transactions.

However, you might be better off using a database layer such as Entity Framework which handles your transactional needs.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top