Permalink
Browse files

Wrapping NetTiers errors thrown while accessing the database in a cus…

…tom Exception class that contains additional data.
  • Loading branch information...
niemyjski committed Dec 1, 2017
2 parents 254a8d4 + 72303b3 commit 54c5f96b15f2bb00c83bc51ec08baa3fadd4d62a
@@ -44,8 +44,9 @@ namespace <%=DALNameSpace%>.SqlClient
/// Uses connection string to connect to datasource.
/// </summary>
/// <param name="connectionString">The connection string to the database.</param>
/// <param name="defaultCommandTimeout">The command timeout for the database calls.</param>
/// <param name="useStoredProcedure">A boolean value that indicates if we use the stored procedures or embedded queries.</param>
/// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>
public <%=ProviderName%>(string connectionString, bool useStoredProcedure, string providerInvariantName): base(connectionString, useStoredProcedure, providerInvariantName){}
public <%=ProviderName%>(string connectionString, int defaultCommandTimeout, bool useStoredProcedure, string providerInvariantName): base(connectionString, defaultCommandTimeout, useStoredProcedure, providerInvariantName){}
}
}
@@ -139,6 +139,7 @@ namespace <%=DALNameSpace%>.SqlClient
string _connectionString;
bool _useStoredProcedure;
string _providerInvariantName;
int _defaultCommandTimeout;
#endregion "Declarations"
@@ -156,13 +157,15 @@ namespace <%=DALNameSpace%>.SqlClient
/// Uses connection string to connect to datasource.
/// </summary>
/// <param name="connectionString">The connection string to the database.</param>
/// <param name="defaultCommandTimeout">The command timeout for the database calls.</param>
/// <param name="useStoredProcedure">A boolean value that indicates if we should use stored procedures or embedded queries.</param>
/// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>
public <%=abstractRepositoryClassName%>(string connectionString, bool useStoredProcedure, string providerInvariantName)
public <%=abstractRepositoryClassName%>(string connectionString, int defaultCommandTimeout, bool useStoredProcedure, string providerInvariantName)
{
this._connectionString = connectionString;
this._useStoredProcedure = useStoredProcedure;
this._providerInvariantName = providerInvariantName;
this._defaultCommandTimeout = defaultCommandTimeout;
}
#endregion "Constructors"
@@ -241,7 +244,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%=GetClassName(primaryTable, ClassNameFormat.Collection)%> GetBy<%=functionname%>(TransactionManager transactionManager, <%=GetFunctionHeaderParameters(junctionTableKey.ForeignKeyMemberColumns)%>, int start, int pageLength, out int count)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + "By" + functionname%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + "By" + functionname%>", _useStoredProcedure);
<% foreach(ColumnSchema col in junctionTableKey.ForeignKeyMemberColumns) { %>
database.AddInParameter(commandWrapper, "@<%=GetPropertyName(col)%>", DbType.<%=col.DataType.ToString()%>, <%=GetFieldName(col)%>);
@@ -320,7 +323,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override bool <%= MethodNames.Delete %>(TransactionManager transactionManager, <%= GetFunctionHeaderParameters(SourceTable.PrimaryKey.MemberColumns) %><% if(RowVersion != null && !RowVersion.IsPrimaryKeyMember) {Response.Write(", byte[] " + GetFieldName(RowVersion));}%>)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + DeleteSuffix%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + DeleteSuffix%>", _useStoredProcedure);
<% for(int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) {
ColumnSchema col = SourceTable.PrimaryKey.MemberColumns[i];
%>
@@ -397,7 +400,7 @@ namespace <%=DALNameSpace%>.SqlClient
return new <%=collectionClassName%>();
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + FindSuffix%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + FindSuffix%>", _useStoredProcedure);
bool searchUsingOR = false;
if (whereClause.IndexOf(" OR ") > 0) // did they want to do "a=b OR c=d OR..."?
@@ -512,7 +515,7 @@ namespace <%=DALNameSpace%>.SqlClient
filter = parameters.GetParameters();
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%= owner + ProcedurePrefix + commandStem + FindSuffix %>_Dynamic", typeof(<%= GetClassName(SourceTable, ClassNameFormat.Column) %>), filter, orderBy, start, pageLength);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%= owner + ProcedurePrefix + commandStem + FindSuffix %>_Dynamic", typeof(<%= GetClassName(SourceTable, ClassNameFormat.Column) %>), filter, orderBy, start, pageLength);
SqlFilterParameter param;
@@ -592,7 +595,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%=collectionClassName%> <%= MethodNames.GetAll %>(TransactionManager transactionManager, int start, int pageLength, out int count)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + SelectAllSuffix%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + SelectAllSuffix%>", _useStoredProcedure);
IDataReader reader = null;
@@ -654,7 +657,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%=collectionClassName%> <%= MethodNames.GetPaged %>(TransactionManager transactionManager, string whereClause, string orderBy, int start, int pageLength, out int count)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + "Paged"%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + SelectSuffix + "Paged"%>", _useStoredProcedure);
if (commandWrapper.CommandType == CommandType.Text
@@ -756,7 +759,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%=collectionClassName%> GetBy<%=GetKeysName(fkeys[j].ForeignKeyMemberColumns)%>(TransactionManager transactionManager, <%= GetFunctionHeaderParameters(fkeys[j].ForeignKeyMemberColumns) %>, int start, int pageLength, out int count)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + GetProcNameForGetByIX(ProcedurePrefix + commandStem + SelectSuffix + "By", fkeys[j].ForeignKeyMemberColumns)%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + GetProcNameForGetByIX(ProcedurePrefix + commandStem + SelectSuffix + "By", fkeys[j].ForeignKeyMemberColumns)%>", _useStoredProcedure);
<% foreach(ColumnSchema col in fkeys[j].ForeignKeyMemberColumns) { %>
database.AddInParameter(commandWrapper, "@<%=GetPropertyName(col)%>", DbType.<%=col.DataType%>, <%=GetFieldName(col)%>);
@@ -863,7 +866,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%=returnType%> GetBy<%=GetKeysName(indexes[j].MemberColumns)%>(TransactionManager transactionManager, <%= GetFunctionHeaderParameters(indexes[j].MemberColumns) %>, int start, int pageLength, out int count)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + GetProcNameForGetByIX(ProcedurePrefix + commandStem + SelectSuffix + "By", indexes[j].MemberColumns)%>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + GetProcNameForGetByIX(ProcedurePrefix + commandStem + SelectSuffix + "By", indexes[j].MemberColumns)%>", _useStoredProcedure);
<% foreach(ColumnSchema col in indexes[j].MemberColumns) { %>
database.AddInParameter(commandWrapper, "@<%=GetPropertyName(col)%>", DbType.<%=col.DataType%>, <%=GetFieldName(col)%>);
@@ -1031,7 +1034,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override bool <%= MethodNames.Insert %>(TransactionManager transactionManager, <%=className%> entity)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + InsertSuffix %>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + InsertSuffix %>", _useStoredProcedure);
<%
for (int i = 0; i < cols.Count; i++)
@@ -1152,7 +1155,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override bool <%= MethodNames.Update %>(TransactionManager transactionManager, <%=className%> entity)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%=owner + ProcedurePrefix + commandStem + UpdateSuffix %>", _useStoredProcedure);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%=owner + ProcedurePrefix + commandStem + UpdateSuffix %>", _useStoredProcedure);
<%
//Debugger.Break();
@@ -1306,7 +1309,7 @@ namespace <%=DALNameSpace%>.SqlClient
public override <%= returnType %> <%=methodName%>(TransactionManager transactionManager, int start, int pageLength <%=TransformStoredProcedureInputsToMethod(true, command.InputParameters) + TransformStoredProcedureOutputsToMethod(true, command.AllOutputParameters)%>)
{
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "<%= GetOwnerName(command, true) %><%=GetSafeName(command.Name)%>", true);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, this._defaultCommandTimeout, "<%= GetOwnerName(command, true) %><%=GetSafeName(command.Name)%>", true);
<% foreach(ParameterSchema col in command.InputParameters) {%>
database.AddInParameter(commandWrapper, "<%=col.Name%>", <%=GetDbType(col)%>, <%=GetFieldName(col)%> );
@@ -245,7 +245,7 @@ namespace <%=DALNameSpace%>.SqlClient
{
if (innerSql<%=providerName%> == null)
{
this.innerSql<%=providerName%> = new Sql<%=providerName%>(_connectionString, _useStoredProcedure, _providerInvariantName);
this.innerSql<%=providerName%> = new Sql<%=providerName%>(_connectionString, DefaultCommandTimeout, _useStoredProcedure, _providerInvariantName);
}
}
}
@@ -291,7 +291,7 @@ namespace <%=DALNameSpace%>.SqlClient
{
if (innerSql<%=providerName%> == null)
{
this.innerSql<%=providerName%> = new Sql<%=providerName%>(_connectionString, _useStoredProcedure, _providerInvariantName);
this.innerSql<%=providerName%> = new Sql<%=providerName%>(_connectionString, DefaultCommandTimeout, _useStoredProcedure, _providerInvariantName);
}
}
}
@@ -179,22 +179,23 @@ namespace <%=DALNameSpace%>.SqlClient
/// Get the command wrapper, either from the stored procedures or from the embedded queries.
/// </summary>
/// <param name="database">The database instance.</param>
/// <param name="defaultCommandTimeout">the timeout of the sp.</param>
/// <param name="commandName">the name of the sp.</param>
/// <param name="useStoredProcedure">Indicates if we want stored procedure.</param>
/// <returns></returns>
public static DbCommand GetCommandWrapper(Database database, string commandName, bool useStoredProcedure)
public static DbCommand GetCommandWrapper(Database database, int defaultCommandTimeout, string commandName, bool useStoredProcedure)
{
if (useStoredProcedure)
{
DbCommand command = database.GetStoredProcCommand(commandName);
command.CommandTimeout = DataRepository.Provider.DefaultCommandTimeout;
command.CommandTimeout = defaultCommandTimeout; //DataRepository.Provider.DefaultCommandTimeout;
return command;
}
else
{
DbCommand command =
database.GetSqlStringCommand(StoredProcedureProvider.GetProcedureBodyFromEmbeddedResource(commandName));
command.CommandTimeout = DataRepository.Provider.DefaultCommandTimeout;
command.CommandTimeout = defaultCommandTimeout; //DataRepository.Provider.DefaultCommandTimeout;
return command;
}
}
@@ -204,14 +205,15 @@ namespace <%=DALNameSpace%>.SqlClient
/// optionally includes server-side paging.
/// </summary>
/// <param name="database"></param>
/// <param name="defaultCommandTimeout"></param>
/// <param name="commandName"></param>
/// <param name="columnEnum"></param>
/// <param name="parameters"></param>
/// <param name="orderBy"></param>
/// <param name="start"></param>
/// <param name="pageLength"></param>
/// <returns></returns>
public static DbCommand GetCommandWrapper(Database database, String commandName, Type columnEnum, SqlFilterParameterCollection parameters, String orderBy, int start, int pageLength)
public static DbCommand GetCommandWrapper(Database database, int defaultCommandTimeout, String commandName, Type columnEnum, SqlFilterParameterCollection parameters, String orderBy, int start, int pageLength)
{
String query = StoredProcedureProvider.GetProcedureBodyFromEmbeddedResource(commandName);
//query = query.Replace(SqlUtil.PAGE_INDEX, string.Concat(SqlUtil.PAGE_INDEX, Guid.NewGuid().ToString("N").Substring(0,8)));
@@ -227,7 +229,7 @@ namespace <%=DALNameSpace%>.SqlClient
query = String.Format(query, whereClause, sortExpression, start, (start + pageLength));
DbCommand command = database.GetSqlStringCommand(query);
command.CommandTimeout = DataRepository.Provider.DefaultCommandTimeout;
command.CommandTimeout = defaultCommandTimeout; //DataRepository.Provider.DefaultCommandTimeout;
return command;
}
@@ -39,8 +39,9 @@ namespace <%=DALNameSpace%>.SqlClient
/// Uses connection string to connect to datasource.
/// </summary>
/// <param name="connectionString">The connection string to the database.</param>
/// <param name="defaultCommandTimeout">The command timeout for the database calls.</param>
/// <param name="useStoredProcedure">A boolean value that indicates if we use the stored procedures or embedded queries.</param>
/// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>
public <%=ProviderName%>(string connectionString, bool useStoredProcedure, string providerInvariantName): base(connectionString, useStoredProcedure, providerInvariantName){}
public <%=ProviderName%>(string connectionString, int defaultCommandTimeout, bool useStoredProcedure, string providerInvariantName): base(connectionString, defaultCommandTimeout, useStoredProcedure, providerInvariantName){}
}
}
Oops, something went wrong.

0 comments on commit 54c5f96

Please sign in to comment.