• ASP.Net Dynamic Data (Scaffolding with .net)

    Published by jcisneros on August 5th, 2008 8:40 pm under .NET, SP1

    1 Comment

    image When you build data centric web applications, a big part of the code is related to the implementation of CRUD (Create, Read, Updated, Delete) operations. Scaffolding is a mechanism to generate fully functional data driven applications based on metadata inferred from a model, basically, all the CRUD operations are implemented by using the metadata obtained for each entity/table in the model, making it a very useful tool for prototyping.

    That is what ASP.Net Dynamic Data does, it is a scaffolding technology that uses templates (ASP.Net Web Pages) to dynamically generate functional CRUD pages. Like Ruby on Rails, Dynamic Data makes a high use of conventions to infer how the results will be rendered, but it can be highly customized in an really easy way by adding metadata to the model or modifying/adding templates. This allows to start with a raw prototype and continue customizing and tuning it ’till a mature application.

    Getting Started

    Once you got the bits installed, you are able to create a new project using one of these two templates:

    • Dynamic Data Web Application: Creates a Dynamic Data web application using a LINQ to SQL data model to generate the scaffolds.
    • Dynamic Data Entities Web Application: Creates a Dynamic Data web application using a Entity Data Model (ADO.NET Entity Framework) to generate the scaffolds.

    The main difference between both, is the data model that Dynamic Data will use as data source and to infer the metadata needed to do the scaffolding.

    The Data Model

    image As said before, the data model can be LINQ to SQL or Entity Data Model depending on the project type selected.

    While LINQ to SQL is tightly bound to the database schema (each property is bound to a specific field in a table), Entity Framework is more flexible and loosely coupled and you can query for entities using either LINQ or Entity-SQL.

    When creating the data model is recommendable to create a Model folder to place the data model file (.dbml or .edmx) and the additional classes that we will create through the development to enhance the metadata.

    Enabling Scaffolding

    This is the easiest thing, to enable the scaffolding you just need to modify a simple line of code.

    In the global.asax.cs file is a method named RegisterRoutes where you will find a big section of commented description on how to register a data context to be used as the model to generate the scaffolds.

    To enable scaffolding for all the classes/tables in the data model, you need to register the model in this way: (un-comment the following line and modify the bold parts)

    model.RegisterContext(typeof(Model.MyDataModelContext), new ContextConfiguration() { ScaffoldAllTables = true });

    Just modifying the previous line your application is ready to run, just press F5 and enjoy the magic!

    If you want to generate scaffolds for specific classes/tables you have to set the ScaffoldAllTables property to false and then decorate the classes you want to scaffold with the [Scaffold(true)] attribute.

    image

    Adding Metadata

    The generated scaffolds have all the common functions of a data-driven application, the list are filtered, sorted and paged, the fields in the edit and insert forms are validated according to its data types, etc., but it only does the best it can with all the metadata that can retrieve from the data model, therefore you will find that many things that can be improved or refined.

    To add metadata to the model, Dynamic Data provides a full set of attributes under the System.ComponentModel.DataAnnotations namespace to be applied to the classes in the data model. But instead of applying the attributes directly over the generated code of the model you have to create partial classes aimed just to add metadata to the model.

    Here, we’ll face a problem since a partial class can not redefine a property, thus we won’t be able to add metadata to the properties. This problem is solved by using an inner class and associating it to the data model class with the MetadataType attribute.

    The following is a sample of a data model class using an inner class to refine the model:

    [MetadataType(typeof(ProductMetadata))]
    public partial class Product
    {
    private class ProductMetadata
    {
    [StringLength(30)]
    [Required(ErrorMessage = "Product name is required.")]
    [DisplayName("Product Name")]

    public object Name { get; set; }

    [Range(0, 10000, ErrorMessage = "Standard Cost should be between {1} and {2}.")]
    public object Price { get; set; }
    }
    }

    Since the inner class porpoise is to add meta data, the properties only need to match in name and not in type (in the sample, all the properties are of object type).

    In this post by MarĂ­a Wenzel you will find a list with some of the attributes available: Dynamic Data Attributes.

    Customizing the Templates

    Under the folder DynamicData you will find all the templates that Dynamic Data uses to generate the scaffolds. You can modify them to change the look and feel, or to add new functions for all the tables, but if you want to customize the templates for an specific table, you can, just follow these simple steps:image

    1. Create a new folder with the name of the table you want to customize under the DynamicData\CustomPages.
    2. Copy from DynamicData\PageTemplates the aspx templates you want to customize (i.e. List.aspx) and paste in the new folder.
    3. Delete the code behind file of the new template (i.e. List.aspx.cs and List.aspx.designer.cs).
    4. Customize the new template at your desire and using all of your creativity.

    Notice the use of conventions in this case that allows a friendly development experience. Dynamic Data will look for the templates, first under DynamicData\CustomPages\[TableToRender] and then under DynamicData\PageTemplates.

    Conclusions

    With ASP.NET Dynamic Data you can experience a nice development experience, it is easy to learn and to use. Some people may say that scaffolding is just for prototyping, I agree on that, but I found Dynamic Data very flexible and customizable, you may use it for a part of the application (where only CRUD operations are needed) and, for the rest of the application, continue developing as you are used to. Something that I noticed is a little of lack of performance, I think that it is caused by the use of reflection to get the metadata from the model. Anyway, it is a very interesting and nice framework that worth the try.

    Tags: ,

  • 1 Comment:

    1. Olivier Giulieri said on February 24, 2009:

      I’m working on an open source framework for CRUD. It covers the UI and ORM together with the same metadata.

      http://www.evolutility.org

    Leave a comment

    Your email address will not be published.