I am trying to create an efficient and generic Data Access Layer for my application using the Microsoft Enterprise Data Library, proper error checking, and the ‘using’ syntax to wherever necessary to ensure that all my resources are handled properly. I’m hoping to then leverage this DAL through the object data source syntax in my aspx page, as well as within my business logic in the code behind.
Since this probably doesn’t make much sense as words, I’ll throw in some pseudo-code:
DAL.cs
--------------
// Function for generic datareader method
Private SqlDataReader GetDataReader (string sqlCommand) {
SqlDatabase db = new SqlDatabase (connectionString);
Try {
Using (SqlDataReader reader = db.ExecuteReader(sqlCommand)) {
// This part I’m fuzzy about…how do I return the results from here???
// I considered using another SqlDataReader object, but that seems redundant?
}
} catch (SqlException error) {
Session[“Error”] = error.message;
Server.Transfer (“~/error/error.aspx”, false);
}
// ideally, I’d like to return the reader here…but if it’s contained in the ‘using block’
// I don’t know how to do so. Would making a temp DataTable work? What about
// Another SqlDataReader and assign it’s value to the result inside the ‘using’ block?
Return reader;
}
// Function to use the generic method
Public SqlDataReader GetAllEmployees() {
Return GetDataReader(“SELECT * FROM EMPLOYEES ORDER BY LAST_NAME”);
}
displayEmployees.aspx
------------------------------
<asp:DropDownList id=”ddlEmployees” runat=”server” DataSourceID=”dsEmployees”></asp:DropDownList>
<asp:ObjectDataSource id=”dsEmployees” runat=”server” selectMethod=”GetAllEmployees” TypeName=”DAL”>
There are all manner of problems I’m having with this though. The original DAL I have is similar to this, but everything is done with datasets. Now that my DAL is several thousand lines long, I’m concerned with the performance hit of populating all my data objects by DataSets….
Soooo…am I overreacting, by trying to remove all the DataSet objects? Or is this concern valid (as it seems the vast majority of opinions on the internet would suggest) and I need to figure out how to complete the above?
Further, would using DataTables as return objects and just forgetting about DataReaders be 'just as good'?
Thanks in advance...
Shaymus
ASKER
DAL.cs
--------------------
private DataTable GetDataTable(string sqlCommand) {
DataTable results = null;
SqlDatabase db = new SqlDatabase (connectionString);
Try {
Using (DataSet ds = db.ExecuteDataSet(sqlComma
result = ds.Tables[0];
}
} catch (SqlException error) {
Session[“Error”] = error.message;
Server.Transfer (“~/error/error.aspx”, false);
}
return result;
}
// Function to use the generic method
Public DataTable GetAllEmployees() {
Return GetDataTable(“SELECT * FROM EMPLOYEES ORDER BY LAST_NAME”);
}