OOP Data Access Question

I am trying to grasp and understand OOP better and I have a question in regards to d classes that perfrom CRUD operations. I am looking for input and thoughts on seperating CRUD functions from domain classes.

My application has three domain classes: item, cart(which contain items) and user(manage the carts, add remove items and such). Items and carts both expose public methods that require CRUD operations and I do not want to build the CRUD into the class so I have added a data class for handling the CRUD operations.

My qeustion is should each item(could be 1000s) and cart(100s) object create a new instance of the data class for calling the methods for performing needed CRUD functions This just seems to be alot of overhead by creating a new instance of the data class for each, especially if I am creating a SQLConnection in each. And I do need to keep the objects seperate from the CRUD functions for re-use.

Should the class being used for CRUD use static SQLConnections (if that works)
Should the methods of the data class be static

If I need to provide more information please let me know. I am thankful for any insight that can be provided.



Answer this question

OOP Data Access Question

  • Vaassu

    hi,

    i'm not expert in this, but what you are talking about is building a framework for application, i have a book about that , but still trying to understand it "Expert C# 2005 Business Objects"

    all what i can say here is that you may use the factory pattern

    hope this helps


  • redshock

    you need to implement more than one pattern not just Factory Pattern for example main one is what called DAO pattern others patterns like Business object,Factory,etc

    look at this Thread it is related somewhere to your question

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=559313&SiteID=1

    also look at DAO pattern at http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

    the code in java but i think it is easy to translate it

    and look also at Application Architecture for .NET: Designing Applications and Services

    another thing about your question about re create connection you can take benefits
    of connection pooling that allow you reuse active database connection look at this article for more info

    http://www.15seconds.com/issue/040830.htm

    another thing that can help you is to take alook at Northwind StarterKit which the creator of it introduce it as "The Northwind Starter Kit is a sample application intended to be used as a blueprint when designing and implementing a .NET based layered application architecture"

    you can download it from http://download.manageddesigns.it/nsk.aspx

    i hope this help



  • reichard

    First thanks for the reply. I actually just bought that book this week and it is sitting at work. I did some quick reading on factory pattern and that makes sense. Plus, I think one on of my issues is that I was thinkg in terms of each object having a instance of the data class at all times. And after sleeping on it I realize that I will only create the object when it is actually needed.

    Chris


  • mahima

    Hello,

    The design patterns mentioned in the above post should only be used if really needed. I don't particularly like complicate things unneccsarily.

    From your query, my simple suggestion would be to use a static methods in DAL. Why Because DAL is almost a "dumb" layer, kind of a helper class to persist business objects.

    To make your code more clean and efficient you can use the DAAB (Data Access Block).

    Regards,

    Vivek


  • saleyoun

    Keep your business model independent from any persistence or database tasks! So don't implement any Save or Load/Retrieve methods within your business model but delegate those tasks to a persistence layer. That's the way to keep your business classes reusable.

    Check out for a persistence layer that doesn't force you to derive from "persistence classes" or to implement a "persistence interface" in your business classes.

    Hans-Peter

  • OOP Data Access Question