By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Object relational mapping via attributes?

P: n/a
I'd like to create a class with public properties and functions that I
can map into a datatable with corresponding fields. For example:

ClassA
string Name {get;set;}
int Age{get;set;}
boolean IsEmployed()

MyDataTable
Name
Age
IsEmployed

Can attributes be used above each class property and function such as:
[Name="Age" DataType="System.Int32"]

and how would I then inspect that information to create the DataTable?

Thanks,
Brett

Aug 30 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Brett Romero escribió:
I'd like to create a class with public properties and functions that I
can map into a datatable with corresponding fields. For example:

ClassA
string Name {get;set;}
int Age{get;set;}
boolean IsEmployed()

MyDataTable
Name
Age
IsEmployed

Can attributes be used above each class property and function such as:
[Name="Age" DataType="System.Int32"]

and how would I then inspect that information to create the DataTable?
I think you are reinventing the wheel. There is already an
object-relational framework which does this:

http://www.castleproject.org/index.php/ActiveRecord

Regards.

--

Aug 30 '06 #2

P: n/a
Actually, there is an even more .NET - friendly implementation of
ActiveRecord on the CodePlex site, written for ASP.NET 2.0, and very nicely
done, I might add. In fact, I had started writing a SQLite DataProvider for
it.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brett Romero" wrote:
I'd like to create a class with public properties and functions that I
can map into a datatable with corresponding fields. For example:

ClassA
string Name {get;set;}
int Age{get;set;}
boolean IsEmployed()

MyDataTable
Name
Age
IsEmployed

Can attributes be used above each class property and function such as:
[Name="Age" DataType="System.Int32"]

and how would I then inspect that information to create the DataTable?

Thanks,
Brett

Aug 30 '06 #3

P: n/a
"Brett Romero" <ac*****@cygen.coma écrit dans le message de news:
11**********************@i3g2000cwc.googlegroups.c om...

| I'd like to create a class with public properties and functions that I
| can map into a datatable with corresponding fields. For example:
|
| ClassA
| string Name {get;set;}
| int Age{get;set;}
| boolean IsEmployed()
|
| MyDataTable
| Name
| Age
| IsEmployed
|
| Can attributes be used above each class property and function such as:
| [Name="Age" DataType="System.Int32"]
|
| and how would I then inspect that information to create the DataTable?

IMO, this is a very poor way of doing things as it "pollutes" the business
class space with database information. Using reflection, it is so simple to
iterate through the properties of any class, from the persistence layer, and
create your mappings there without altering the definition of your business
class.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 30 '06 #4

P: n/a
>Using reflection, it is so simple to
iterate through the properties of any class, from the persistence layer, and
create your mappings there without altering the definition of your business
class.
Yes, reflection is easy but it is super slow.

Brett

Aug 31 '06 #5

P: n/a
Hi Brett,

I would recommend you to use the ObjectMapper .NET.

In opposite to NHibernate you can tag your properties with attributes
in order to do the object mapping. It's a very fast implementation, so
you don't have to fear about performance problems.

Furthermore you can create DDL files out of your business entities. I
think that's the thing you're looking for.

So have a look at http://www.objectmapper.net
or http://blog.objectmapper.net

Cheers
Gerhard

Brett Romero schrieb:
I'd like to create a class with public properties and functions that I
can map into a datatable with corresponding fields. For example:

ClassA
string Name {get;set;}
int Age{get;set;}
boolean IsEmployed()

MyDataTable
Name
Age
IsEmployed

Can attributes be used above each class property and function such as:
[Name="Age" DataType="System.Int32"]

and how would I then inspect that information to create the DataTable?

Thanks,
Brett
Aug 31 '06 #6

P: n/a
"Brett Romero" <ac*****@cygen.coma écrit dans le message de news:
11**********************@m79g2000cwm.googlegroups. com...

| Yes, reflection is easy but it is super slow.

Not if you do the reflection, to create the mappings, once on first use of
any class and then cache the results.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 31 '06 #7

P: n/a
| Yes, reflection is easy but it is super slow.
>
Not if you do the reflection, to create the mappings, once on first use of
any class and then cache the results.
How do you mean to cache those?

BTW, what is DDL?

Thanks,
Brett

Aug 31 '06 #8

P: n/a
"Brett Romero" <ac*****@cygen.coma écrit dans le message de news:
11*********************@i42g2000cwa.googlegroups.c om...

|| Yes, reflection is easy but it is super slow.
| >
| Not if you do the reflection, to create the mappings, once on first use
of
| any class and then cache the results.
|
| How do you mean to cache those?

You only need to read the PropertyInfo for a given type once to determine
what properties are available; so the first time that you start the program,
build a list of mappings from the business class types to their tables and
stream that list to something like a database or XML file. then, subsquent
executions of your program only need to recreate the mappings from the
stored stream, instead of using reflection all over again.

| BTW, what is DDL?

DDL = Data Definition Language

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 31 '06 #9

P: n/a
<ge*************@googlemail.comwrote:
I would recommend you to use the ObjectMapper .NET.
Just thought it would be worth pointing out for the record that this
isn't exactly an unbiased recommendation - the author of the post is
the author of ObjectMapper .NET, according to another post.

<snip>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.