views:

138

answers:

3

So I am refactoring a little application as an example to get more practice. The purpose of the application (let's say) is to collect the data from a "sign up new user" form, save it in the database. The only limitation I have is I have to use a special custom Data Access class which communicates directly with the database and returns the data (if applicable) in a DataTable object.

I have a question regarding a little details on a form and how do they fit in into the layer architecture. For example, my form has a drop down list that's fed from the database, but at the same time drop down list doesn't represent an object per SE (unlike a User that is a object, there is a class User that has multiple methods, data members etc). I don't want to have calls to the stored procedure right there in the code behind but I also do not wish to overdo on abstraction.

What would be an elegant way to take care of these little details w/o creating a class abstraction galore.

Hope I am being clear

+1  A: 

Funny you should ask that. I went through that issue here.

These other Stack Overflow Questions that I've answered that show other parts (tangentially related):

Getting ListView Data Items from Objects
Working with ListViews
Concatenating Properties in a DropDownList

George Stocker
A: 

I don't know what's best practice, but what I do is I have a utility class that has a method that takes as arguments a DropDownList object and an enum, so I do

FillDropDown( ddlistPhoneType, DropDownTypes.PhoneTypes );

The utility class fills the dropdowns sometimes from the database, other times from XML, and occasionally some hardcoded values. But at least the GUI doesn't have to worry about that.

MikeW
what are you passing enum argument for? Are you doing the database interaction inside the FillDropDown() function? I am using stored procs which makes it a bit more hassle as i have to pass the parameters, param types etc etc into every function that deals with db, so I am looking for a way to make it less hassle.
gnomixa
Most of our drop down values are in a single XML file, so the enum maps to a section of that XML file. The enum is just to make it easier for the GUI developers. The XML file has somethign like: <data type="PhoneTypes" value="Fax"> <data type="PhoneTypes" value="Cell">Dropdowns tend to have simple value/text pairs of data, so you could use a single stored proc with an argument to tell it what source to use for the data.
MikeW
+1  A: 

An option for getting non-object data to the UI is to create one or more lookup classes that are a bucket or "service" for getting odd bits of data for things like drop down lists etc...

Example:

myDDL.DataSource = Lookup.GetAllCountries(); // GetAllCountries is a static method
// set name/value fields etc...
myDDL.DataBind();

Using this methodology, you can still support tier separation. It's not object oriented or elegant, but it is very practical.

Daniel Auger
Doesn't it break the separation if you are doing the DataBind() inside the UI layer? I may be over thinking it, but I might as well get it right if I am re-writing it.
gnomixa
I'm not sure I'm following you - the logic for setting up the DDL and performing the databind belongs in UI layer (the form code/codebehind).
Daniel Auger
Let me explain a little more - the Lookup class is a business layer or infrastructure layer class which would call the data access layer just as any other business class would. This means that the UI layer is responsible for calling this business layer class and binding its result (a dataset in this case) to the the DDL. This binding hookup is traditionally considered an appropriate responsibility for the UI layer.
Daniel Auger
gotcha, thanks Daniel.
gnomixa