Architecture advice: dependant classes

Procaine

Active member
Joined
Feb 3, 2004
Messages
37
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 :-)
 
Any ideas with this?
If I havent made it clear what Im trying to do, let me know and Ill re-write. I thought this would be a problem the many class designers come across - having a child class requiring to reference its parent class (note: not as in inheritance, but as in one class has an instance in another).
 
Try this...

This is your file class, with the property FileName.

class File{

private string fileName;
private DataConnection data;
public File(DataConnection instance){
this.data = instance;
}

public string FileName{
get{return this.fileName; }
set{ this.fileName = value; }
}

public void ChangeFileName(string name){
string sql = "UPDATE File SET Filename="+name+";";
this.data.Update(sql);
}

}

Now all you have to do is pass the data object reference into the file object when instantiated, and call the ChangeFileName method whenever you need to make the change.
 
Hi Rodenberg,
Thats what I meant by I could, for example, pass the database connection everytime I create a new File or Property instance, but these seems a bit rubbish.. I guess if thats my only option Ill do this, but it seems a bit of a shame. I use the example of property and file, but Ill be having loads of different classes to represents the different data. Having to have have an instance of the data connection for every class seems a bit of an overload.

Just wondering if there was a better way of doing it.
 
What Ive done in the past is on the Initialisation of the Files class, pass the Database object as a parameter which the class stores a reference to. This way the Files class has its own reference to the Database object that it can use any time it is needed.
 
Yea I do it that way on occasion as well, it depends on the situation.

Either way, you are using a reference to a single object instance so its a matter of preference more than anything.

Another option would be to create an abstract base class containing the connection instance, and have each other class inherit from it. Hope this example helps:

abstract DatabaseClass{

private string dbConnString = <get string from file/reg/etc.>
private SqlConnection cnn = new SqlConnection(dbConnString);
.............

public void Update(string sql){
.......
}
public void Insert(string sql){
......
}

}

class File:DatabaseClass{
public File(){}

public void DoSomething(){
string sql = <arbitrary sql string>;
this.Update(sql);
}

}
 
Could you explain the difference between what the two ideas are? They both seem to be the same to me - the File class holds a reference to the database connection. This is the method I didnt really want to do, mentioned in the initial post.
 
When you inherit from a base-class as Rodenberg showed, youre File-Class actually contains all the methods of the Base-class. As opposed to having your database-class and passing a reference of that class instance into the instance of file-class.

Lets see if I can explain it as a win-form example:
When you drop a control onto a form, youve got properties available to that control, and you can handle events for that control, even though you never defined these properties and events.

This is what happens if you use a base class - you inherit a new class from this base-class and you have all the public/protected elements available to you in this new class at no-cost.

No, if you pass a reference to your database class, theres a physical instance of that database-class floating around, whereas with inheritance, theres just the new-classes that you work with.
 
Back
Top