Django type database access
-
If you haven't seen the Django web app framework, it's pretty cool. All DB access code is generated dynamicly -- all you have to do is define the table structures you want. I.E. (python code)
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)Which Django would evaluate to:
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);To create a new row in the person table, just create a new instance of Person and call save()
p = Person(first_name='Alex', last_name='Egg')
p.save()To get all the rows/fields from the Person table just call the static method all
Persons.objects.all()
It all seems so elegant, so I wanted to try and recreate this using C#. So I created a class called Model which is a DB Model like the person class above. I also created a Manager class which exposes the static method All to read data.public class Model
{
public Model()
{new NotImplementedException(); } public static Manager Objects; public void Save() { } public void Delete() { } } public class Manager { public Manager() { } public void Get() { new NotImplementedException(); } public void All() { new NotImplementedException(); } }
So now I will create my Person object in C#:
public class SentRecord : Model
{
public Person(string firstName, string lastName)
{
this.FirstName= firstName;
this.LastName = lastName;
}public string FirstName; public string LastName; }
I figure this this is pretty much the same idea as the python Person class at the top. Now I create a new instance of my Person model:
Person p=new Person("Alex","Egg");
Now I can save it to the DB by calling:
p.Save();
Now, my question is, how do I go about implementing Model.Save()? I somehow need to get the names of deriving class' public fields. Reflection? So If I just get the table name (I'll hardcode for now) and the People's fields I can generate my SQL insert statement. Any pointers on h
-
If you haven't seen the Django web app framework, it's pretty cool. All DB access code is generated dynamicly -- all you have to do is define the table structures you want. I.E. (python code)
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)Which Django would evaluate to:
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);To create a new row in the person table, just create a new instance of Person and call save()
p = Person(first_name='Alex', last_name='Egg')
p.save()To get all the rows/fields from the Person table just call the static method all
Persons.objects.all()
It all seems so elegant, so I wanted to try and recreate this using C#. So I created a class called Model which is a DB Model like the person class above. I also created a Manager class which exposes the static method All to read data.public class Model
{
public Model()
{new NotImplementedException(); } public static Manager Objects; public void Save() { } public void Delete() { } } public class Manager { public Manager() { } public void Get() { new NotImplementedException(); } public void All() { new NotImplementedException(); } }
So now I will create my Person object in C#:
public class SentRecord : Model
{
public Person(string firstName, string lastName)
{
this.FirstName= firstName;
this.LastName = lastName;
}public string FirstName; public string LastName; }
I figure this this is pretty much the same idea as the python Person class at the top. Now I create a new instance of my Person model:
Person p=new Person("Alex","Egg");
Now I can save it to the DB by calling:
p.Save();
Now, my question is, how do I go about implementing Model.Save()? I somehow need to get the names of deriving class' public fields. Reflection? So If I just get the table name (I'll hardcode for now) and the People's fields I can generate my SQL insert statement. Any pointers on h
-
If you haven't seen the Django web app framework, it's pretty cool. All DB access code is generated dynamicly -- all you have to do is define the table structures you want. I.E. (python code)
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)Which Django would evaluate to:
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);To create a new row in the person table, just create a new instance of Person and call save()
p = Person(first_name='Alex', last_name='Egg')
p.save()To get all the rows/fields from the Person table just call the static method all
Persons.objects.all()
It all seems so elegant, so I wanted to try and recreate this using C#. So I created a class called Model which is a DB Model like the person class above. I also created a Manager class which exposes the static method All to read data.public class Model
{
public Model()
{new NotImplementedException(); } public static Manager Objects; public void Save() { } public void Delete() { } } public class Manager { public Manager() { } public void Get() { new NotImplementedException(); } public void All() { new NotImplementedException(); } }
So now I will create my Person object in C#:
public class SentRecord : Model
{
public Person(string firstName, string lastName)
{
this.FirstName= firstName;
this.LastName = lastName;
}public string FirstName; public string LastName; }
I figure this this is pretty much the same idea as the python Person class at the top. Now I create a new instance of my Person model:
Person p=new Person("Alex","Egg");
Now I can save it to the DB by calling:
p.Save();
Now, my question is, how do I go about implementing Model.Save()? I somehow need to get the names of deriving class' public fields. Reflection? So If I just get the table name (I'll hardcode for now) and the People's fields I can generate my SQL insert statement. Any pointers on h
eggie5 wrote:
I somehow need to get the names of deriving class' public fields. Reflection?
Yep, reflection would be the way to get the data. Custom attributes would come in handy too:
class Person
{
[Column("AgeColumn")]
public int Age;
}Where TableFieldName is a custom attribute. Those may come in handy to get the corresponding table or column for a class or field. As the other poster noted, there are a lot of data access layers out there that do this. In the upcoming .NET 3.5, you'll have built-in support for this in the form of LINQ (language integrated query), and it's extension of LINQ-to-SQL and other databases. See Scott Guthrie's blog post on LINQ to SQL[^] for more info.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Minnesota Bridge Collapses The apostle Paul, modernly speaking: Epistles of Paul Judah Himango