开发者

ASP.Net Code Behind design

开发者 https://www.devze.com 2023-01-30 14:39 出处:网络
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#).

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 th开发者_运维问答e 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.


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消