Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: OOP Design Help

  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OOP Design Help

    Hello,

    I am in the process of designing a custom application for a friend for his business and will be writing it in C#. Now, I can write it in a modular way no problem, but would love to approach it from a completely OOP point of view. This is where I am stuck.

    Basically, the application will track the inventory in the store, as well as customers. It sounds very simple, I know... but for some reason I cannot figure out how to approach it. I would love to hear how this problem would be solved from a complete OOP standpoint (what types of classes and functionality to create).

    Let's take the customers functions for example. How would you approach this from an OOP point of view (being able to add, update, delete, find customers). What classes would you design (I thought a Customer object to hold the fields and data, and CustomersManager object to add, update, delete, find). I could be totally off base here, though.

    A very simple example would be awesome. Thanks so much in advance!

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    You're on the right track. You would create a Customer class that would store all the fields of your Customer table in your database (preferably a relational database like SQL Server over a CSV or XML file). You could create a Customer Manager class that imports the database libraries needed to establish a connection to your database and query the database for your Customer object. You would do the same with your Inventory also.

    I don't know C#, but I do know Java, which is similar in syntax.
    Code:
    // Customer Manager Package that handles the connects,
    // disconnects, and queries between your customer and database.
    import mypackage.db.CustomerManager;
    
    public class Customer
    {
        private String _firstName;
        private String _lastName;
        private String _address;
        private CustomerManager _myCM;
    
        public Customer(String fname, String lname, String address)
        {
            _firstName = fname;
            _lastName = lname;
            _address = address;
            _myCM = new CustomerManager();
            _myCM.connect();
        }
    
        public String getFirstName()
        {
            return _firstName;
        }
    
        public void setName(String fname, String lname)
        {
            _firstName = fname;
            _lastName = lname;
        }
    
        public boolean addCustomer()
        {
            // Insert customer into Customer table using
            // Customer Manager's insertCustomer method
            return _myCM.insertCustomer(this);
        }
    
        public void cleanUp()
        {
             _myCM.disconnect();
             _myCM = null;
        }
    }
    -Shane

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I would actually make the class that wraps around your database connection a singleton. That way you can just use it in the places you need it and your program maintains one connection while the program is open.
    OracleGuy

  • #4
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheShaner View Post
    You're on the right track. You would create a Customer class that would store all the fields of your Customer table in your database (preferably a relational database like SQL Server over a CSV or XML file). You could create a Customer Manager class that imports the database libraries needed to establish a connection to your database and query the database for your Customer object. You would do the same with your Inventory also.

    I don't know C#, but I do know Java, which is similar in syntax.
    Code:
    // Customer Manager Package that handles the connects,
    // disconnects, and queries between your customer and database.
    import mypackage.db.CustomerManager;
    
    public class Customer
    {
        private String _firstName;
        private String _lastName;
        private String _address;
        private CustomerManager _myCM;
    
        public Customer(String fname, String lname, String address)
        {
            _firstName = fname;
            _lastName = lname;
            _address = address;
            _myCM = new CustomerManager();
            _myCM.connect();
        }
    
        public String getFirstName()
        {
            return _firstName;
        }
    
        public void setName(String fname, String lname)
        {
            _firstName = fname;
            _lastName = lname;
        }
    
        public boolean addCustomer()
        {
            // Insert customer into Customer table using
            // Customer Manager's insertCustomer method
            return _myCM.insertCustomer(this);
        }
    
        public void cleanUp()
        {
             _myCM.disconnect();
             _myCM = null;
        }
    }
    -Shane
    Thank you very much. I will see what I can come up with.

  • #5
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by oracleguy View Post
    I would actually make the class that wraps around your database connection a singleton. That way you can just use it in the places you need it and your program maintains one connection while the program is open.
    A much better option! Opening individual connections for each customer and/or inventory object would really cause some problems down the road. Good catch.

    -Shane

  • #6
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey guys,

    Thanks for your responses. I have created a CustomerManager class which has the following public method: addCustomer(Customer). The method accepts a Customer object which I have defined which holds all the customer's information. The CustomerManager class makes calls to a wrapper class I have written which wraps all the DB connectivity.

    So basically, when the user chooses to open the Customers section of the program, a CustomersManager object is created. When the user enters all the information, and clicks the 'Add' button, a Customer object is created, which is passed all the information from the textboxes on the form. Then the addCustomer() method is called (from the CustomerManager object) and passed the newly created Customer object.

    Would this be the correct way to go about it? I am a bit confused, as the Customer object really doesn't seem necessary here since it will contain no methods.

    Thanks for all the help.

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Ok, how about this:

    3 classes:
    • database connection class (singleton) called dbConn
    • customer collection class called customerManager
    • customer class called customer


    Obviously the names aren't important, you can call them something different.

    The customer class should have properties for all the columns in your customers table in your DB. Then it should have at least a save method. The save method then gets the instance of the database connection class and uses it to save the data into the database.

    Your customer manager class can have methods for searching for customers in the database and it would read the results, create customer classes for each one and then add it to the collection. You could also have a saveAll function on this class that would loop through and save all the customers in the collection if desired.

    There are more methods you could add as you need them.

    The using a singleton design pattern for the database connection is the best idea since it maintains a single connection and if your database connection changes, there is only one class that has to be updated.
    OracleGuy

  • #8
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I actually do not use a singleton db layer with my .NET applications. It all depends of course, but if you have applications that spawn multiple threads you can run into issues with a singleton, also in .NET applications I use an as needed connection...with connection pooling I find it a better solution.

    As said above, I like generics for data objects, makes it pretty easy to create collections of data objects (e.g customers).


    So...there's another point of view for you

  • #9
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    Ok, how about this:

    3 classes:
    • database connection class (singleton) called dbConn
    • customer collection class called customerManager
    • customer class called customer


    Obviously the names aren't important, you can call them something different.

    The customer class should have properties for all the columns in your customers table in your DB. Then it should have at least a save method. The save method then gets the instance of the database connection class and uses it to save the data into the database.

    Your customer manager class can have methods for searching for customers in the database and it would read the results, create customer classes for each one and then add it to the collection. You could also have a saveAll function on this class that would loop through and save all the customers in the collection if desired.

    There are more methods you could add as you need them.

    The using a singleton design pattern for the database connection is the best idea since it maintains a single connection and if your database connection changes, there is only one class that has to be updated.
    Would you put your addCustomer() method inside the customerManager class or put the add() method in the customer class? The same for the updateCustomer method.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •