Autocreate Data Access Layer

R

Rbrt

I have a class with properties that correspond to the columns in a database
table. So for example the column BookTitle in table Books will have a
corresponding property BookTitle in the class Books.

I am writing a data access layer that creates a list of Book objects and so
I have to read the data from the table, and then loop through all the records
and set the properties of each instance of the Book class by getting the
corresponding value from the datatable, e.g....

If not dtBooks.Rows(intIdx)("BookTitle").Equals(DBNull.Value) then
BookInstance.BookTitle = datatableBooks.Rows(RowIndex)("BookTitle").value
end if

There are a lot of columns in the table and properties in the class and each
time I add a new column and property, I have to add new code to the data
access layer.

Is there any way to simplify the data access layer so that it can
automatically loop through the records and set the appropriate properties of
the class object just by looking at column names?

I've been trying to figure out how to do this using reflection, but it's
tough slogging.

Thanks!
 
S

sloan

You'd have to resort to reflection, which will significantly reduce
performance.

1. Code them by hand.
2. Use a Code Generator, to do the work that you would normally do by hand.
But this gives you static code. Its a good "start up" code..that you tweak
over time.
3. Use reflection, (which you're proposing).

I would NEVER suggest #3. Its too slow, and a bad architecture decision
from the get-go, IMHO.


If youre set on doing it...then here:
http://www.codeproject.com/useritem...22471&exp=0&select=1954453&df=100#xx1954453xx

That is my follow up post to the guy's article. Scroll to the top to see
his article.

...
 
R

Rbrt

I was wondering about performance. I guess I'll stick to code by hand.

Thanks for your response.
 
R

randy.buchholz

I use PowerDesigner (Option 2) and it's OR Mapping capabilities to generate
the DAL. You can easily create custom generation templates and get pretty
specific. When I need to add new columns to the tables of object I just add
them to my model, compare my new model to the old and the system will
generate new and modified code for the changes.
 
R

Robbe Morris - [MVP] C#

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top