Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Django type database access

Django type database access

Scheduled Pinned Locked Moved C#
databasequestionpythoncsharp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    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

    O J 2 Replies Last reply
    0
    • E eggie5

      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

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      Theres 101 data access layers out there, go do some googling and you'll find plenty including tutorials on how to make your own.

      1 Reply Last reply
      0
      • E eggie5

        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

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups