Question

I am using ASP.NET Web Forms/C#.

I am using this function to fill City DropDownLists based on the selected state from State DropDownList.There are 3 DropDownLists for State and City.(Residential,Native,Office).

Here is my function.

public void CityFill(int index,int id)
{          
    var city = CustomerBLL.GetCities(index);

    //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());
        }
    }
}

This function based on the id will fill appropriate City DropDownLists based on id argument.

This is currently in code behind.My question is whether this function should be used here or should I move it to my Business Logic class. Here is my CustomerBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using System.Web.Services;
using System.IO;
using System.Xml.Linq;
using System.Web.Security;
using System.Text;

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

What is a proper approach.Can anyone guide me on this?

Any suggestions are welcome.

Was it helpful?

Solution

Should be on your Business Logic Layer. One of the reason is that you also want to use this functionality on other forms, So, you just call it.

The BLL would handle things that are a part of the business domain, not a part of the database, and not a part of the UI (usually). For example, using the age of a customer to determine if they qualify for a special senior's discount. The DAL shouldn't be doing this, it should simply be retrieving the customer data, and then storing it with the discount data after the BLL has done its work.

  1. It keeps all of your business logic localized, and in one place. Future changes will be much easier as a result.
  2. It allows you to more easily unit test your business logic. This is a big one. It's very difficult to write automated unit tests against your business logic if this code is tightly coupled to your web page, or windows form.
  3. It keeps your UI much slimmer.

OTHER TIPS

Proper approach would be separate adding items to dropdownlist and business logic:

// this goes to business logic
public CitiesView GetCities(int id) {
       var cities = new CitiesCollection();
       var city = CustomerBLL.GetCities(index);

        //Loop through all the cities in st object
        foreach (var c in city)
        {
            //If id=0 then fill all dropdowns
            if (id == 0)
            {
                cities.Residental.Add(c.city_name.Trim());
                cities.Office.Add(c.city_name.Trim());
                cities.Native.Add(c.city_name.Trim());
                cities.Nominee.Add(c.city_name.Trim());
            }
            else
            {
                //If 1 then fill Res City
                if(id==1)
                    cities.Residental.Add(c.city_name.Trim());

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

                //If 3 then fill nat city
                if(id==3)
                cities.Native.Add(c.city_name.Trim());
            }
        }
  }

  // this goes to code behind
  public void FillCitiesDrowDowns(int id) {
       var citiesView = GetCities(id);
       NewCustomerddlResidentialCity.Items.AddRange(citiesView.Residental);
       NewCustomerddlOfficeCity.Items.Add(citiesView.Office);
       NewCustomerddlNativeCity.Items.Add(citiesView.Native);
       NewCustomerddlNomineeCity.Items.Add(citiesView.Nominee);    
 }

 public class CitiesView {
     public List<string> Residental {get;set;}
     public List<string> Office {get;set;}
     public List<string> Native {get;set;}
     public List<string> Nominee {get;set;}
 }

In that way, you can test all your conditions without touching UI elements, you can replace UI elements without touching business logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top