Object relational mapping via attributes?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

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

--
 
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
 
"Brett Romero" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
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
 
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" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
| 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
 
"Brett Romero" <[email protected]> a écrit dans le message de (e-mail address removed)...

|> | 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
 
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>
 
Back
Top