ASP.Net 代码背后设计

发布于 2024-10-07 21:59:08 字数 431 浏览 5 评论 0原文

我正在创建一个小型 asp.net 应用程序,其中包含多个 .aspx 页面。 我不确定如何设计我的代码(c#)。

示例: 让我有一个网页,从数据库加载书籍并将其名称放入 HTML 列表中。 我的代码背后需要: 1)

  1. 从DB获取图书数据。
  2. 创建 HTML 列表
  3. 将列表放入页面源代码中。

我发现实现这一点的一种简单方法是:

  1. 从数据库加载数据并格式化
  2. HtmlFactory 类,该类从对象创建 HTML 字符串
  3. 简单的属性绑定,将生成的 HTML 代码放入页面中

我想有更干净的方法来执行此操作多于。 我了解MVC,但我猜还有更多设计代码的模型。

请描述您对上述的使用方式或相关数据的链接。

I am creating a small asp.net application which contains several .aspx pages.
I am not sure about the way to design my code behind (c#).

Example:
Lets day I have a web page that loads books from DB and put their names in a HTML list.
My code behind need to:
1)

  1. Get the books data from DB.
  2. Create the HTML list
  3. Put the list inside the page source.

One simple way I've found to do it is by:

  1. Class to load data from DB and formatting
  2. HtmlFactory class that create HTML string from objects
  3. Simple property binding to put the generated HTML code inside the page

I guess there are cleaner ways to do the above.
I know about MVC, but I guess there are more models for designing the code.

Please describe the way you use to the above or link to relevant data.

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

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

发布评论

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

评论(2

梦巷 2024-10-14 21:59:08

ASP.Net 附带了许多可以轻松处理数据的控件。对于您提供的示例,我将研究使用 Repeater 控件轻松将从数据库获取的书籍列表绑定到您的页面。其他数据绑定控件包括:

  • GridView
  • ListView
  • DetailsView
  • FormView
  • DataList

所有这些都可以轻松地将数据绑定到页面。

最好的办法是通过分离功能来对应用程序进行分层,例如。业务层、数据访问层、表示层等。我一直推荐的一系列优秀文章是 Imar Spaanjar 的 构建 N 层应用程序

我已经有一段时间没有访问过他的博客了,但你可能还会发现一组类似的 MVC 文章,但我对此不确定。

ASP.Net comes with a number of controls that make working with data easy. For the example that you have provided I would look into using the Repeater control to easily Bind the list of books that you get from the database to your page. Other Databound controls include:

  • GridView
  • ListView
  • DetailsView
  • FormView
  • DataList

All of which make it easy to bind your data to your page.

The best thing is to Layer your application by separating the functionality, ex. Business Layer, Data Access Layer, Presentation Layer, etc. An excellent series of artciles that I have been recommending are the ones by Imar Spaanjar on Building a N-Layer Application.

It has been some time since I have visited his blog, but you might also find a similar set of articles for MVC, but I am not sure about that.

独享拥抱 2024-10-14 21:59:08

如果您使用标准 Web 窗体 ASP.Net(即不是 MVC),我将使用 ListView 控件来实现您所描述的内容。这个想法是您指定一个 LayoutTemplate 和一个 ItemTemplate,并将数据绑定到您的列表。

我会避免将 HTML 构建为大量的长字符串并将其注入到您的页面中,当它不可避免地中断时,调试此类事情并不有趣,而且很可能会成为性能狗。 ASP.Net 控件正是为了实现这一目的,并受益于数千/数百万工时的开发和测试。

我的经验法则是,HTML 是由为此目的而设计的控件生成的,而代码隐藏仅用于设置这些控件的属性。

一般来说,对于您所描述的内容,我会使用 LinqToSql 或更可能的实体框架来包装我的数据库访问。根据项目的复杂性,我可能会将其抽象到存储库后面,但前提是值得这样做。 Linq/EF 上下文代表您所描述的 (1),即仅负责数据库访问的单个类。

然后,我将在页面中使用 ListView 控件,并在页面加载时绑定它。

例如:

<!-- rest of page -->
<asp:ListView ID="ListView_Books" runat="server" ItemPlaceholderID="itemPlaceholder">
   <LayoutTemplate>
      <ul>
         <asp:Placeholder ID="itemPlaceholder" runat="server" />
      </ul>
   </LayoutTemplate>
   <ItemTemplate>
      <li>
         <%# DataItem.Title %>, by <%# DataItem.Author %>
      </li>
   </ItemTemplate>
</asp:ListView>
<!-- rest of page -->

我的代码后面会有类似(VB)的内容:

Protected Sub Page_Load()
   ListView_Books_Bind()
End Sub

Protected Sub ListView_Books_Bind()
   Dim dataContext As New MyDataContextClass
   ListView_Books.DataSource = dataContext.Books
   ListView_Books.DataBind()
End Sub

Protected Readonly Property DataItem As Book
   Get
      Return DirectCast(Page.GetDataItem(), Book)
   End Get
End Property

编辑 - 合并保利的评论 - 谢谢!

If you're using standard Web Forms ASP.Net (i.e. not MVC), I would use a ListView control for what you're describing. The idea is that you specify a LayoutTemplate, and an ItemTemplate, and databind to your list.

I would steer WELL clear of building HTML as massive long strings and injecting it into your page, debugging that kind of thing is no fun when it inevitably breaks, and it's likely to be a performance dog. ASP.Net Controls are there to do that exact purpose, and have the benefit of thousands/millions of man-hours of development and testing.

My rule of thumb is that HTML is generated by controls designed for that purpose, and the codebehind is just for setting properties on these controls.

Generally speaking for what you described, I would use LinqToSql or more likely Entity Framework to wrap my database access. Depending on the complexity of the project, I might abstract that away behind a repository, but only if it merits doing so. The Linq/EF context represents what you describe as (1), i.e. a single class with responsibility only for database access.

I would then use a ListView control in the page, and bind it on page load.

For example:

<!-- rest of page -->
<asp:ListView ID="ListView_Books" runat="server" ItemPlaceholderID="itemPlaceholder">
   <LayoutTemplate>
      <ul>
         <asp:Placeholder ID="itemPlaceholder" runat="server" />
      </ul>
   </LayoutTemplate>
   <ItemTemplate>
      <li>
         <%# DataItem.Title %>, by <%# DataItem.Author %>
      </li>
   </ItemTemplate>
</asp:ListView>
<!-- rest of page -->

My code behind would then have something like (VB):

Protected Sub Page_Load()
   ListView_Books_Bind()
End Sub

Protected Sub ListView_Books_Bind()
   Dim dataContext As New MyDataContextClass
   ListView_Books.DataSource = dataContext.Books
   ListView_Books.DataBind()
End Sub

Protected Readonly Property DataItem As Book
   Get
      Return DirectCast(Page.GetDataItem(), Book)
   End Get
End Property

Edit - incorporated Pauli's comment - thanks!

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