I am using ASP.NET Web Forms/C#.I am having a page Customer.aspx.I have created CustomerBLL class in which I plan to use Linq To SQL queries to perform all database related tasks.
Consider this example.Here is a method called GetDateFormat() in my CustomerBLL class which returns Date format from database.Here is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace CwizBankApp
{
public class CustomerBLL
{
public string GetDateFormat()
{
using (var db = new DataClasses1DataContext())
{
var format = db.CODEs.Where(code => code.NAME.Equals("dateformat")).SingleOrDefault();
return format.DRNAME;
}
}
}
}
From code behind I am calling this function inside of another function like this.
public void RetrieveDateFormat()
{
//In this function we retrieve the date format
//from the database to identify if it is british or american
var format = CustomerBLL.GetDateFormat();
//The global variable gDateFormat stores the format
if (format != null)
{
Global.DateFormat = format;
}
I am calling the function and storing the result first and then checking if it is null or not.Should I be doing this way or Should I check if it is null or not in the CustomerBLL.cs file itself?
What is better way to do this.Is my way of calling the function GetDateFormat() feasible.
Also is this approach of maintaining Linq queries in such class files and then calling them from code behind considered to be a good practice?
Can somebody tell me if I am heading in the right direction or not?
Any suggestions are welcome.
Your way of calling the function is ok. However, you should check for null within CustomerBLL.cs.