Data Binding C Sharp

Data Binding C#


Data binding is the time-honored tradition of pulling information out of an object and displaying it in
your application’s user interface, without writing the tedious code that does all the work. Often, rich
clients use two-way data binding, which adds the ability to push information from the user interface
back into some object—again, with little or no code. Because many Windows applications are all about
data (and all of them need to deal with data some of the time), data binding is a top concern in a user
interface technology like WPF.
Developers who are approaching WPF from a Windows Forms background will find that WPF data
binding has many similarities. As in Windows Forms, WPF data binding allows you to create bindings
that take information from just about any property of any object and stuff it into just about any property
of any element. WPF also includes a set of list controls that can handle entire collections of information
and allow you to navigate through them. However, there are significant changes in the way that data
binding is implemented behind the scenes, some impressive new functionality, and a fair bit of tweaking
and fine-tuning. Many of the same concepts apply, but the same code won’t.
In this chapter, you’ll learn how to use WPF data binding. You’ll create declarative bindings that
extract the information you need and display it in various types of elements. You’ll also learn how to
plug this system into a back-end database.


Binding to a Database with Custom Objects

When developers hear the term data binding, they often think of one specific application—pulling
information out of a database and showing it onscreen with little or no code.
As you saw in Chapter 8, data binding in WPF is a much more general tool. Even if your application
never comes into contact with a database, it’s still likely to use data binding to automate the way
elements interact or translate an object model into a suitable display.
However, you can learn a lot about the details of object binding by considering a traditional
example that queries and updates a table in a database. In this chapter, you’ll use an example that
retrieves a catalog of products. The first step in building this example is to create a custom data access component.

Building a Data Access Component

In professional applications, database code is not embedded in the code-behind class for a window but encapsulated in a dedicated class. For even better componentization, these data access classes can be pulled out of your application altogether and compiled in a separate DLL component. This is
particularly true when writing code that accesses a database (because this code tends to be extremely
performance-sensitive), but it’s a good design no matter where your data lives.


Designing Data Access Components

No matter how you plan to use data binding (or even if you don’t), your data access code should always be
coded in a separate class. This approach is the only way you have the slightest chance to make sure you
can efficiently maintain, optimize, troubleshoot, and (optionally) reuse your data access code.
When creating a data class, you should follow a few basic guidelines in this section:
Open and close connections quickly. Open the database connection in every method call, and close
it before the method ends. This way, a connection can’t be inadvertently left open. One way to
ensure the connection is closed at the appropriate time is with a using block.
Implement error handling. Use error handling to make sure that connections are closed even if an
exception occurs.
Follow stateless design practices. Accept all the information needed for a method in its parameters,
and return all the retrieved data through the return value. This avoids complications in a number of
scenarios (for example, if you need to create a multithreaded application or host your database
component on a server).
Store the connection string in one place. Ideally, this is the configuration file for your application.