views:

68

answers:

3

I'm a newbie and when messing around with creating database applications I always just created my forms and put all the code and bindings in there. Instead of having arrays and lists that held information I made changes to the database directly.

Now that I have evolved a little bit let's say that I sold widgets to customers and kept the sales information in a database. If I were writing a program to access the database wouldn't I want to create a class of type 'Customer' and 'Widget' to work with those entities?

If I'm mistaken then what is the appropriate approach to programming database applications?

+4  A: 

Yes.

You want to look into n-tier programming.

Basically, you allow you front end (presentation layer) access only to your class library (business layer). Your class library then accesses you database.

This gives you a less tightly coupled solution, and allows for more maintainable code. Also by introducing tiers you allow for changes to your DB without the need to re-write code in your front end, as long as the interface with the Business Layer doesn't need to be changed.

As far as binding is concerned, if you are using Visual Studio win forms (2005 onwards) you should be able to us an bindingSource that you can then use to bind your controls. If you are using ASP.NET then your control should bind to a list of objects without any problems.

For AP.Net the ObjectDataSource might be worth looking into. I haven't used it myself, but there's lots of samples on the web. Try here or here.

littlechris
So then what is the use of binding and other data features in Visual Studio and .NET? If you will be using a "data access" layer to feed the presentation layer, wouldn't you be writing most of the code from scratch in the data access layer?
Pete
No use. Since it was introduced--long before .NET--I've always felt that binding front-end controls directly to the underlying database is Evil. Just my opinion, of course....
RolandTumble
Alright, so I would want to write a class from scratch to interact with the database and another class for customers that would use my database class in order to populate it's fields? This is preferred over throwing some textboxes and labels on a form and binding the controls?
Pete
That would be a good approach Pete. You would then have four tiers. Presentation --> Business --> Data Access -- Database. Doing it this way will give you the benefits listed. The important thing is to keep it loosely coupled, for example your Data Access Layer must be the only layer to communicate with your DB, you cannot just get the Business layer to access it directly... if you do this your Business layer becomes dependant on your DB and any changes there will affect the Business Layer and the Data Access Layer.
littlechris
+2  A: 

Yes.

You want to look closely at Object-Relational Mapping.

Your real-world business entities are modeled by objects which map to relational tables.

S.Lott
Say that carefully. It might be worth explaining that "map" doesn't mean "reflect".
le dorfier
+1  A: 

You do not want your presentation layer to directly depend on your database structure; the problem with that is if your database structure changes at all, your presentation layer must change, and in the long term, that tends to cause problems. Moreover, there are security issues involved with having your presentation layer directly interact with your database.

The rough analogy here is to markets; when you go to the store to buy a loaf of bread, you don't need to know how to grow wheat; all you need to know is that you have money, and they have bread, and that they will exchange a certain amount of bread for a certain amount of money. You don't need to know what time of year to plant the wheat, or how to remove the chaff, or any of that, because the backing layer takes care of that for you. Similarly, the farmer doesn't need to know how to sell bread to a whole bunch of people, or even how to make bread; all he has to do is know how to grow wheat.

Modern design philosophy recommends that you use an intermediate layer to interact between your presentation layer and your database layer; this is where you put your business logic. So, for example, let's say that you're selling widgets on your site. Instead of having your presentation code query the database for widgets and display that, you have a Business Object which handles your widgets. This way, your business object needs to know what your database structure is, but your presentation layer only needs to know how to ask your business object for a list of widgets to display. More importantly, in your business object you can place the rules that are to be invoked when certain things happen. So instead of your presentation layer directly making changes to the database for inventory and orders when an order is made, your business object knows how to make the changes and what tables to modify when your presentation layer requests that a sale occurs.

In this way, you separate the display of your information from the persistence and the logic underlying the website. What is involved is good planning; specifically, you have to figure out what your web site will be doing at any given point, and what that means in terms of what interfaces your business objects will provide. Then you implement your business objects based off of those requirements; those business objects are where you put the knowledge of the database structure and your specific business logic ("when A happens, do B and then C", etc.).

This seems like a lot of extra work at the beginning, but it really is worth it.

McWafflestix