2012-07-03 30 views
1

我使用ASP.NET Web Forms/C#。我有一個頁面Customer.aspx。我創建了CustomerBLL類,其中我計劃使用Linq To SQL查詢來執行所有database相關任務。調用Linq從後面的代碼中駐留在類中的SQL查詢。

考慮這個例子。下面是我的CustomerBLL類中的一個名爲GetDateFormat()的方法,它返回日期格式爲database。這裏是代碼。

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; 
      } 
     } 

    } 
} 

code behind我在這樣的另一個函數裏面調用這個函數。

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; 
      } 

我打電話的功能和第一存儲結果,然後檢查是否爲空或not.Should我做這樣或我應該檢查它是否爲空或不在CustomerBLL.cs文件本身?

什麼是更好的方法來做到這一點。是我的方式調用函數GetDateFormat()是可行的。

也是這種方法在這樣的類文件中維護Linq查詢,然後從code behind調用它們被認爲是一種好的做法嗎?

有人可以告訴我,如果我正朝着正確的方向走?

歡迎任何建議。

+0

你應該看看倉庫模式 –

+0

@COLDTOLD存儲庫模式可以與Web Forms一起使用。可以指出關於此主題的幾篇文章。如果可能的話。謝謝。 – freebird

+0

請詳細解釋並解釋清楚的問題 –

回答

1

您調用該函數的方式可以。但是,您應該在CustomerBLL.cs中檢查null。

+0

因此,您建議我應該檢查.cs文件中的空值,並在後面的代碼中使用結果。 – freebird

+0

是的,不要稱之爲cs文件。把它叫做業務邏輯類。 –

+0

所以你認爲我的方法是推薦的,如果有邏輯執行,那麼我應該在代碼後面做? – freebird

1
+0

我會通過鏈接,並在有進一步的幫助情況下回復you.Thanks。 – freebird

相關問題