Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Databinding Without Breaking Abstraction Layers

It seems to me that all the articles I can find on DataBinding are all about hooking form controls up to the database directly in the CodeBehind of an ASPX page.

Yuk!  I only just removed most of the db code from the ASPX pages in the app I've been handed and abstracted away the database layer.  Now you want me chucking it back in there?

Someone excuse my ignorance and enlighten me like the top 20 results on google did not!  Thanks!

Thomas David Baker
Sunday, October 05, 2003

You can databind to anything that exposes an IList interface. So it's perfectly possible to create middle-tier collections and use those collections, instead of raw database tables, in databinding.

Mike Gunderloy
Sunday, October 05, 2003

>> hooking form controls up to the database directly...

This is only really true if you bind to an IDataReader. If you're binding to DataSets/DataTables etc then these are totally disconnected - they don't know the database from Larry (who he?). If you build your own Domain objects then you can bind to these too.

Duncan Smart
Monday, October 06, 2003

Now THAT is exciting.  I really should have read up on this language before assuming it was identical to Java in every respect and ploughing in.  (Ideal point for someone to tell me you can do it in Java too, if you can.)

Thanks guys.

Thomas David Baker
Tuesday, October 07, 2003

Hi Thomas,

First, sorry for not continuing responding to your original query, I've been off for the last few days.

Anyway, glad you went into the right direction with DataBinding.

On the point of similiar facilities in Java. If we talk about data binding than every complex control in Java has a DataModel interface defined for it which make it very easy to implement the classic MVC, binding views to whatever data you like (e.g. database, JMS topic).

Admittedly you don't have default wrappers as in .NET that will allow you to bind e.g. a label to a specific column in the db via the IDE, but then there are some helper classes in more "enterprise-oriented" environments as e.g. JBuilder that will allow you to do exactly the same.

Personally, I find facilities to create custom data models easily in Java more useful than the data-binding schema in .NET.

As often .NET provides more a UI-based programming support (for those who do everything in the design mode in Visual Studio) at the costs of limiting the implementation.

Obviously, that's just my personal view ;)

Finally, on the point of assuming that C# is pretty much like Java one must be very careful as although they appear very similar there are some fundamental differences in implementation of some basic OO concepts.

For example, consider the implementation of the dynamic binding. Suppose the following structure:

class ComponentA
{
    public void DoSomething()
    {
        DoA();
    }
}

class ComponentB : ComponentA
{
    public void DoSomething()
    {
        DoB();
    }
}

then, in the main code:

ComponentA cA1 = new ComponentA();
ComponentA cA2 = new ComponentB();

cA1.DoSomething() -> calls DoA()
cA2.DoSomething() -> calls ... DoA() in C# and DoB() in Java

I've come across an example in one (otherwise useful) C# book that recommends if you had a collection of generic ComponentA that actually holds both ComponentA and ComponentB objects to add type information to the collection, so you have pairs objectA - real type that you need to check and cast down the object to ComponentB when necessary => does this sound very OO? - I doubt ;)

Admittedly .NET provides a way around it - you have to declare DoSomething() in ComponentA as "virtual" and then use "override" in front of DoSomething() in ComponentB, so it is not a big issue. It is however a good illustration how one might fall into traps while assuming similar behaviour for Java and .NET.

id
Wednesday, October 08, 2003

id, that code you gave actually gives a compiler warning:

test.cs(12,17): warning CS0108: The keyword new is required on 'ComponentB.DoSomething()' because it hides inherited member 'ComponentA.DoSomething()'
test.cs(5,17): (Location of symbol related to previous warning)

which should be your first clue that you've done something different than intended.

(All warnings to errors to good developers, right?)

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, October 08, 2003

Indeed Brad:
  csc /warnaserror
:-)

Duncan Smart
Wednesday, October 08, 2003

Brad,

True, there is a warning that would be a clue enough for a good developer, though than a good developer probably knows the difference already ;)

The point of example was to illustrate the language difference that does not stand out enough to stop you from implementing code based on a potentially incorrect assumption of behaviour because "this how it was in Java and C# is just a MS version of it"  ;).

id

id
Thursday, October 09, 2003

Then when you hear people making that false claim, correct them. "C# and Java both share C++ as their base language, and there are numerous differences, not the least of which is whether methods are virtual by default."

See? Problem solved. :)

Brad Wilson (dotnetguy.techieswithcats.com)
Saturday, October 11, 2003

*  Recent Topics

*  Fog Creek Home