如何在三层架构中绑定下拉菜单

发布于 2024-12-12 01:13:25 字数 77 浏览 0 评论 0原文

下拉菜单需要如何以专业的方式绑定? DropDown组件是否需要传递给BAL(业务访问层)?

请提供一些代码片段和参考链接。

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍暮颜 2024-12-19 01:13:25

我通常会在我的业务层中有一个方法,它返回一个自定义类列表(例如:对于状态,我将有一个具有 ID、名称、代码等公共属性的状态类)。然后我会将其绑定到下拉列表 )

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

上面的代码在我的 UI 层中执行(在 aspx 页面的代码隐藏中

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)

还如梦归 2024-12-19 01:13:25

您可以传递保存数据的类的数组..该类应该具有公共属性,以便可以访问它们并将它们绑定到您的下拉列表..或者您需要将数据绑定到的任何控件

you can pass an array of the class holding your data .. the class should has public properties so they can be accessed and bound to your dropdownlist .. or which ever control you need to bind data to

硬不硬你别怂 2024-12-19 01:13:25

UI层必须处理UI操作,同样适用于业务层必须处理业务操作。如果您将 DropDown 对象发送到业务层,则会违反原则,这只会​​使您的代码更加混乱,更难以维护且更难以理解。

由于 DropDown 控件是专门包含在 UI 中的控件,因此我建议您请求从“业务层”绑定数据,并将控件绑定到“UI 层”内的数据。

简而言之:让各层之间的交互处理数据,而不是信息。可视化控件是一种信息,是具有非常特定用途、处理/包含操作、规则的数据的聚合。客户列表本身只是一个客户列表,纯粹的数据,没有任何规则、操作或任何东西。

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.

在风中等你 2024-12-19 01:13:25

不,下拉菜单肯定不需要传递到任何较低层,像 BL 或服务层这样的较低层最终应该由 UI(表示层)调用并使用检索到的数据,可能不依赖于数据库技术或 DAL数据访问层中使用的逻辑。

请参阅此答案以获取一些示例: MVC3 和实体框架

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文