locked
OOP question RRS feed

  • Question

  • Hi folks,

    Apologies if this is not the correct form to post this question but I thought that users who work with databases might be able to help me.

    I come from a procedural programming background with windows forms. The work I mainly do is with relational databases. Typically using customers, salesledger, purchaseledger etc tables. 

    I am trying to get my head around some OOP concepts. The main problem I have is understanding the connection between classes and tables.

    For example, take my customers table. For simplicity, it has customerID, name, address and so on. Suppose I want to write a program / form / class (whatever you want to call it) that manages the details of all the customers. 

    • I have read books that show me how to use a datagrid that is databound to the table. Validation would then be performed using a partial class of a dataset.
    • Other books would tell me to create a customclasser. Validation would be performed inside the customer class. What this book doesn’t tell me is how to get the data into the customer objects.

    With what I have read so far regarding OOP, I feel the second option (create the customer class) is the way to go. I can see the benefits in having all the control / validation built into the customer class for all things that concern customers.

    However, this does not answer my question on how to get the data from the table into my customer objects.

    Should it be something like:

    Customer c = new customer(“ABC123”) 

    Where ABC123 is the customer ID and I use the constructer to access the customer table and fill in the remaining fields there ?

    If anyone could give me any advice it would be appreciated. I really don’t want to start off on the wrong foot with this.

    Regards,

    John


    jppnn

    Monday, December 3, 2012 3:33 PM

Answers

  • Yes, classes are best.  Read about n-tier programming to get the full picture.

    Yes, that type of construction could be done and it would be an improvement over a DataSet/DataTable.  However there is so much more that you can do, but unfortunately cannot be taught in a forum post.  You need to learn about patterns.  In my experience, the best combination for a business layer (n-tier programming concept) is to construct based on a DTO (Data Transfer Object) and passing it to the constructor having in mind IoC (Inversion of Control).  Ideally, you would only define an interface for the DTO so you can easily exchange DTO's, which will make unit-testing a breeze.

    A quick sample:

    //The DTO's interface:
    public interface IEntityData
    {
        object this[string fieldName] { get; set; }
    }
    
    //A super simple DTO that implements the above interface would be a Dictionary<string, object>:
    public class DictionaryData : Dictionary<string, object>, IEntityData
    { }
    
    //now you can write the Customer class as:
    public class Customer
    {
        public int ID { get; protected set; }
        public string Name { get; set; }
        public string Address { get; set; }
    
        public Customer(IEntityData data)
        {
            ID = (int)data["ID"];
            Name = data["Name"].ToString();
            Address = data["Address"].ToString();
        }
        public Customer()
        { }
    }

    As you can see, you can easily read from a database using a connection object, a command object and a data reader object and store the data into a DictionaryData object, then pass that object to the constructor of Customer.  Or you could just create data "on the fly" in code for testing purposes that would not require to query the database.  Much simpler for testing.

    So the idea here is to maximize separation between layers (read about n-tier programming) and also take advantage of well-known and solid design patterns.


    Jose R. MCP
    Code Samples

    • Marked as answer by Chester Hong Thursday, December 13, 2012 6:46 AM
    Monday, December 3, 2012 3:59 PM

All replies

  • Yes, classes are best.  Read about n-tier programming to get the full picture.

    Yes, that type of construction could be done and it would be an improvement over a DataSet/DataTable.  However there is so much more that you can do, but unfortunately cannot be taught in a forum post.  You need to learn about patterns.  In my experience, the best combination for a business layer (n-tier programming concept) is to construct based on a DTO (Data Transfer Object) and passing it to the constructor having in mind IoC (Inversion of Control).  Ideally, you would only define an interface for the DTO so you can easily exchange DTO's, which will make unit-testing a breeze.

    A quick sample:

    //The DTO's interface:
    public interface IEntityData
    {
        object this[string fieldName] { get; set; }
    }
    
    //A super simple DTO that implements the above interface would be a Dictionary<string, object>:
    public class DictionaryData : Dictionary<string, object>, IEntityData
    { }
    
    //now you can write the Customer class as:
    public class Customer
    {
        public int ID { get; protected set; }
        public string Name { get; set; }
        public string Address { get; set; }
    
        public Customer(IEntityData data)
        {
            ID = (int)data["ID"];
            Name = data["Name"].ToString();
            Address = data["Address"].ToString();
        }
        public Customer()
        { }
    }

    As you can see, you can easily read from a database using a connection object, a command object and a data reader object and store the data into a DictionaryData object, then pass that object to the constructor of Customer.  Or you could just create data "on the fly" in code for testing purposes that would not require to query the database.  Much simpler for testing.

    So the idea here is to maximize separation between layers (read about n-tier programming) and also take advantage of well-known and solid design patterns.


    Jose R. MCP
    Code Samples

    • Marked as answer by Chester Hong Thursday, December 13, 2012 6:46 AM
    Monday, December 3, 2012 3:59 PM
  • Thanks Jose for you comprehensive reply.

    Much of what you said is beyond my understanding. I have not come upon the Dictionary class yet. I assume this is some sort of collection ???

    Anyway, I will endeavor with classes in the meantime as I dont want to have regrets down the line.

    Thanks,

    John


    jppnn

    Tuesday, December 4, 2012 1:15 PM