|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Screenshot
IntroductionWith the advent of ASP.NET, and Visual Studio.NET with its graphical tools to provide design-time databinding support, the concept of databinding has really come of age. Databinding to ADO.NET Binding ObjectsFortunately for developers who prefer to work with objects rather than Model View ControllerThe approach used here follows model view controller architecture. They are presented here as model, controller, view, simply because this is the logical order in which to discuss them. Model (the business object)I will not discuss the business object in detail as it is simply a very simple class, Controller (PersonAdapter)So far we have a business object ( public class PersonAdapter : System.ComponentModel.Component
{
///
/// provides access to our person object so a web form
/// can bind to the person's properties
///
private Person person;
public Person Person
{
get
{
return person;
}
set
{
person=value;
}
}
Presentation CodeThe adapter is the logical place to put presentation code. In our example, we want to make the back color of one of our controls match the person’s favorite color. The public System.Drawing.Color FavoriteColor
{
get
{
switch (person.FavoriteColor.Color)
{
case "Blue":
return System.Drawing.Color.Blue;
case "Green":
return System.Drawing.Color.Green;
case "Red":
return System.Drawing.Color.Red;
default:
return System.Drawing.Color.Black;
}
}
}
Design TimeOnce this component is compiled, it can be placed on our toolbar in the standard manner, by right-clicking on the toolbox and selecting ‘Add/Remove Items’. Once placed on the form, the component appears in the web forms design tray:
View (our webform)To databind to our new We are now ready to bind our desired control properties. Note that ASP.NET supports binding a large number of control properties, not just text. The example form has many properties bound, we will take only one as an example. Say, we want a text box on our form with the backcolor set to the favorite color selected by the user. To do this, we select the text box we want to bind and click the ‘Databindings’ builder:
This brings up the databindings dialog box. To bind, we simply click on the control property we want to bind,
Putting it all togetherThere is one final step before we can make this form ‘go’. We must tell the web form to activate databinding. This is done by calling a single method: this.DataBind()
I have added some rudimentary editing capabilities into our private void Page_Load(object sender, System.EventArgs e)
{
//if the page has been posted back then apply updates requested
if (this.IsPostBack)
{
//pass form's Request name value collection to the
//static Update method
Person.Update(Request.Form);
}
//pass our person object to our adapter. The various controls
//are bound to this adapter
this.personAdapter1.Person=StateHelper.Person;
//this one line of code takes care of setting up the
//entire web form including setting controls' text properties
//back color etc etc
this.DataBind();
}
That’s literally all the client-side code required. ConclusionIn my (biased) opinion, this is an elegant solution to many standard business data-centric web based projects that could scale to any degree of complexity. The business logic is in the business logic classes, the presentation logic is in the controllers (adapters), the web form binds to the presentation logic, and the web forms themselves don’t contain much code at all. The object oriented nature of this solution makes its code easy to read, easy to maintain, and quite flexible. Further DevelopmentDepending on interest levels and my time, this approach could be extended: Whidbey The upcoming .NET 2.0 / Visual Studio 2005 will feature Two-way databinding The data binding presented here is one way only. Data is passed from the object to the form. When the user submits the form, we just use the POSTed values to update the object. Two way databinding would pass the updated object back from the client to the server. This may be able to be achieved by the use of viewstate and/or serialization. Add a database Code to persist the object to a database, rather than just in memory, could be integrated. The SQL would be embedded into the business logic itself. The approximately 8 million Data Abstraction Layer methods discussed in CodeProject articles do not fit well within an object oriented architecture, I believe. But that’s another discussion…
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||