Link to home
Start Free TrialLog in
Avatar of crypto_monkey
crypto_monkey

asked on

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

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()
        {
            InitializeComponent();
        }

        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! :-)
ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crypto_monkey
crypto_monkey

ASKER

anyoneis:

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.
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":

https://www.microsoftelearning.com/eLearning/offerDetail.aspx?offerPriceId=78263&lid=Course+2924%3a+Building+Data+Components+in+Microsoft%u00ae+Visual+Studio%u00ae+2005+(2)_Course

Caio,
David
anyoneis:

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.