2012-07-04 33 views
0

我正在使用ASP.NET Web Forms/C#將功能從代碼後移到業務邏輯層類

我在我的code behind中有這個功能,根據州DropDownList的選擇填寫城市DropDownList

這是我的功能。

public void CityFill(int index,int id) 
     { 
      //This function calls GetCities method which will get all cities of a state. 
      var city = CustomerBLL.GetCities(index); 



      //If id=0 then clear all dropdown before filling 
      //or else they get appended. 
      if (id == 0) 
      { 
       NewCustomerddlResidentialCity.Items.Clear(); 
       NewCustomerddlOfficeCity.Items.Clear(); 
       NewCustomerddlNativeCity.Items.Clear(); 
       NewCustomerddlNomineeCity.Items.Clear(); 
      } 
      else 
      { 
       //If 1 then clear residential city 
       if(id==1) 
        NewCustomerddlResidentialCity.Items.Clear(); 

       //If 2 then clear Office city. 
       if(id==2) 
        NewCustomerddlOfficeCity.Items.Clear(); 

       //If id=3 then clear Native City. 
       if(id==3) 
        NewCustomerddlNativeCity.Items.Clear(); 

       //If id=4 then clear Nominee City 
       if(id==4) 
        NewCustomerddlNomineeCity.Items.Clear(); 
      } 

      //Loop through all the cities in st object 
      foreach (var c in city) 
      { 
       //If id=0 then fill all dropdowns 
       if (id == 0) 
       { 
        NewCustomerddlResidentialCity.Items.Add(c.city_name.Trim()); 
        NewCustomerddlOfficeCity.Items.Add(c.city_name.Trim()); 
        NewCustomerddlNativeCity.Items.Add(c.city_name.Trim()); 
        NewCustomerddlNomineeCity.Items.Add(c.city_name.Trim()); 
       } 
       else 
       { 
        //If 1 then fill Res City 
        if(id==1) 
        NewCustomerddlResidentialCity.Items.Add(c.city_name.Trim()); 

        //If 2 then fill Off City 
        if(id==2) 
        NewCustomerddlOfficeCity.Items.Add(c.city_name.Trim()); 

        //If 3 then fill nat city 
        if(id==3) 
        NewCustomerddlNativeCity.Items.Add(c.city_name.Trim()); 

        //If 4 then fill nominee city 
        if(id==4) 
        NewCustomerddlNomineeCity.Items.Add(c.city_name.Trim()); 

       } 
      } 
     } 

傳遞給函數的參數是index和id。 索引是SelectedIndex的州DropDownList。 id是哪個城市DropDownList需要填寫。

下面是BLL

namespace CwizBankApp 
{ 
    public class CustomerBLL 
    { 
     public IList<mem_city> GetCities(int index) 
     { 
      using (var db = new DataClasses1DataContext()) 
      { 
       var city = db.mem_cities.Where(c => c.state_id.Equals(index)).ToList(); 
       return city; 
      } 
     } 
    } 
} 

我需要擺脫代碼功能背後BLL類。

我該如何解決這個問題。

任何人都可以幫我解決這個問題嗎? 歡迎任何建議。

+0

您不應該(不能)從bll類執行ui操作。你的代碼有什麼問題? –

+0

@SteveB這段代碼正在工作,所以你推薦它只應用於代碼後面嗎?可以嗎? – freebird

+0

每層都有其責任。 BLL層應該處理業務邏輯,DAL數據訪問和UI呈現。在這種情況下,如果我理解正確,您展示的代碼是基於某些業務價值設置UI的方式。在這種情況下,我認爲這是UI工作,所以對我而言,您的代碼看起來確定。 –

回答

1

我建議不要在BLL類中執行該操作。不要將其強制爲BLL,以便將表示邏輯與數據訪問邏輯分開。

+0

那麼我現在在代碼背後所做的是好的,你覺得呢?謝謝。 – freebird

+0

是的..這是正確的。你可以在代碼本身後面的代碼中優化它。 –

+0

感謝您的幫助:) – freebird