Click here to Skip to main content
15,888,610 members
Articles / Web Development / ASP.NET

ASP.NET DaST to wrestle with MVC and WebForms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Mar 2013CPOL82 min read 23.2K   152   11  
DaST is a new architectural pattern for building highly dynamic Web 2.0 applications. A web page is rendered as a set of randomly nested rectangles where each rectangle is controlled individually and every combination of rectangles can be partially updated via AJAX.
// --------------------------- COPYRIGHT NOTICE ---------------------------------
// ASP.NET DaST - LittleNonsense DEMO
// Copyright (C) 2011 by Roman Gubarenko
// http://www.makeitsoft.com/
// ------------------------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// ------------------------------------------------------------------------------
// Primary Contact  : rgubarenko@gmail.com
// Project Home     : http://www.makeitsoft.com/
// Help & Support   : http://www.makeitsoft.com/forums/
// ------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AspNetDaST.Web;

/// <summary>
/// Summary description for NonsenseController
/// </summary>
public class NonsenseExample1Controller : ScopeController
{
  public override string ProvideTemplate()
  {
    return Utils.LoadTemplate("NonsenseExample1.htm"); // load temlate content from anywhere
  }

  public override void InitializeModel(ControllerModelBuilder model)
  {
    // use one binding handler to bind all nested scopes (except for root scope)
    model.SetDataBind(new DataBindHandler(DataBind_ROOT));
    model.Select("CustomerRepeater").SetDataBind(new DataBindHandler(DataBind_CustomerRepeater));
    model.Select("CustomerRepeater", "OrderRepeater").SetDataBind(new DataBindHandler(DataBind_OrderRepeater));
    model.Select("CustomerRepeater", "OrderRepeater", "ItemRepeater").SetDataBind(new DataBindHandler(DataBind_ItemRepeater));
  }


  private void DataBind_ROOT()
  {
    CurrPath().Replace("{TodayDate}", DateTime.Now.ToString("yyyy-MM-dd")); // output some example values
  }

  private void DataBind_CustomerRepeater()
  {
    var customers = DataLayer.GetCustomers(); // get all customers

    CurrPath().RepeatStart(); // zeroize repeater
    foreach (var c in customers) // loop through customers
    {
      CurrPath().Repeat(); // repeat scope for current customer
      CurrPath().Replace("{CustomerID}", c.GetValue("ID")); // bind customer id
      CurrPath().Replace("{CustomerName}", c.GetValue("Name")); // bind customer name
      CurrPath("OrderRepeater").Params.Set("CUST", c); // pass customer to next scope
    }
  }

  private void DataBind_OrderRepeater()
  {
    var c = CurrPath().Params.Get<object>("CUST"); // customer passed in params
    var orders = DataLayer.GetOrders((string)c.GetValue("ID")); // customer orders

    CurrPath().RepeatStart(); // zeroize repeater
    foreach (var o in orders) // loop through orders
    {
      CurrPath().Repeat(); // repeat scope for current order
      CurrPath().Replace("{OrderID}", o.GetValue("ID")); // bind order id
      CurrPath().Replace("{OrderDate}", o.GetValue("Date")); // bind order date
      CurrPath("ItemRepeater").Params.Set("Order", o); // pass order to next scope
    }
  }

  private void DataBind_ItemRepeater()
  {
    var o = CurrPath().Params.Get<object>("Order"); // order passed in params
    var items = DataLayer.GetOrderItems((string)o.GetValue("ID")); // order items

    CurrPath().RepeatStart(); // zeroize repeater
    foreach (var i in items) // loop through items 
    {
      CurrPath().Repeat(); // repeat scope for current item
      CurrPath().Replace("{ItemID}", i.GetValue("ID")); // bind item id
      CurrPath().Replace("{ItemName}", i.GetValue("Name")); // bind item name
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
Software Architect with over 15 years in IT field. Started with deep math and C++ Computer Vision software. Currently in .NET and PHP web development. Creator of DaST pattern, open-source frameworks, and plugins. Interested in cutting Edge IT, open-source, Web 2.0, .NET, MVC, C++, Java, jQuery, Mobile tech, and extreme sports.

Comments and Discussions