forked from Alachisoft/NCache-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteThruProvider.cs
More file actions
144 lines (127 loc) · 6.17 KB
/
WriteThruProvider.cs
File metadata and controls
144 lines (127 loc) · 6.17 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Alachisoft.NCache.Runtime.DatasourceProviders;
using Alachisoft.NCache.Samples.Dapper.Models;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Alachisoft.NCache.Samples.Dapper.BackingSources
{
public class WriteThruProvider : IWriteThruProvider
{
public SqlConnection Connection { get; set; }
public void Dispose()
{
if (Connection != null)
{
Connection.Close();
Connection.Dispose();
}
}
public void Init(
IDictionary parameters,
string cacheId)
{
var connectionString = parameters["connectionString"] as string;
Connection = new SqlConnection(connectionString);
Connection.Open();
}
public OperationResult WriteToDataSource(
WriteOperation operation)
{
var operationResult =
new OperationResult(
operation,
OperationResult.Status.Failure);
int rowsAffected = 0;
Customer customer = null;
if (operation.OperationType == WriteOperationType.Add ||
operation.OperationType == WriteOperationType.Update)
{
customer = operation.ProviderItem.GetValue<Customer>();
}
if (operation.OperationType == WriteOperationType.Add)
{
var commandDefinition = new CommandDefinition(
@"INSERT INTO dbo.Customers (
CustomerID,
CompanyName,
ContactName,
ContactTitle,
Address,
City,
Region,
PostalCode,
Country,
Phone,
Fax)
VALUES (
@CustomerID,
@CompanyName,
@ContactName,
@ContactTitle,
@Address,
@City,
@Region,
@PostalCode,
@Country,
@Phone,
@Fax);",
customer,
flags: CommandFlags.NoCache);
rowsAffected = Connection.Execute(commandDefinition);
}
else if (operation.OperationType == WriteOperationType.Update)
{
var commandDefinition = new CommandDefinition(
@"UPDATE dbo.Customers
SET CompanyName = @CompanyName,
ContactName = @ContactName,
ContactTitle = @ContactTitle,
Address = @Address,
City = @City,
Region = @Region,
PostalCode = @PostalCode,
Country = @Country,
Phone = @Phone,
Fax = @Fax
WHERE CustomerID = @CustomerID;",
customer,
flags: CommandFlags.NoCache);
rowsAffected = Connection.Execute(
commandDefinition);
}
else if (operation.OperationType == WriteOperationType.Delete)
{
var customerId =
operation.Key.Replace("Customer:CustomerID:", "").Trim();
var commandDefinition = new CommandDefinition(
"DELETE FROM dbo.Customers " +
"WHERE CustomerID = @cId",
new { cId = customerId },
flags: CommandFlags.NoCache);
rowsAffected = Connection.Execute(commandDefinition);
}
if (rowsAffected > 0)
{
operationResult.OperationStatus = OperationResult.Status.Success;
}
return operationResult;
}
public ICollection<OperationResult> WriteToDataSource(
ICollection<WriteOperation> operations)
{
var operationResults = new List<OperationResult>();
foreach (var operation in operations)
{
operationResults.Add(WriteToDataSource(operation));
}
return operationResults;
}
public ICollection<OperationResult> WriteToDataSource(
ICollection<DataTypeWriteOperation> dataTypeWriteOperations)
{
throw new NotImplementedException();
}
}
}