[Last Call] Learn how to a build a cloud-first strategyRegister Now


C# Class Design: ADO.NET, separating business logic from presentation, etc.

Posted on 2006-05-13
Medium Priority
Last Modified: 2010-08-05
Hi Experts,

I am a C# novice trying to develop a simple DB application for use in my office, and I could use some expert advice with class deisgn. I am using VisualStudio 2005 and thus C# 2.0, and I will be using SQL Server as my DB.

I am trying to design the classes for this application, but I am having difficulty since this is my first try at a "no-spaghetti-code" design.

This app is to help me track Patients, Doctors, and any lab Tests that Doctors prescribe for their respective Patients.

What I'm concerned about is the interaction of presentation with business logic and data, and their (necessary ?) separation:

1: My DB has tables: 'Patients', 'Doctors', and 'Tests'

2: I have a WinForm named "Form1" (which VS 2005 creates as a class), and this WinForm has some textBoxes that will be bound to fields on the 'Patients' table, which will happen when "Form1" is loaded. In the past, I've just put the business logic code straight onto the "Form1" class...the Form would load and the textBoxes would be populated, and then I could navigate using other controls, etc. (see example below)

- - - - - - - - - - - - - - - - - - - - - -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataApp
    public partial class Form1 : Form
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            // establish connection to DB, 'Patients' table

            // retrieve data from record in 'Patients' table

            // bind data from 'Patients' fields to textBoxes on this "Form1"

- - - - - - - - - - - - - - - - - - - - - -

From reading about current programming models, it seems like the developer is being asked to design an additional class to represent a "Patient" record from the table (or to represent the 'Patients' table itself ??). In other words, it sounds like all my SqlConnection, DataSet, Get/Set methods, and all the code needed to access the records/ etc. need to be in another class?? Does this help clarify what I mean? In the past, I would have effectively made the WinForm "Form1" class my "Patient" class, but it seems like "n-tier" would mean separation of the WinForm class from the "Patient" class. If this is the case, how would I get the "Form1" class to interact with the "Patient" class to still have data-bound controls on "Form1" ?

I also am unsure about how to design classes for a DB. Do I need to create a class for every table in the DB, with attributes assigned for each field in a particular table?

Hopefully what I explained is not sounding like complete nonsense!

Thanks! :-)
Question by:crypto_monkey
  • 2
  • 2
LVL 11

Accepted Solution

anyoneis earned 2000 total points
ID: 16675590
(For a full answer read the first two chapters to "Data Binding with Windows Forms 2.0" by Brian Noyes.)

His advice would be to build a data layer dll which will give you a strongly typed, extensible, dataset, datatables, and tableadapters. You use the TableAdapters to control the interaction between the database and the dataset. You using Bindingsource objects to control the interaction between the UI and the dataset.

There is surprisinglyu little code involved, and it is one hell of a lot easier to do with VS2005 than it was with VS2003.


Author Comment

ID: 16687659

Thanks for the info about the book and about the TableAdapters, etc. That was helpful to get me pointed in the right direction.

I went looking on the web and didn't have any luck locating tutorials for such a process. Until I can free up some extra $$ to check out and possibly buy the book you mentioned, I have to make use of what info is available for free. Do you happen to know of any links to such material? Thanks.
LVL 11

Expert Comment

ID: 16692630
You could try the free virtual labs here: http://msdn.microsoft.com/virtuallabs/vstudio2005/default.aspx

Or, start here at the free training module "Course 2924: Building Data Components in Microsoft® Visual Studio® 2005":



Author Comment

ID: 16694120

I was able to take a lookat the Book you recommended, and it is indeed very informative. The second chapter tells me alot about the same type of scenario I had questions about. Thanks for the input, and thanks for the additional links, too.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question