Archive for November 19th, 2008

19
Nov

Dynamic Data: Models, MetaModels And Everything In Between

In the previous article, I mentioned that the existing data controls in WebForms aren’t very smart as regards to your data, but in all fairness, it isn’t really their fault. How is a GridView supposed to know that a property is required? How can a FormView know that a specific control should include regular expression validation? How can a DetailsView know that certain columns shouldn’t be displayed? Since those controls are completely agnostic as to the type of data they are bound to, there is no set of common assumptions they can make about their bound data in order to provide a richer set of UI behaviors.

It almost seems like what we need is some sort of meta-model, that provides additional semantics/metadata on top of our data model that our WebForms controls can leverage to make the desired assumptions. That way, we could bind any type of data to the controls, and they wouldn’t have to rely on any specific model type, but could rather use the meta-model to determine the additional actions it should (or shouldn’t) take. Because the meta-model would have a common form, the controls would only have to be aware of a single metadata system.

While having a common metadata representation is great, how can a meta-model be created based on every possible type of data? Obviously that wouldn’t be practical to have a single codebase that determined metadata from every data type. Rather, what we’d need is the ability to create a provider that was aware of a specific data type and could create a meta-model from it. This way, allowing a new data type to work with our common metadata system would be as easy as creating a new provider.

The solution described thus far is exactly what Dynamic Data provides. A common meta-model system for describing data models with a higher semantic that a UI can leverage to be smarter by default. Out of the box, it includes providers for LINQ-To-SQL and the Entity Framework (which is why it has gotten a reputation for working solely against databases), but nothing stops it from working against any other data source. In fact, prototypes already exist that have Dynamic Data working against an ADO.NET Data Service, as well as a SQL Data Service.

So what does the code look like for creating a meta-model from an existing data model?

public class Global : HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        var model = new MetaModel();
        model.RegisterContext(typeof(AdventureWorksContext));
    }
}

In that code sample, “AdventureWorksContext” is the name of my ObjectContext (a generated class used for working with an Entity Data Model). It could have also been a DataContext (a class used for working with a LINQ To SQL model), and the MetaModel would have been created just fine. This is because the RegisterContext method is aware of L2S and EF models and will created the necessary provider to fill the MetaModel. What if your data source is something other then a L2S or EF model? Then you can just pass an instance of your custom provider to the RegisterContext method.

var model = new MetaModel();
model.RegisterContext(new ArbitraryDataModelProvider());

Your provider would then be responsible for traversing your data source and retrieving the metadata that described it. The first MetaModel that you create is considered the default instance, and is stored in the static MetaModel.Default property. MetaModels can also be retreived by type using the MetaModel.GetModel method (in case you have multiple). Once you’ve created your MetaModel during your application’s start event in the Global.asax file, your MetaModel is now globally available throughout your application, just waiting to be leveraged by your UI.

While this all seems dandy, two questions arise: what metadata does the MetaModel expose, and where did it get it from? I’ll cover those questions in the next article.

19
Nov

Dynamic Data: Kickin’ It Old School

There have already been articles written illustrating how to leverage Dynamic Data within an existing web application, but instead of just outlining the steps required to do so, I’d like to give a little contextual explanation as to why you might want to do that, and what benefits may exist.

If you’re developing data-driven web applications with ASP.NET today then you’re already making use of the rich server-control offerings provided (e.g. GridView, ListView, *DataSource), and can attest to the productivity boosts gained (most of the time). What you lack though, when using the existing server-controls, is any notion of data-centricness. Your UI is completely stupid as regards to your data, and requires complete maintenance/upkeep whenever a change is made to your model, no matter how minute it is.

Imagine I’m using a GridView to provide an inline-editing tabular experience to my users. Within the GridView I configure the columns I want to display (or use AutoGenerateColumns), what each column’s header text is, what each column’s sort expression is, and what the primary key is (i.e. DataKeyNames). In addition, since I’m using the control’s inline-editing functionality I have to determine whether I want to allow certain columns to use the default TextBox rendering or provide a custom edit mode by creating a template. Even if I’m fine using the default edit mode provided by a BoundField, I might need to have some validation, which I won’t get out of the box, so I have to create a template. These decisions go on and on…

If you read through every requirement in the above paragraph, you may be thinking that some of that could easily be deduced from our data model (e.g. primary keys, required fields). I would go so far as to say that all of that, and more, should be easily deduced from our data model, and that is exactly the functionality that Dynamic Data provides. Dynamic Data provides the glue between your data model and your UI, making your UI able to automatically reflect model-level changes.

How does Dynamic Data provide that functionality exactly? Remember in the last article I mentioned that Dynamic Data is merely a set of extensions to ASP.NET, which means you don’t have to learn some completely new API, but rather a handful of new tools that work along with what you already know. From the perspective of adding data-centric functionality to an ASP.NET application, what we need in order to solve the problems outlined above, and what Dynamic Data provides us, can be broken down into three categories:

  1. Data-smart server controls
  2. Field templates for re-usable field-level UI
  3. Higher-level abstraction of our data model

Understanding why those three aspects are necessary and how Dynamic Data leverages them will make the goals of Dynamic Data much clearer. In the next article I’ll explain in detail how those three features can enhance the development of data-driven web applications.