Hi guys, Looking for some advice regarding an architectural problem. Seems a bit of a common one but I cant really decide the best way to do whilst keeping within the OOP idea. Perhaps Im missing something obvious, or perhaps Im looking at it from the wrong way.
Basically Ive got a windows application that uses a database. Ive created a Database class that has a Connect(db as string) and Disconnect() method. A global database connection instance is used in the application. So far so good.
Then Ive got loads of classes which represent the data in the database. For example, the database holds information on Files and their Properties. So Ive got a File class, with a collection of property classes.
The file class has methods like ChangeFileName, which changes the name in the database. Adding a new property to a property collection adds a new property to the database, etc.
My problem is all these tasks in the File and Property classes obviously depend a database connection, and Im not sure the best way to relate them.
I could, for example, pass the database connection everytime I create a new File or Property instance, but these seems a bit rubbish.
What would be best is if I could just create the database object, then do things like:
DataConn.Files.Add("filename")
DataConn.Files(0).ChangeFileName("newfilename")
DataConn.Files(0).Properties.Add("newprop")
etc. However I dont know how to design the File class that can reference the parent classes data connection. Similarly the Property class will need to reference the File ID of its parent (the File).
Hope that makes some kind of sense. Any advice welcome
Basically Ive got a windows application that uses a database. Ive created a Database class that has a Connect(db as string) and Disconnect() method. A global database connection instance is used in the application. So far so good.
Then Ive got loads of classes which represent the data in the database. For example, the database holds information on Files and their Properties. So Ive got a File class, with a collection of property classes.
The file class has methods like ChangeFileName, which changes the name in the database. Adding a new property to a property collection adds a new property to the database, etc.
My problem is all these tasks in the File and Property classes obviously depend a database connection, and Im not sure the best way to relate them.
I could, for example, pass the database connection everytime I create a new File or Property instance, but these seems a bit rubbish.
What would be best is if I could just create the database object, then do things like:
DataConn.Files.Add("filename")
DataConn.Files(0).ChangeFileName("newfilename")
DataConn.Files(0).Properties.Add("newprop")
etc. However I dont know how to design the File class that can reference the parent classes data connection. Similarly the Property class will need to reference the File ID of its parent (the File).
Hope that makes some kind of sense. Any advice welcome