Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bubbling Events from Class to Class to GUI 1

Status
Not open for further replies.

mbde

Programmer
Mar 14, 2005
55
US
The goal question is toward the bottom of the post.

Setup
Complex Object:
Class AObject (
property BCollection

)
Class BCollection (
property CCollection

)
Class CCollection()

I have a class that populates the object then that class calls a different class method that saves the object

Class Populate
{
Public void PopulateObject()
{
….
DataProvider.SaveObject()
}
}

Class DataProvider
{
Public void SaveObject
{
Foreach (b in BCollection)
{

// event call one that b was entered in database.
}
}
}

Class InsertForm : Form
{
// Goal is to make this line work in the best practice
Populate.event += OnTheFormHandler
}

Now InsertForm has no visibility to DataProvider what is the best way to define an event in DataProvider so that its EventHander can be used in the Form and its delegate in PopulateObject

Goal:
What I am trying to avoid is declaring two sets of events one for each class like….

Class Populate
{ delegate void PopulateProcess(..)
Public event PopulateProcess Processed
Public void PopulateObject()
{
….
DataProvider.Processed += new DataDelegate(DataProcess)
DataProvider.SaveObject()

}
Public DataProcess ()
{
If (Process != null)
Process(…)
}
}

Class DataProvider
{
delegate void DataDelegate (..)
Public event DataDelegate DataProcess;

Public void SaveObject
{
Foreach (b in BCollection)
{

// event call one that b was entered in database.
OnProcess()
}
}
Public OnProcess()
{
If (DataProcess != null)
DataProcess(this, new EventArg(…)

}
}

Class InsertForm : Form
{
// Goal is to make this line work in the best practice
Populate. Processed += new PopulateProcess (OnTheFormHandler)
}

Thanks in advance
 
What you are talking about is event bubbling - and it can be a pain sometimes.

You may be interested in a good design pattern such as the Observer Pattern. Anything that needs to be updated when data changes can subscribe to a class and be notified on updates.

As soon as you start the long process of event bubbling your workflow can become distorted and hard to follow.
 
An Observer pattern does make sense though I don't know if I fully grasp one aspect.

Since InsertForm has no visablity to DataProvider, I am thinging that I would have to folling the singleton pattern also so there is one instance that both the Form can subscribe to and the DataProvider can notified

In DataProvider I can say ObserverClass.Notify("Process x Successful")
Notify would call all the IObservers with an update.

In the insert form I can call ObserverClass.Register(this)

But in the Insert form I have to inherit from IObserve
so that an Update method can be called, and addressed back to the GUI to upate the status??

Is this how you would implement it or am I missing the point?
 
Your insert method would call the update on the observer class which would in turn call the Update method on each IObserver.

Also: you may want to consider not using the observerclass as a singleton - instead create a controller (model-view-controller pattern) that holds a single instance of the observer class for this purpose. That way, if you need another observer class for something else you can re-use it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top