-
Notifications
You must be signed in to change notification settings - Fork 14
/
RfcContext.cs
61 lines (46 loc) · 2 KB
/
RfcContext.cs
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
using System;
using System.Threading.Tasks;
using LanguageExt;
namespace Contiva.SAP.NWRfc
{
public class RfcContext : IRfcContext
{
private readonly Func<Task<Either<RfcErrorInfo, IConnection>>> _connectionBuilder;
private Option<IConnection> _connection;
public RfcContext(Func<Task<Either<RfcErrorInfo, IConnection>>> connectionBuilder)
{
_connectionBuilder = connectionBuilder;
}
private Task<Either<RfcErrorInfo, IConnection>> GetConnection()
{
return _connection.MatchAsync(s => Prelude.Right(s),
async () =>
{
var res = await _connectionBuilder();
res.Map(connection => _connection = Prelude.Some(connection));
return res;
});
}
public Task<Either<RfcErrorInfo, Unit>> InvokeFunction(IFunction function)
{
return GetConnection().BindAsync(conn => conn.InvokeFunction(function));
}
public Task<Either<RfcErrorInfo, IRfcContext>> Ping()
{
return CreateFunction("RFC_PING")
.BindAsync(InvokeFunction)
.MapAsync(r => (IRfcContext) this );
}
public Task<Either<RfcErrorInfo, IFunction>> CreateFunction(string name) => GetConnection().BindAsync(conn => conn.CreateFunction(name));
public Task<Either<RfcErrorInfo, Unit>> Commit() => GetConnection().BindAsync(conn => conn.Commit());
public Task<Either<RfcErrorInfo, Unit>> CommitAndWait() => GetConnection().BindAsync(conn => conn.CommitAndWait());
public Task<Either<RfcErrorInfo, Unit>> Rollback() => GetConnection().BindAsync(conn => conn.Rollback());
public void Dispose()
{
if (_connection.IsSome)
_connection.Map(conn => conn.Rollback().GetAwaiter().GetResult());
_connection.IfSome(conn => conn.Dispose());
_connection = Prelude.None;
}
}
}