Calling Linq To SQL queries residing in classes fr

2019-08-07 09:00发布

问题:

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.

回答1:

Your way of calling the function is ok. However, you should check for null within CustomerBLL.cs.



回答2:

here are couple of good examples of how to use repository pastern with web-forms

http://msdn.microsoft.com/en-us/library/ff649690.aspx

http://forums.asp.net/t/1808905.aspx/1?Repository+Architecture+Using+WebForm+in+C+With+N+Tier+Architechure

http://code.google.com/p/nhibernate-repository-example/

http://www.expertbloggingon.net/post/2011/11/23/CSharp-Repository-Pattern-Design-Patterns-in-Action.aspx