简体   繁体   中英

how to bind dropdown in 3-tier architecture

how the dropdown needs to be bound in professinal way? Does DropDown component need to be passed to BAL(business access layer)?

Please provide some code snippet and reference links..

I usually will have a method in my Business Layer which returns a List of Custom class ( Ex : for State, I will have a State Class which has public properties like ID,Name,Code) .Then i will bind that to the drop down list

            List<State> objLstState =new List<State>();
            objLstState = StateManager.GetStates();  //This method returns a list of States
            if(objLstState!=null && objListState.Count>0)
            {
              dropDownSchoolState.DataSource = objLstState;
              dropDownSchoolState.DataTextField = "Name";
              dropDownSchoolState.DataValueField = "ID";
              dropDownSchoolState.DataBind();
            }

The above code executes in my UI layer (in a codebehind of an aspx page)

您可以传递包含数据的类的数组..该类应具有公共属性,以便可以访问它们并将其绑定到您的dropdownlist ..或需要将数据绑定到哪个控件

The UI layer must deal with UI operations, same applies to the business layer that must deal with business operations. If you send your DropDown object to your business layer, you'll create a violation of principles that will only make your code messier, harder to mantain and more difficult to understand.

Since a DropDown control is something exclusively contained for the UI, I suggest that you request the data to be bound from your "business layer" and bind the control to the data inside your "UI layer".

On a short phrase: let the interactions between your layers deal with data, instead of information . A visual control is an information, an aggregation of data with a very specific use, dealing/containing operations, rules. A list of customer per se is just a list of customers, pure data, without any rules, operations or whatsoever.

No the dropdown surely does not need to be passed to any lower layer, the lower layers like BL or service layer should be eventually called by the UI (Presentation Layer) and consume the data retrieved, possibly with no dependency on the database technology or DAL logic used in the Data Access layer.

see this answer for some examples: MVC3 and Entity Framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM