This repository was archived by the owner on Feb 16, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathEventStoreTransaction.cs
More file actions
93 lines (84 loc) · 3.64 KB
/
Copy pathEventStoreTransaction.cs
File metadata and controls
93 lines (84 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EventStore.ClientAPI.Common.Utils;
using EventStore.ClientAPI.Internal;
using EventStore.ClientAPI.SystemData;
namespace EventStore.ClientAPI
{
/// <summary>
/// Represents a multi-request transaction with the Event Store
/// </summary>
public class EventStoreTransaction : IDisposable
{
/// <summary>
/// The ID of the transaction. This can be used to recover
/// a transaction later.
/// </summary>
public readonly long TransactionId;
private readonly UserCredentials _userCredentials;
private readonly IEventStoreTransactionConnection _connection;
private bool _isRolledBack;
private bool _isCommitted;
/// <summary>
/// Constructs a new <see cref="EventStoreTransaction"/>
/// </summary>
/// <param name="transactionId">The transaction id of the transaction</param>
/// <param name="userCredentials">User credentials under which transaction is committed.</param>
/// <param name="connection">The connection the transaction is hooked to</param>
internal EventStoreTransaction(long transactionId, UserCredentials userCredentials, IEventStoreTransactionConnection connection)
{
Ensure.Nonnegative(transactionId, "transactionId");
TransactionId = transactionId;
_userCredentials = userCredentials;
_connection = connection;
}
/// <summary>
/// Asynchronously commits this transaction
/// </summary>
/// <returns>A <see cref="Task"/> that returns expected version for following write requests</returns>
public Task<WriteResult> CommitAsync()
{
if (_isRolledBack) throw new InvalidOperationException("Cannot commit a rolledback transaction");
if (_isCommitted) throw new InvalidOperationException("Transaction is already committed");
_isCommitted = true;
return _connection.CommitTransactionAsync(this, _userCredentials);
}
/// <summary>
/// Writes to a transaction in the event store asynchronously
/// </summary>
/// <param name="events">The events to write</param>
/// <returns>A <see cref="Task"/> allowing the caller to control the async operation</returns>
public Task WriteAsync(params EventData[] events)
{
return WriteAsync((IEnumerable<EventData>)events);
}
/// <summary>
/// Writes to a transaction in the event store asynchronously
/// </summary>
/// <param name="events">The events to write</param>
/// <returns>A <see cref="Task"/> allowing the caller to control the async operation</returns>
public Task WriteAsync(IEnumerable<EventData> events)
{
if (_isRolledBack) throw new InvalidOperationException("Cannot write to a rolled-back transaction");
if (_isCommitted) throw new InvalidOperationException("Transaction is already committed");
return _connection.TransactionalWriteAsync(this, events);
}
/// <summary>
/// Rollsback this transaction.
/// </summary>
public void Rollback()
{
if (_isCommitted) throw new InvalidOperationException("Transaction is already committed");
_isRolledBack = true;
}
/// <summary>
/// Disposes this transaction rolling it back if not already committed
/// </summary>
public void Dispose()
{
if (!_isCommitted)
_isRolledBack = true;
}
}
}