Skip to content

Commit

Permalink
wip #49
Browse files Browse the repository at this point in the history
* refactored the code
* moved oracle integration into core
  • Loading branch information
mrksoftware committed Mar 19, 2020
1 parent 0ff3f82 commit eba72a9
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 191 deletions.
5 changes: 4 additions & 1 deletion Elfo.Wardein.Abstractions/Elfo.Wardein.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<AssemblyName>Elfo.Firmenich.Wardein.Abstractions</AssemblyName>
<RootNamespace>Elfo.Firmenich.Wardein.Abstractions</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion Elfo.Wardein.Core/Elfo.Wardein.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<AssemblyName>Elfo.Firmenich.Wardein.Core</AssemblyName>
<RootNamespace>Elfo.Firmenich.Wardein.Core</RootNamespace>
<Version>1.1.0</Version>
</PropertyGroup>

Expand All @@ -20,12 +23,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.30" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.4" />
<PackageReference Include="Microsoft.Web.Administration" Version="11.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.6.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.0" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.60" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.5.0" />
<PackageReference Include="Warden" Version="2.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace Elfo.Wardein.Core.Helpers
{
public interface IOracleService
{
IEnumerable<T> Query<T>(IDbConnection connection, string query,
IDictionary<string, object> parameters, TimeSpan? timeout = null);

int Execute(IDbConnection connection, string command,
IDictionary<string, object> parameters, TimeSpan? timeout = null);

Task<IEnumerable<T>> QueryAsync<T>(IDbConnection connection, string query,
IDictionary<string, object> parameters, TimeSpan? timeout = null);

Task<int> ExecuteAsync(IDbConnection connection, string command,
IDictionary<string, object> parameters, TimeSpan? timeout = null);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
using System;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;

namespace Elfo.Wardein.Integrations.Oracle.Integration
namespace Elfo.Wardein.Core.Helpers
{
public interface IOracleService
public class DapperOracleService : IOracleService
{
Task<IEnumerable<T>> QueryAsync<T>(IDbConnection connection, string query,
IDictionary<string, object> parameters, TimeSpan? timeout = null);
public IEnumerable<T> Query<T>(IDbConnection connection, string query,
IDictionary<string, object> parameters, TimeSpan? timeout = null)
{
var queryParameters = new DynamicParameters();
if (parameters != null)
{
foreach (var parameter in parameters)
{
queryParameters.Add(parameter.Key, parameter.Value);
}
}

Task<int> ExecuteAsync(IDbConnection connection, string command,
IDictionary<string, object> parameters, TimeSpan? timeout = null);
}
return connection.Query<T>(query, queryParameters,
commandTimeout: (int?)timeout?.TotalSeconds);
}

public int Execute(IDbConnection connection, string command,
IDictionary<string, object> parameters, TimeSpan? timeout = null)
{
var commandParameters = new DynamicParameters();
if (parameters != null)
{
foreach (var parameter in parameters)
{
commandParameters.Add(parameter.Key, parameter.Value);
}
}

return connection.Execute(command, parameters,
commandTimeout: (int?)timeout?.TotalSeconds);
}

public class DapperOracleService : IOracleService
{
public async Task<IEnumerable<T>> QueryAsync<T>(IDbConnection connection, string query,
IDictionary<string, object> parameters, TimeSpan? timeout = null)
IDictionary<string, object> parameters, TimeSpan? timeout = null)
{
var queryParameters = new DynamicParameters();
if (parameters != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using Oracle.ManagedDataAccess.Client;
using System.Text;

namespace Elfo.Wardein.Integrations.Oracle.Integration
namespace Elfo.Wardein.Core.Helpers
{
public class OracleIntegrationConfiguration
public class OracleConnectionConfiguration
{
public OracleIntegrationConfiguration(string connectionString)
public OracleConnectionConfiguration(string connectionString)
{
ConnectionString = connectionString;

Expand All @@ -18,7 +19,8 @@ public OracleIntegrationConfiguration(string connectionString)
{
var oracleConnectionStringBuilder = new OracleConnectionStringBuilder(connectionString);
DatabaseType = oracleConnectionStringBuilder.DataSource;
} catch (Exception ex)
}
catch (Exception ex)
{
throw new ArgumentException("Oracle connection string is invalid.", nameof(connectionString), ex);
}
Expand All @@ -40,16 +42,21 @@ public OracleIntegrationConfiguration(string connectionString)
public IDictionary<string, object> CommandParameters { get; protected set; }
public TimeSpan? CommandTimeout { get; protected set; }

public string ClientId { get; set; }
public string ClientInfo { get; set; }
public string ModuleName { get; set; }
public string DateLanguage { get; set; }


public static Builder Create(string connectionString) => new Builder(connectionString);

public class Builder
{
protected readonly OracleIntegrationConfiguration Configuration;
protected readonly OracleConnectionConfiguration Configuration;

public Builder(string connectionString)
{
Configuration = new OracleIntegrationConfiguration(connectionString);
Configuration = new OracleConnectionConfiguration(connectionString);
}

public Builder WithCommand(string command, IDictionary<string, object> parameters = null)
Expand Down Expand Up @@ -126,7 +133,59 @@ public Builder WithOracleServiceProvider(Func<IOracleService> oracleServiceProvi
return this;
}

public OracleIntegrationConfiguration Build() => Configuration;
public Builder WithClientId(string clientId)
{
if (string.IsNullOrWhiteSpace(clientId))
{
throw new ArgumentNullException(nameof(clientId),
"ClientId can not be empty");
}

Configuration.ClientId = clientId;

return this;
}

public Builder WithClientInfo(string clientInfo)
{
if (string.IsNullOrWhiteSpace(clientInfo))
{
throw new ArgumentNullException(nameof(clientInfo),
"ClientInfo can not be empty.");
}

Configuration.ClientId = clientInfo;

return this;
}

public Builder WithModuleName(string moduleName)
{
if (string.IsNullOrWhiteSpace(moduleName))
{
throw new ArgumentNullException(nameof(moduleName),
"ModuleName can not be empty.");
}

Configuration.ClientId = moduleName;

return this;
}

public Builder WithDateLanguage(string dateLanguage)
{
if (string.IsNullOrWhiteSpace(dateLanguage))
{
throw new ArgumentNullException(nameof(dateLanguage),
"DateLanguage can not be empty.");
}

Configuration.ClientId = dateLanguage;

return this;
}

public OracleConnectionConfiguration Build() => Configuration;
}
}
}
136 changes: 136 additions & 0 deletions Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Elfo.Wardein.Core.Helpers
{
public class OracleHelper
{
private readonly OracleConnectionConfiguration oracleConfiguration;

public OracleHelper(OracleConnectionConfiguration oracleConfiguration)
{
this.oracleConfiguration = oracleConfiguration;
}

public IEnumerable<T> Query<T>(string query, IDictionary<string, object> parameters = null)
{
try
{
var result = default(IEnumerable<T>);
using (var connection = new OracleConnection(oracleConfiguration.ConnectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
connection.SetSessionInfoForTransaction(oracleConfiguration);

var queryToExecute = string.IsNullOrWhiteSpace(query) ? oracleConfiguration.Query : query;
var queryParameters = parameters ?? oracleConfiguration.QueryParameters;

result = oracleConfiguration.OracleServiceProvider().Query<T>(connection, queryToExecute,
queryParameters, oracleConfiguration.QueryTimeout);
transaction.Commit();
}
}
return result;
}
catch (Exception exception)
{
throw exception;
}
}

public int Execute(string command, IDictionary<string, object> parameters = null)
{

try
{
int result = 0;

using (var connection = new OracleConnection(oracleConfiguration.ConnectionString))
{
connection.Open();

using (var transaction = connection.BeginTransaction())
{
connection.SetSessionInfoForTransaction(oracleConfiguration);

var commandToExecute = string.IsNullOrWhiteSpace(command) ? oracleConfiguration.Command : command;
var commandParameters = parameters ?? oracleConfiguration.CommandParameters;

result = oracleConfiguration.OracleServiceProvider().Execute(connection, commandToExecute,
commandParameters, oracleConfiguration.CommandTimeout);
transaction.Commit();
}
}
return result;
}
catch (Exception exception)
{
throw exception;
}
}

public async Task<IEnumerable<T>> QueryAsync<T>(string query, IDictionary<string, object> parameters = null)
{
try
{
var result = default(IEnumerable<T>);
using (var connection = new OracleConnection(oracleConfiguration.ConnectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
connection.SetSessionInfoForTransaction(oracleConfiguration);

var queryToExecute = string.IsNullOrWhiteSpace(query) ? oracleConfiguration.Query : query;
var queryParameters = parameters ?? oracleConfiguration.QueryParameters;

result = await oracleConfiguration.OracleServiceProvider().QueryAsync<T>(connection, queryToExecute,
queryParameters, oracleConfiguration.QueryTimeout);
transaction.Commit();
}
}
return result;
}
catch (Exception exception)
{
throw exception;
}
}

public async Task<int> ExecuteAsync(string command, IDictionary<string, object> parameters = null)
{

try
{
int result = 0;

using (var connection = new OracleConnection(oracleConfiguration.ConnectionString))
{
connection.Open();

using (var transaction = connection.BeginTransaction())
{
connection.SetSessionInfoForTransaction(oracleConfiguration);

var commandToExecute = string.IsNullOrWhiteSpace(command) ? oracleConfiguration.Command : command;
var commandParameters = parameters ?? oracleConfiguration.CommandParameters;

result = await oracleConfiguration.OracleServiceProvider().ExecuteAsync(connection, commandToExecute,
commandParameters, oracleConfiguration.CommandTimeout);
transaction.Commit();
}
}
return result;
}
catch (Exception exception)
{
throw exception;
}
}
}
}
File renamed without changes.
20 changes: 20 additions & 0 deletions Elfo.Wardein.Core/Helpers/OracleConnectionExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace Elfo.Wardein.Core.Helpers
{
public static class OracleConnectionExtensionMethods
{
public static void SetSessionInfoForTransaction(this OracleConnection connection, OracleConnectionConfiguration oracleConfiguration)
{
connection.ClientId = oracleConfiguration.ClientId;
connection.ClientInfo = oracleConfiguration.ClientInfo;
connection.ModuleName = oracleConfiguration.ModuleName;
var sessionInfo = connection.GetSessionInfo();
sessionInfo.DateLanguage = oracleConfiguration.DateLanguage;
connection.SetSessionInfo(sessionInfo);
}
}
}
Loading

0 comments on commit eba72a9

Please sign in to comment.