DataSource Controls and Declarative Programming

My personal thoughts and opinions, along with a bit of history, on the Whidbey DataSource control model, as well as the idea of making programming more declarative.

Just catching up with some reading... Fritz Onion posted his thoughts on DataSource controls, and while his story is "to-be-continued", one question I sense, is "what is a DataSource control after all?" Is it just a representation of a DataSet? Elsewhere, I have seen comments about these controls breaking the n-tier model. Hopefully this post will give me a chance to air out my own thoughts in this area, and a declarative programming model.

First a little history...
The DataSource/DataBound control pair first emerged as a model in Web Matrix. We didn't have a true code-behind model, so there was no way to support the InitializeComponent-based DataSet. We needed a declarative, no-code solution for data. The result was MxDataGrid and SqlDataSource. Shortly thereafter Whidbey planning kicked into full gear, and one of the themes was developer productivity. We looked at the patterns that had emerged (and frankly were learning ourselves) with v1. 90% (maybe even more) of pages had the same data-binding code over and over (examples in Fritz's post). The experience with the WebMatrix work showed controls could be smarter. We just needed to use the server control model more fully! This established the direction for data...

Not just no-code...
An important goal of the model is to hide the complexity of the manual data-binding pattern. For example, to get desired behavior, one had to DataBind in Page_Load if IsPostBack == false, and also in Page_Init if EnableViewState == false to get events to fire as expected. Successful data-binding required deep knowledge of the underlying page lifecycle and state management. DataSource and DataBound controls work together to hide all that.

As an example, DataGrid was capable enough to build a table, render it, and handle button clicks, and that's about it (I say that since I implemented it). To do anything more, one had to handle appropriate events and tell it what to do next. In a sense, it had hit a ceiling... and page code was its out. From that perspective, DataSource controls are just a new out. The grid can talk to it, and intelligently take the next step to provide default behavior for those 90% cases. Of course being a framework, we did provide a whole slew of events to allow one to change the default behavior as well as preserved the v1 model.

With the smarter controls (controls that understood the page lifecycle, its events, and state management, and could fully utilize the underlying infrastructure), we added features like declarative parameters, transparent data caching, server-side paging ability, hierarchical data support, and ability to work asynchronously (ok, the last one is just architecture... we don't have any built-in async controls). These features would require much more code than simply calling DataBind.

Are DataSources simply an abstraction over a DataSet? I wouldn't say so. I think they are a simple abstraction for data access, i.e. for getting data into the UI tier, and pushing changes back into the data tier, independent of the actual storage.

Are data sources a quick and dirty tool that break the n-tiered model? Again, I don't think so. ObjectDataSource illustrates using data sources while keeping data access and business logic separate from UI. The model doesn't prevent one from writing a DataSource that works against a custom data access layer. The SqlDataSource is just one of many tools in the ASP.NET toolbox, and should be used when it is the right thing for the job. The point is, a DataSource control simply exists as a counterpart to the DataBound control so the latter can work intelligently.

Declarative programming model
I am a believer in this. I see markup as the new code albeit a more designable form of code. Wire-up glue shouldn't have to be expressed in code (atleast for mainline scenarios). I'd like to see a model where components encapsulate behavior and patterns, and are wired together declaratively (might even make MVC approachable!). Yes, there is still code... in fact, lots of it... in components and in the framework. I am a developer myself. :-)

I am particularly interested in comments on the subject of declarative programming. I know Whidbey is just a start, and more needs to happen before it becomes a real "programming model." But can it become real and capable of covering most of the typical application scenarios? Thoughts?

Posted on Tuesday, 5/17/2005 @ 7:36 AM | #ASP.NET


Comments

9 comments have been posted.

Javier G. Lozano

Posted on 5/17/2005 @ 9:00 AM
In a previous life, I was part of architecture team in charge of developing a framework that used declaritive methodologies to connect UI components with business objects and business objects to their respective persistance layer. We developed the framework in such a way that you defined task X in XML for an specific action within a page (handling post, data retrieval, etc) and it was passed to an interpreter written in Java which in turned used internal components to perform such task.

What we learned from all of this is that as the tasks became more complex or specific, the logic/components used by the interpreter became larger and bulkier (part of this problem was because of poor framework design) and that our internal developers had a really hard time understanding how to use the framework to their advantage.

There's definitely a gain in development for those developers that understand the concept and are willing to see it as a tool that eases customization, but in the other hand those that struggle with it see it as another road block.

Bertrand

Posted on 5/17/2005 @ 4:37 PM
I've blogged some time ago about the DataSource controls breaking (or not) the n-tier model:
http://weblogs.asp.net/bleroy/archive/2004/07/12/180921.aspx

Michael Teper

Posted on 5/17/2005 @ 11:14 PM
I think the problem I and others are seeing is that this model mixes declarative layout with declarative behavior thus throwing us right back to the days of the spagetti code. Thus the declarative approach is fine, but needs to be segregated out of UI. Give me an .ASPX for layout, .CS for code, and .AS?X for declarative "glue" and I'll be much happier with the abstraction because I will be able to hand the .ASPX to my designer without fear of her breaking behavior and the .AS?X to the analyst without fear of her breaking the layout.

Marc Hoeppner

Posted on 5/18/2005 @ 12:05 AM
Having completed a couple of prototypes with ASP.NET v2 I have to say I still have about the same amount of 'code', of course most of it in declarative form, but still some C# code. I was quite excited initially, but the more I use this paradigm the more I see the downsides: a vast amount of code goes into a angle brakets form in a different (aspx,ascx,...) file in a WSDL-like form, meaning it gets very hard to read and to understand. I bet that most guys here will have an easier time understand the C# equivalent instead of deciphering the ASPX code. Also, some code still is C#, so now you have two 'code' parts in two different files that interact by an 'invisible' event model which you have to know intimately before anything works right (I mean, anything more complex than the non-sense samples for ASP.NET v2 you can find on the web nowadays...). So, it really is a mixed blessing...

Barry Kelly

Posted on 5/18/2005 @ 2:17 AM
I'm currently one half of a team of two developing an architecture that sounds somewhat similar to Javier's architecture above. Our web controls have properties that are reflected from the business layer via a simple functional language. So, the value of some data-bound textbox on a web page has its Value property calculated from an expression, e.g. "FirstName", where FirstName is the name of some property in the data layer or business layer.

The converse, where a value is entered into the textbox and propagated to the server, calls an event on the server-side control. This event is implemented in the language too, but as a single statement: "FirstName = value", for example.

Part of our architecture is our unique data model. Here, FirstName may either be a column in a row in a table (generally all our column names are unique across all tables, but column names can be resolved by table name too if necessary), or it may be overridden in the business layer to become a business-layer Property, if you will. Also re our data model: it's all in a custom disconnected data set kept on the server(s) but without the overhead of streaming a big object graph per server connection (that's part of our secret sauce); having said that, the data is often small enough to use the ASP.NET VIEWSTATE-style strategy where desireable.

Our architecture is pretty good (we think, humbly!), and a whole lot more flexible (in a business sense) and quick to develop than ASP.NET.

Nikhil Kothari

Posted on 5/19/2005 @ 12:43 AM
Good discussion! Would love to continue hearing opinions and experiences. Exactly my intent with this post...

One (simplistic?) view of the declarative programming envisioned here is about making the controller part of the MVC model declarative via things like bindings, triggers, workflows etc. The view has mostly been the only declarative aspect so far. Some of the model can also be defined declaratively (for example, look at Profile in Whidbey), but things like entities, business objects, data transformers etc. will probably remain in code. I see these as pseudo-components that do not belong in the code-behind to start with. In fact, the idea is could you go so far as to not require a code-beside, so you don't suffer from the problem of the invisible event model? Given the right level of smart components, could you look at a page, and understand how it works, as opposed to looking at the angle brackets?

Yes, I agree, a primary means of evaluation of this model is whether its percieved as a road block, or an enabler. I don't quite see this turning into spagetti code, because code is tucked away into components, and into app-specific model representations.

Marc Hoeppner

Posted on 5/19/2005 @ 9:54 AM
Ok, you asked for it, so here are my 2 cent…

Well, if you want to kill all code-behind you’ll need to cover a large number of different scenarios and I don’t think v2 is anywhere near nor do I perceive this goal to be overly realistic, just from my perspective. Just building a ‘simple’ GridView with edit/insert templates, in-place editing, two interconnected dropdowns (for postal code and corresponding streets) and a new button already breaks the declarative-only mode. If you even throw in stuff like out-of-band postbacks and client-side code, declarative mode IMHO makes it even more difficult than before. Also, GUI support will have to go a long way before it really supports that adequately, just switching between different edit/insert templates for a FormView can be a test to one’s patience in a real-world scenario (= more than 1-2 fields), let alone getting a good grip of the current connections between all the datasource items, controls and their events you can see on the screen. Next, the separation between UI and code gets diluted, so when I get a bunch of ASPX/ASCX files to my graphics guy (or gal), he’ll have a hard time wading through the declarative code as no doubt he will work in text view instead of WYSIWYG view 90% of his time. Yes, I do have master pages, themes and skins, but I have a feeling that not all GridViews will look alike in a project and you’ll still have some do a little touch-up here and there. Then, if one has to compare two versions from source control, I am sure you will agree that a C# diff will be a lot easier to understand than an ASPX one. And yes, I do think it will be necessary to do so from time to time. In the end, I wonder, if the amount of code roughly stays the same, albeit in a different form, does it make sense to go down declarative-avenue at all!? – I am sure that many hobby programmers/small apps will gladly (and successfully) use that paradigm, but I am not sure about the (so called) Enterprise dev. So, unless you have a perfect way of covering all possible scenarios (meaning more than 95% or so) and support all these with an omnipotent GUI, you will sure need code-behind and in turn you inevitably will run into the event model ‘black hole’. All in all, I still have mixed feelings about this, but there is more than one way to do things in ASP.NET which is giving me the option to go a different, more programmatic route (g)– Two-way data binding is a real (and long awaited) killer feature for me…

Martin Ennemoser

Posted on 12/5/2005 @ 2:57 PM
I think a lot of the success of declarative markup depends on the availability of good designers for it. Building designers for markup is easier than building them for code. I wouldn't expect to write less code using declarative markup. Sometimes I'm sure it's even more. But if a good amount of markup is created by an intelligent, easy to use designer, it helps to increase productivity. It also helps you to quickly build prototypes. E.g. in ASP.NET 2.0 you can build a prototype using SqlDataSource, replacing it by ObjectDataSource for the final version.

Calvin Guo

Posted on 3/20/2006 @ 7:02 PM
I also see all the datasource controls is problematic, as we see markups does not belong to the UI showup in the UI files. I used to work with a solution that tries to do everything declaritively, event wiring, if/else/and/or logic..., etc. As a matter of fact, if you work in the team for 6 months, you found yourself writing 100 lines of code, most of the time, you are manipulating the XML markups.

The feedback from the developers are: It's way too complicated, and way to limited. Developers have to learn this new XML model, and they have to know the limitation. Because thet need to know when to get back to C# code. It just becomes a problem for any new developers come on board. And for expereienced developers, they simply found their productivity worse than work with C# code.


Unfortunately, MS is trying to push this direction, as I see some functionality is just unavailable if you don't use DataSourceControl. for example, for a GridView, if I don't use DataSourceControl, it's almost impossible to implement automatic sorting. I override the OnSorting method to take care the sorting myself (I get a sorted datasource, and bind to the gridview.) But the SortDirection simply won't toggle! But if you use a datasourceid, don't know why, the SortDirection start to toggle itself!
The discussion on this post has been closed. Please use my contact form to provide comments.