tags:

views:

285

answers:

5

I have a table in my database with 2 rows of employee data.

...........

ID |  Name

...........

1 | Jon Doe

2 | Jane Doe

...........

I need to convert this into:

I NEED THIS IN MY CODE !!!! JUST LIKE THAT AS CODE YOU PEOPLE NOT ORM AS CODE !!!

Employee e1 = new Employee();
e1.ID = 1;
e1.Name = "Jon Doe";

Employee e2 = new Employee();
e2.ID = 2;
e2.Name = "Jane Doe";

SO THAT IT CAN BE SAVED TO DB WHEN APP RUNS FIRST TIME.

TO SAVE MY TIME FROM ACTUALLY WRITING 1200 OBJECT INSTANCES TO THE DATABASE,I JUST NEED SOME WAY TO (LIKE XML FROM DB) !!! ON FIRST RUN

This is an example,why I need it is because,I have a table that stores settings (width,height,label for grid) etc that need to be set to something first time the app runs.

What way could I generate this, are there any tools available,or how would you go about it?

Sorry for not being so f*** clear rough day...

+6  A: 

The normal way to do this outside of an ORM is to write an EmployeeFactory class with a .Create() method that accepts a DataRow as a parameter and returns a new Employee object.

This way you don't have to "pollute" your employee class with the code to do that, and you can maintain separation of concerns in a nice, testable way.

Joel Coehoorn
I guess this would be an option I wanted to do something like powerdesigner would do reverse engineer from db ah well
abmv
i dont understand why you were downvoted
Sam Saffron
I added the text "outside of an ORM" later.
Joel Coehoorn
Still doesn't mean it should be downvoted. I really think people need to learn that there is more than their way that is correct. (and this is an example of an ORM... just one that was manually coded)
Matthew Whited
A: 

You can use something like Entity Framework, Linq2SQL, SubSonic, nHibernate, etc.. to generate objects based on your database schema.

Cody C
My preference is NHibernate as its open source and there are loads of examples of it on the net. Also ActiveRecord is a good one as it wraps NHibernate to make it easier!
Penfold
not object object instance to persist as code
abmv
+2  A: 

Edited I appear to have misread your question. It looked like what you want is an orm mapper but really what you are looking for is something like MyGeneration which you can point at a database and have it build you the objects you need. I leave the remainder of the original answer as an ORM is still useful in these situations.

There are several available for .Net and if you are developing on .Net 3 or higher, than you have one built in call Linq. Getting started with Linq is beyond the scope of this answer so here is a great blog post from Scott Guthrie which walks you through it. Another Microsoft offering is available called EntityFramwork and more information about that can be found here

NHibernate is a free .Net port of the Hibernate framework which also gets the job done.

ORM mappers make converting database data into classes very easy, but they come with trade offs. See the wikipedia entry for more info.

Rob Allen
NHibernate is also a pain in the butt for small projects
Matthew Whited
Are there any MyGeneration templates to do this?
abmv
MyGeneration ships (downloads) with templates for EasyObjects and dOOdads, the ORM built by the same company.
Rob Allen
A: 

There are many ORM tools (as Cody C suggested) that will generate code and do the mapping for you. But from the sound of the question, I am thinking that would be overkill for your application. If this is all you need, run your query with your perfered data access (dynamic sql, stored proc, views, etc) and then simply map the values in the returning data to your object.

This is certainly not the most elegant solution, but introducing a 3rd party tool may be more than you need.

Mike Ohlsen
+1  A: 

Why don't you select the data from db in this format and save it to a file?

For mysql I would do (and did):

Save this query as c:\in.sql

SELECT CONCAT('Employee e', id, ' = new Employee();', char(13),
       'e', id, '.ID = ', id, ';', char(13),
       'e', id, '.Name = "', name, '";',
        char(13), char(13)) as '//generated' from employee;

and run from commandline:

mysql -u root < c:\in.sql > c:\out.cs

This will execute the query from in.sql and put the output to out.cs in this format:

//generated
Employee e1 = new Employee();
e2.ID = 1;
e2.Name = "...";


Employee e2 = new Employee();
e2.ID = 2;
e2.Name = "...";
SchlaWiener