-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataAccess.cs
More file actions
87 lines (75 loc) · 3.19 KB
/
DataAccess.cs
File metadata and controls
87 lines (75 loc) · 3.19 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
using Dapper;
using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
namespace ProcessorLibrary
{
public static class DataAccess
{
private static readonly string LogDatabase = "LoggingDb";
private static readonly ConcurrentDictionary<string, DbProviderFactory> ProviderFactories = new ConcurrentDictionary<string, DbProviderFactory>();
public static int InsertDeploymentAction(string deploymentType, string siteName)
{
var param = new
{
DeploymentType = deploymentType,
LoggedInUser = string.Empty,
SiteName = siteName
};
return RunProcedure(LogDatabase, "dbo.spDeploymentLog_InsertAction", param);
}
public static int InsertOutOfCycleFileChange(string fileName, string changeType)
{
var param = new
{
FileChange = fileName,
ChangeType = changeType
};
return RunProcedure(LogDatabase, "dbo.spOutOfCycleFileChanges_Insert", param);
}
private static int RunProcedure(string connectionKey, string procedureName, object param = null)
{
using (IDbConnection connection = GetConnection(connectionKey))
{
return connection.Execute(procedureName, param, commandTimeout: connection.ConnectionTimeout, commandType: CommandType.StoredProcedure);
}
}
private static Task<int> RunProcedureAsync(string connectionKey, string procedureName, object param = null)
{
using (IDbConnection connection = GetConnection(connectionKey))
{
return connection.ExecuteAsync(procedureName, param, commandTimeout: connection.ConnectionTimeout, commandType: CommandType.StoredProcedure);
}
}
private static IDbConnection GetConnection(string connectionName)
{
ConnectionStringSettings settings = Configuration.GetConnectionSettings(connectionName);
DbProviderFactory factory = GetProviderFactory(settings.ProviderName);
try
{
IDbConnection connection = factory.CreateConnection();
connection.ConnectionString = settings.ConnectionString;
return connection;
}
catch (Exception ex)
{
throw new Exception($"{factory.GetType().Name} failed to create a connection.", ex);
}
}
private static DbProviderFactory GetProviderFactory(string providerName = "System.Data.SqlClient")
{
DbProviderFactory providerFactory;
if (!ProviderFactories.TryGetValue(providerName, out providerFactory))
{
providerFactory = DbProviderFactories.GetFactory(providerName);
if (providerFactory == null)
throw new NotSupportedException($"{providerName} database provider is not available.");
ProviderFactories[providerName] = providerFactory;
}
return providerFactory;
}
}
}