Search code examples
c#asp.netcode-behindbusiness-logic-layer

Should I use this function in my code behind or BusinessLogic Class


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.


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.