For usage in my current project I've created a class that allows me to call SQL Server async.
My code looks like this:
internal class CommandAndCallback<TCallback, TError>
{
public SqlCommand Sql { get; set; }
public TCallback Callback { get; set; }
public TError Error { get; set; }
}
class MyCodes:SingletonBase<MyCodes>
{
private static string _connString = #"Data Source=MyDB;Initial Catalog=ED;Integrated Security=True;Asynchronous Processing=true;Connection Timeout=0;Application Name=TEST";
private MyCodes() { }
public void SetSystem(bool production)
{
_connString =
string.Format(#"Data Source=MyDB;Initial Catalog={0};Integrated Security=True;Asynchronous Processing=true;Connection Timeout=0;Application Name=TEST", production ? "ED" : "TEST_ED");
}
public void Add(string newCode, Action<int> callback, Action<string> error)
{
var conn = new SqlConnection(_connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = #"ADD_CODE";
cmd.Parameters.Add("#NEW", SqlDbType.NVarChar).Value = newCode;
cmd.Parameters.Add("#NewId", SqlDbType.Int).Direction = ParameterDirection.Output;
try
{
cmd.Connection.Open();
}
catch (Exception ex)
{
error(ex.ToString());
return;
}
var ar = new CommandAndCallback<Action<int>, Action<string>> { Callback = callback, Error = error, Sql = cmd };
cmd.BeginExecuteReader(Add_Handler, ar, CommandBehavior.CloseConnection);
}
private static void Add_Handler(IAsyncResult result)
{
var ar = (CommandAndCallback<Action<int>, Action<string>>)result.AsyncState;
if (result.IsCompleted)
{
try
{
ar.Sql.EndExecuteReader(result);
ar.Callback(Convert.ToInt32(ar.Sql.Parameters["#NewId"].Value));
}
catch (Exception ex)
{
ar.Error(ex.Message);
}
}
else
{
ar.Error("Error executing SQL");
}
}
public void Update(int codeId, string newCode, Action callback, Action<string> error)
{
var conn = new SqlConnection(_connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = #"UPDATE_CODE";
cmd.Parameters.Add("#CODE_ID", SqlDbType.Int).Value = codeId;
cmd.Parameters.Add("#NEW", SqlDbType.NVarChar).Value = newCode;
try
{
cmd.Connection.Open();
}
catch (Exception ex)
{
error(ex.ToString());
return;
}
var ar = new CommandAndCallback<Action, Action<string>> { Callback = callback, Error = error, Sql = cmd };
cmd.BeginExecuteReader(Update_Handler, ar, CommandBehavior.CloseConnection);
}
private static void Update_Handler(IAsyncResult result)
{
var ar = (CommandAndCallback<Action, Action<string>>)result.AsyncState;
if (result.IsCompleted)
{
try
{
ar.Sql.EndExecuteReader(result);
ar.Callback();
}
catch (Exception ex)
{
ar.Error(ex.Message);
}
}
else
{
ar.Error("Error executing SQL");
}
}
}
This may look like too much of code, but it lets me call it as so:
private void Add_Click(object sender, EventArgs e)
{
MyCodes.Instance.Add("Test",Success,Error)
}
private void Success(int newId)
{
MessageBox.Show(newId.ToString(), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Error(string error)
{
MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Above code works just fine for me, I'm able to do every call async.
Problem that I have right now is to do multiple calls as transaction - I would like to update 2 codes and add one new.
Normally I would call update, then in success handler call second update, and in handler to second update I would call add that would return new id.
Something like:
-UPDATE CODE
|-UPDATE CODE
|-ADD CODE (only this one return something)
But I would like to call all of those as transaction, so if add code would break updates would rollback.
Question:
Is it possible to call multiple async queries as a transaction?
Can I call my above methods as transaction or do I must create separate method to call my procedures as one? (I would like to avoid this one because it's just copying the same code from one method to another)
I would like to add that I use .NET 3.5 so await and other nice features aren't an option.
string cnnString =WebConfigurationManager.ConnectionStrings["MyString"].ConnectionString;
SqlConnection cnn = new SqlConnection(cnnString);
SqlTransaction transaction;
cnn.Open();
transaction = cnn.BeginTransaction();
try
{
// Command Objects for the transaction
SqlCommand cmd1 = new SqlCommand("sproc1", cnn);
SqlCommand cmd2 = new SqlCommand("sproc2", cnn);
cmd1.CommandType = CommandType.StoredProcedure;
cmd2.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add(new SqlParameter("#Param1", SqlDbType.NVarChar, 50));
cmd1.Parameters["#Param1"].Value = paramValue1;
cmd1.Parameters.Add(new SqlParameter("#Param2", SqlDbType.NVarChar, 50));
cmd1.Parameters["#Param2"].Value = paramValue2;
cmd2.Parameters.Add(new SqlParameter("#Param3", SqlDbType.NVarChar, 50));
cmd2.Parameters["#Param3"].Value = paramValue3;
cmd2.Parameters.Add(new SqlParameter("#Param4", SqlDbType.NVarChar, 50));
cmd2.Parameters["#Param4"].Value = paramValue4;
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException sqlEx)
{
transaction.Rollback();
}
finally
{
cnn.Close();
cnn.Dispose();
}
Yes, it is possible. Simply call SqlConnection.BeginTransaction before your first call. Make sure you assign the returned SqlTransaction object to each SqlCommand.Transaction in the chain and call SqlTransaction.Commit() at the end.
public class Command
{
public string sql { get; set; }
public CommandType cmdType { get; set; }
public Dictionary<string, object> parameter { get; set; } = null;
}
private Command insertInvoice(Invoice invoice)
{
try
{
Dictionary<string, object> parameterLocal = new Dictionary<string, object>();
parameterLocal.Add("p_customerId", invoice.customerId);
parameterLocal.Add("p_invoiceNo", invoice.invoiceNo);
parameterLocal.Add("p_invoiceDate", invoice.invoiceDate);
parameterLocal.Add("p_invoiceAmount", invoice.invoiceAmount);
parameterLocal.Add("p_withInvoice", invoice.withInvoice);
return (new Command { sql = "sp_insertInvoice", cmdType = CommandType.StoredProcedure, parameter = parameterLocal });
}
catch (Exception ex)
{
throw ex;
}
}
private Command insertInvoiceModel(InvoiceModel invoiceModel)
{
try
{
Dictionary<string, object> parameterLocal = new Dictionary<string, object>();
parameterLocal.Add("p_invoiceNo", invoiceModel.invoiceNo);
parameterLocal.Add("p_model", invoiceModel.model);
parameterLocal.Add("p_quantity", invoiceModel.quantity);
parameterLocal.Add("p_unitPrice", invoiceModel.unitPrice);
return (new Command { sql = "sp_insertInvoiceModel", cmdType = CommandType.StoredProcedure, parameter = parameterLocal });
}
catch (Exception ex)
{
throw ex;
}
}
List<Command> commandList = new List<Command>();
cmd = insertInvoice(invoicesave);
commandList.Add(cmd);
cmd = insertInvoiceModel(invoiceModelSave);
commandList.Add(cmd);
try
{
erplibmain.erpDac.runOleDbTransaction(commandList);
}
catch (Exception ex)
{
throw ex;
}
public void runOleDbTransaction(List<Command> commandList)
{
OleDbConnection erpConnection = new OleDbConnection(ErpDalMain.connectionstring);
erpConnection.Open();
OleDbCommand erpCommand = erpConnection.CreateCommand();
OleDbTransaction erpTrans;
// Start a local transaction
erpTrans = erpConnection.BeginTransaction();
// Assign transaction object for a pending local transaction
erpCommand.Connection = erpConnection;
erpCommand.Transaction = erpTrans;
try
{
foreach (Command cmd in commandList)
{
erpCommand.CommandText = cmd.sql;
erpCommand.CommandType = cmd.cmdType;
foreach (KeyValuePair<string, object> entry in cmd.parameter)
{
erpCommand.Parameters.AddWithValue(entry.Key, entry.Value);
}
erpCommand.ExecuteNonQuery();
erpCommand.Parameters.Clear();
}
erpTrans.Commit();
}
catch (Exception e)
{
try
{
erpTrans.Rollback();
}
catch (OleDbException ex)
{
if (erpTrans.Connection != null)
{
throw ex;
}
}
throw e;
}
finally
{
erpConnection.Close();
}
}
Related
i am trying to create separate class for connection named Connection.cs, why am i having this error while executing the query
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
what am i doing wrong ?
in my Dal.cs class
public void Insert(string f1,string f2)
{
string query;
Connection c = new Connection();
c.OpenCnn();
try {
query = "inset into ..."
cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
catch (Exception ex){
throw ex;
}
finally {
c.CloseCnn();
}
}
and in my Connection.cs class
public class Connection
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
public void OpenCnn()
{
string cnnStr = ConfigurationManager.ConnectionStrings["myconnstrng"].ToString();
con = new SqlConnection(cnnStr);
con.Open();
}
public void CloseCnn()
{
con.Close();
}
}
Currently the con field is private and can not be accessible from outside of Connection class.
You can add a property to your connection class:
public SqlConnection Connection { get { return con; } }
Then access it via your instance:
Connection c = new Connection();
c.OpenCnn();
try
{
query = "inset into ..."
cmd = new SqlCommand(query, c.Connection);
cmd.ExecuteNonQuery();
}
Btw, I'm not sure what are you referring by con in your Insert method (probably a local variable instead of the con field, because otherwise it wouldn't even compile)
public class Connection
{
SqlConnection con;
public SqlConnection Conn { get { return con; } }
public void OpenCnn()
{
string cnnStr = ConfigurationManager.ConnectionStrings["myconnstrng"].ToString();
con = new SqlConnection(cnnStr);
con.Open();
}
public void CloseCnn()
{
con.Close();
}
}
Method Insert:
Note SqlCommand initialization and usage of public property conn.
public void Insert(string f1, string f2)
{
string query;
Connection c = new Connection();
c.OpenCnn();
try
{
query = "insert into ..."
SqlCommand cmd = new SqlCommand(query, c.Conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex){
throw ex;
}
finally {
c.CloseCnn();
}
}
I've coded a dll to execute SQL commands.
And I have some code duplication in my public methods. Is there some way to avoid code duplication?
Method 1:
public Object ExecuteScalar(String command)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand commandScalar = GetSqlCommand(strCommand: command);
object result = new object();
using (commandScalar)
{
try
{
connection.StatisticsEnabled = statisticsEnabled;
connection.Open();
result = commandScalar.ExecuteScalar();
}
catch (Exception exception)
{
throw new Exception(String.Format("Не удалось выполнить команду: {0}", commandScalar.CommandText), exception);
}
finally
{
connection.Close();
}
}
return result;
if (connection.StatisticsEnabled)
AddDictionary(connection.RetrieveStatistics());
return result;
}
}
Method 2:
public void ExecuteNonQuery(String command, List<SqlParameter> lsParams = null)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand commandNonQuery = GetSqlCommand(strCommand: command, lsParams: lsParams, log: logger);
using(commandNonQuery)
{
try
{
connection.StatisticsEnabled = statisticsEnabled;
connection.Open();
commandNonQuery.ExecuteNonQuery();
}
catch (Exception exception)
{
throw new Exception(String.Format("Не удалось выполнить команду: {0}", commandNonQuery.CommandText), exception);
}
finally
{
connection.Close();
}
}
if (connection.StatisticsEnabled)
AddDictionary(connection.RetrieveStatistics());
}
}
Method 3:
public List<IDataRecord> ExecuteReader(String strCommand, List<SqlParameter> lsParams = null)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand sqlCommandRead = GetSqlCommand(strCommand: strCommand, lsParams: lsParams, log: logger);
List<IDataRecord> lsDataRecord = new List<IDataRecord>();
using (sqlCommandRead)
{
try
{
connection.StatisticsEnabled = statisticsEnabled;
connection.Open();
using (SqlDataReader sqlDataReader = sqlCommandRead.ExecuteReader())
{
while (sqlDataReader.Read())
{
lsDataRecord.Add((IDataRecord)sqlDataReader);
}
}
}
catch (Exception exception)
{
throw new Exception(String.Format("Не удалось выполнить команду: {0}", sqlCommandRead.CommandText), exception);
}
finally
{
connection.Close();
}
}
if (connection.StatisticsEnabled)
this.AddDictionary(connection.RetrieveStatistics());
return lsDataRecord;
}
}
Method that generates SQL command to execute:
private static SqlCommand GetSqlCommand(string strCommand, List<SqlParameter> lsParams = null, Logger log = null)
{
lsParams = null ?? new List<SqlParameter>();
log = null ?? LogManager.GetLogger("noname");
if (strCommand == null || strCommand == "")
{
throw new ArgumentException("Передан пустой (или null) текст команды.", "strCommand");
}
int parametersRequired = strCommand.Split('#').Length - 1;
if (parametersRequired != lsParams.Count)
{
String strParameters = null;
foreach (var item in lsParams)
{
strParameters += item.ParameterName + " : " + item.Value + "\n";
}
strParameters = null ?? "No parameters";
throw new ArgumentException(String.Format("При формировании SQL - команды выявлено, что число требуемых параметров: {0} не соответствует числу переданных: {1}\n"
+ "Команда:\n{2}\nПараметры:\n{3}", parametersRequired, lsParams.Count, strCommand, strParameters), "lsParams");
}
SqlCommand sqlCommand = new SqlCommand(strCommand);
foreach (var item in lsParams)
{
sqlCommand.Parameters.Add(item);
}
return sqlCommand;
}
public class SqlHelper
{
private readonly bool statisticsEnabled;
public SqlHelper(bool statisticsEnabled)
{
this.statisticsEnabled = statisticsEnabled;
}
public T ExecuteSqalar<T>(SqlCommand command)
{
return Execute(command, c => (T) c.ExecuteScalar());
}
public void ExecuteNonQuery(SqlCommand command)
{
Execute(command, c => c.ExecuteNonQuery());
}
public List<IDataRecord> ExecuteReader(SqlCommand command)
{
return Execute<List<IDataRecord>>(command, c =>
{
var lsDataRecord = new List<IDataRecord>();
using (SqlDataReader sqlDataReader = command.ExecuteReader())
{
while (sqlDataReader.Read())
{
lsDataRecord.Add(sqlDataReader);
}
}
});
}
public T Execute<T>(SqlCommand command, Func<SqlCommand, T> processFunction)
{
using (var connection = new SqlConnection("CONNECTION_STRING"))
{
object result = new object();
using (command)
{
try
{
connection.StatisticsEnabled = statisticsEnabled;
connection.Open();
result = processFunction(command);
}
catch (Exception exception)
{
throw new Exception(String.Format("Не удалось выполнить команду: {0}", command.CommandText), exception);
}
finally
{
connection.Close();
}
}
if (connection.StatisticsEnabled)
AddDictionary(connection.RetrieveStatistics());
return (T)result;
}
}
private void AddDictionary(IDictionary retrieveStatistics)
{
// TODO:
}
}
Business entity class:
public class TextileApplication
{
private System.Int64 _UserId;
private System.String _ApplicationNo;
private System.Int32 _SchemeId;
}
Code on .cs page:
protected void ibtnSubmitFirstPanel_Click(object sender, EventArgs e)
{
TextileApplication _TextileApplication = new TextileApplication();
_TextileApplication.UserId = 1;//static
_TextileApplication.ApplicationNo = GenerateApplicationNo();
_TextileApplication.SchemeId = Convert.ToInt16(rblScheme.SelectedValue);
string i = blTextileApplication.InsertTextileApplication(_TextileApplication);
if (Convert.ToInt16(i.Split(',').GetValue(0)) > 0)
{
// insert into another table
}
else
{
// rollback
}
}
Business Access class:
public static string InsertTextileApplication(TextileApplication _TextileApplication)
{
string i = "0";
try
{
daTextileApplication _daTextileApplication = new daTextileApplication();
object [] o = _daTextileApplication.InsertTextileApplication(_TextileApplication);
i = o[0].ToString();
}
catch (Exception ex)
{
LogErrorToLogFile logFile = new LogErrorToLogFile();
logFile.LogError(ex);
throw ex;
}
return i;
}
Data access class:
public object[] InsertTextileApplication(TextileApplication _TextileApplication)
{
try
{
pList = new List<SqlParameter>();
pList.Add(new SqlParameter("#UserId", _TextileApplication.UserId));
pList.Add(new SqlParameter("#ApplicationNo", _TextileApplication.ApplicationNo));
pList.Add(new SqlParameter("#SchemeId", _TextileApplication.SchemeId));
SqlParameter _AppNoOut = new SqlParameter("#AppNoOut", SqlDbType.VarChar,50);
_AppNoOut.Direction = ParameterDirection.Output;
pList.Add(_AppNoOut);
object[] o = sa.ExecuteQueryWithOutParameters("SPInsertTextileApplication", pList);
return o;
}
catch (Exception ex)
{
// logFile.LogError(ex);
// throw ex;
}
}
SQL access class:
public class SqlAccess
{
public object[] ExecuteQueryWithOutParameters(String procedureName, List<SqlParameter> param)
{
int count = 0;
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procedureName;
cmd.Parameters.Clear();
if (param != null)
{
foreach (SqlParameter p in param)
{
cmd.Parameters.Add(p);
if (p.Direction == ParameterDirection.Output)
{
count++;
}
}
}
try
{
connect();
cmd.Connection = con;
cmd.ExecuteNonQuery();
//iRet = Convert.ToInt32(retValReference.Value);
object[] obj = new object[count];
count = 0;
if (param != null)
{
for (int i = 0; i < param.Count; i++)
{
if (param[i].Direction == ParameterDirection.Output)
{
obj[count] = param[i].Value.ToString();
count++;
}
}
}
return obj;
}
catch (Exception ex)
{
throw ex;
}
finally
{
closeconnect();
}
}
}
I know this is complex architecture. I need to apply transaction concept then how to do it??
Read up on the System.Transaction namespace. A transactionscipe if quite powerfully and perfectuy suitable to project transactions even through a not exactly well designed multi tiered architecture such as yours.
Alternatively a unit of work pattern would be suitable.
But man, you really try to write as much code as possible instead of using established patterns. the lower 3 classes in your list should never be written by a human.
I am writing my own data access layer class for the first time so I would like to wrap it inside a (using) parenthesis but that requires the class to implement Idisposable.
The only thing I put in there is
conn.close()
is there something else missing that i should be adding there?
class overview:
public class DAL : IDisposable
{
SqlConnection conn;
SqlCommand cmd;
SqlTransaction transaction;
public int Status = 1;
public DAL()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);
conn.Open();
}
public void InsertRequest(UserAndRequestInfo UInfo, UserCrops UCrop)
{
}
public void InsertBGs(BreedingGroups BG)
{
}
public void EndTransaction()
{
if (Status == 1)
transaction.Commit();
if (Status < 0)
transaction.Rollback();
}
public void EndConnection()
{
conn.Close();
}
void Dispose()
{
conn.Close();
}
called from:
using (DAL db = new DAL())
{
foreach (var crop in UInfo.UserCropInfo)
{
db.InsertRequest(UInfo, crop);
foreach (BreedingGroups bg in crop.BGs)
{
db.InsertBGs(bg);
}
db.EndTransaction();
}
db.EndConnection(); <---- //If 'using' is there I'll remove this line
}
Also, in this particular case, wouldn't be just better to call an db.EndConnection() method
Yes: Call Dispose() on any disposable variables in your class.
void Dispose()
{
if(cmd != null)
cmd.Dispose();
if(transaction != null)
transaction.Dispose();
if(conn != null)
conn.Dispose();
}
edit: conn.Dispose() will also call conn.Close() so you shouldn't need to also call close it if you call dispose.
This is just a choich for you, to use the using scope in InsertRequest method instead as the following:
public class DAL
{
public void InsertRequest(UserAndRequestInfo UInfo, UserCrops UCrop)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
{
conn.Open();
SqlCommand command = conn.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = conn.BeginTransaction("SampleTransaction");
command.Connection = conn;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
public void InsertBGs(BreedingGroups BG)
{
}
}
Note that you don't need to make database connection in Constructure, remove it out.
And use the same concept to implement InsertBGs method.
This is form to call the
DAL db = new DAL();
foreach (var crop in UInfo.UserCropInfo)
{
db.InsertRequest(UInfo, crop);
foreach (BreedingGroups bg in crop.BGs)
{
db.InsertBGs(bg);
}
}
I have a DB class that makes all DB calls like below:
public delegate void Part1_Callback(string message);
public delegate void Part2_Callback(DataTable dt);
public delegate void Part3_Callback(DataTable dt, int x, int y);
public delegate void ErrorHandler(string message);
public class CommandAndCallback<TCallback>
{
public SqlCommand Sql { get; set; }
public TCallback Callback { get; set; }
public ErrorHandler Error { get; set; }
}
class DB : SingletonBase<DB>
{
public static readonly string SqlConnectionString = #"Data Source=MyDB;Initial Catalog=Stats;Integrated Security=True;Asynchronous Processing=true;";
private DB()
{
}
public void Part2(Part2_Callback callback, ErrorHandler error)
{
SqlConnection conn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Part2";
try
{
conn.Open();
}
catch (Exception ex)
{
error(ex.ToString());
return;
}
CommandAndCallback<Part2_Callback> ar = new CommandAndCallback<Part2_Callback>() { Callback = callback, Error = error, Sql = cmd };
IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(Part2_Handler), ar, CommandBehavior.CloseConnection);
}
private void Part2_Handler(IAsyncResult result)
{
DataTable dt = new DataTable();
CommandAndCallback<Part2_Callback> ar = (CommandAndCallback<Part2_Callback>)result.AsyncState;
SqlDataReader dr;
if (result.IsCompleted)
{
dr = ar.Sql.EndExecuteReader(result);
}
else
dr = null;
dt.Load(dr);
dr.Close();
dt.Columns[3].ReadOnly = false;
ar.Callback(dt);
}
}
And in my main class I'm using it like so:
private void Form1_Enter(object sender, EventArgs e)
{
showStatus("Loading");
DB.Instance.Part2(Part2_OK, ErrorHandler);
}
private void ErrorHandler(string msg)
{
hideStatus();
viewStack1.InvokeIfRequired(c => { c.moveToFirst(); });
//MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void Part2_OK(DataTable dt)
{
dataGridView1.InvokeIfRequired(c =>
{
c.DataSource = dt;
});
}
Right now I have 3 methods in my DB class that return 3 different sets of data, for every type I must declare delegate.
If in future I'll add more methods then I'll have to add more delegates.
Can I remove usage of delegates? I would like to simplify build of my class so that it will be easy to add new methods.
What I need is to be able to call my DB Class like so:
DB.Instance.PartX(PartX_OK, ErrorHandler);
PartX is declared as shown
private void PartX_OK(DataTable dt, int x, int y, ...)
{
//logic here
}
Can Action<T> be used to to that, so I can call my handlers with multiple parameters? If yes then how?
Yes, I meant to get back to your earlier question, so your function would become
public void Part2(Action<DataTable> callback, ErrorHandler error)
{
SqlConnection conn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Part2";
try
{
conn.Open();
}
catch (Exception ex)
{
error(ex.ToString());
return;
}
CommandAndCallback<Action<DataTable>> ar = new CommandAndCallback<Action<DataTable>>() { Callback = callback, Error = error, Sql = cmd };
IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(Part2_Handler), ar, CommandBehavior.CloseConnection);
}
private void Part2_Handler(IAsyncResult result)
{
DataTable dt = new DataTable();
CommandAndCallback<Action<DataTable>> ar = (CommandAndCallback<Action<DataTable>>)result.AsyncState;
SqlDataReader dr;
if (result.IsCompleted)
{
dr = ar.Sql.EndExecuteReader(result);
}
else
dr = null;
dt.Load(dr);
dr.Close();
dt.Columns[3].ReadOnly = false;
ar.Callback(dt);
}
There are Action classes with upto 16 generic parameters, you will probably find the one for your needs;). See MSDN page.
And the invocation
DB.Instance.PartX((p1, p2, p3, p4) => { ... }, ErrorHandler);
Part1_Callback will be equivilant to Action<string>
Part2_Callback will be equivilant to Action<DataTable>
Part3_Callback will be equivilant to Action<DataTable, int, int>
ErrorHandler will be equivilant to Action<string>
Just use those types in place of each of your existing delegates.
There are almost no cases anymore where you need to define your own delegates. If you have >16 parameters, ref/out parameters, params parameters, or optional arguments then there may not be an Action/Func overload for you, but that's not particularly common.