I have a method in MyWebpage.aspx.cs lik so:
public partial class MyWebpage : PageBase
{
private readonly DataAccessLayer dataAccessLayer;
protected string GetMyTitle(string myVar, string myId)
{
if (string.IsNullOrEmpty(myVar))
{
return string.Empty;
}
return dataAccessLayer.GetMyTitle(Convert.ToInt32(myId), myVar);
}
}
In the DataAccessLayer class, I have a methoud that talks to the DB and does the DAL stuff and returns the title.
What’s the best practice on accessing the DAL from MyWebPage.aspx.cs class (as in do I need to create a new DataAccessLayer() object each time? Where should I create it in my PageBase class or everytime I call it in a code behind?
First thing is accessing DAL from your code behind or presentation layer generally is not a good practice. Because in this case you need to put your Business logic code in your code behind(Presentation Layer) which causes conflicting of concerns, high coupling, duplication and many other issues. So, if you’re look for the best practices, I suggest to have a look at the following links:
And these are really good books:
Fowler)
Also about having static function for calling the DAL. As you know static functions are vulnerable to the multi-threading, so if you are using anything shared in the DAL functions (which its the case sometimes, like shared connection, command etc) it will break your code, so I think it’s better to avoid static functions in this layer.