Help on delagate strange behaviour ???

G

Guest

Dear all,

I have a strange behaviour on the use of delagate. I will try to explain.
I have an assembly (Lets call it AssmDB) which contains database function
operation like Insert, Delete, Update... Wen one of this operation occurs, I
raise an event to any client subscribers.

Lets take as an example the Insert function as follow:

Step1:
====
AssmDB is inside namespace "Nomos.Plateform.DB"

// decalaration
public event DBEventHandler RaiseDBInsertEvent;

public Boolean Insert()
{

DBEventArgs m_dbArgs = new DBEventArgs(OccuredAt,DBAction.Insert);

try
{
... process insert operation here

//check if there is any subscriber to the event
// if no subscribers, no need to raise the events
if (DBInsert != null)
{
RaiseDBInsertEvent(OccuredAt,DBAction.Insert);
}
return true;
}

Then I compile this assembly to get it as AssmDb.dll


Step 2:
=====
Then I have a parent assembly (Lets call it AssmParent) which has reference
to the AssmDB.dll, and call the insert method as follow:

AssmParent is inside namespace "Client.DB"

using (Nomos.Plateform.DB action = new Nomos.Plateform.DB.Action())
{
return action.Insert();
}

Then I compile the assembly and get the AssmParent.dll

At this stage I have a AssmParent assembly which instantiate an Action
object from AssmDb.dll and call the insert method.

Then based on that the Insert method of assembly AssmDb.dll raide the event
DBInsert to any subscribers.

Step3:
=====

To test that I have created a client form application with reference with
both assenblies.

Then I create a button in which I create the registration to events that
might occurs during the Insert as describe above.
What I would like to get is that subscribers should answer to the DBInsert
event of AssmDB.

For that in my tets form I set the rgistration as follow

Nomos.Plateform.DB.Action action=new Nomos.Plateform.DB.Action() ;
action.RaiseDBInsertEvent+= new InsertDelegate(InsertOccuredMethod);

based on the code above the registration is done by creating an instance of
the class which contains the event from AssmDb assembly.

So far so good, and here is what happen.

As the event registration is done through an object belonging to the
Nomos.Plateform.DB namespace, event is not catch from the path:

AssmParent ->AssmDB.Insert

But only catch from the path AssmDB.Insert !!!

Any idea why it behaves like this ?
Is is due to different name space that I use for both class ?

Thnaks for your reply
serge
 
D

Doker

For that in my tets form I set the rgistration as follow
Nomos.Plateform.DB.Action action=new Nomos.Plateform.DB.Action() ;
action.RaiseDBInsertEvent+= new InsertDelegate(InsertOccuredMethod);

based on the code above the registration is done by creating an instance of
the class which contains the event from AssmDb assembly.

So far so good, and here is what happen.

As the event registration is done through an object belonging to the
Nomos.Plateform.DB namespace, event is not catch from the path:

AssmParent ->AssmDB.Insert
What I see is that you, in your test application, create one object of
"child" type for which event you then subscribe.
After that you create another object, this time of "parent" type. This
object instatnialize his own object of "child" type.
That is why your event has nothing to do with the ovject you think
you're testing.

Solution to your problem lies in architecture of your application.
You can use event bubbling which is creating an event in parent and then
creating a protected method that subscribes to inner child object's
event and raises parents event.
Your second option is to create a public readonly property returning
inner child object, similar way the TcpClient exposes its Client property.
 
G

Guest

What do you mean by the following :

"After that you create another object, this time of "parent" type. This
object instatnialize his own object of "child" type.
That is why your event has nothing to do with the ovject you think
you're testing."

The only thing I do from the client test application is registering to the
event as :

Nomos.Plateform.DB.Action action=new Nomos.Plateform.DB.Action() ;
action.RaiseDBInsertEvent+= new InsertDelegate(InsertOccuredMethod);

As the event is generated within the Nomos.Plateform.DB.Action class, I can
register it like above no ?

I could not see clearly why I have not registering from the proper object ?
In reallity its true because it fails...

Could you explain ?
Sorry to bother you but sometimes an easy think is hard to see :)

regards
serge
 
G

Guest

Hmm I think tehre is maybe a confusion ...

the following line is not part of the code

"AssmParent ->AssmDB.Insert"

I just wnat to explain here the path which is going through

What I try to explain is that if the function is called from the follwoing
path

AssmParent is calling an instance of AssmDB.Insert
===> event is not catch

But if Insert function is called from the path AssmDB.Insert
====> event is catch

So what I try to understande is that does when a parent call a child which
raise an events, the only way to catch the events from a client application
is to Pass the parent object as parameter to the child instance, or
escaladate events ?

regards
serge
 
G

Guest

Hmmm thnaks ..that was what I wass expecting...
So in otherwords I need to escaladate the event all the way through the full
path Parent, client and test application right ?
 
D

Doker

calderara pisze:
Hmmm thnaks ..that was what I wass expecting...
So in otherwords I need to escaladate the event all the way through the full
path Parent, client and test application right ?
It was hard to tell what were you doing because .Action (it is a class,
right?) was not mentioned earlier so I assumed it the child class.
Term child is confusing because you must remember that the fack you call
it "child" has really nothing to do with the fact that is is declared
somewere in parent class.
So you subscribe to an event of child - action and it works.
You create a parent object that it in itself creates another object of
class "child" that has nothing to do with the first object of "child"
class and so it has no method subscribed to ITS events.
 
G

Guest

Yes so the only thing I want to do from my client form application is to
register to the so called Client class which raise the events but this is not
working unless the Insert method is call straight away from the assemly
containing the event...

And actually I cannot call directly the Insert method becasue I am using an
other assembly ( The "parent") whcih is the only interface that my final user
can use in order to perform that Insert function.

So my final user will only call the Insert method from the "parent" which
will call the Insert method of the class ( hidden for my final user)...
And that is the point, so if I am doing in that way, event is not cath..

Does it means in such situastion that I need to escaladate the events until
the proper final layer ?

for example parent raise an even when Insert is called, whcih is catch from
the "client" ?

serge
 
D

Doker

calderara pisze:
Yes so the only thing I want to do from my client form application is to
register to the so called Client class which raise the events but this is not
working unless the Insert method is call straight away from the assemly
containing the event...

And actually I cannot call directly the Insert method becasue I am using an
other assembly ( The "parent") whcih is the only interface that my final user
can use in order to perform that Insert function.

So my final user will only call the Insert method from the "parent" which
will call the Insert method of the class ( hidden for my final user)...
And that is the point, so if I am doing in that way, event is not cath..
Because Child object definded in Parent one is not the same one defined
in Client app.
Events are local to each instance.


Does it means in such situastion that I need to escaladate the events until
the proper final layer ?
You have to expose this child from parent and do
parentObjectName.childObjectName.EventName += myMethod;
or yes escalade as you call it.
In some situation you can create a parent class by deriving (inheriting)
from the child class. Events are then inherited too.
 
G

Guest

Thnaks,
I will give a try

serge

Doker said:
calderara pisze:
Because Child object definded in Parent one is not the same one defined
in Client app.
Events are local to each instance.



You have to expose this child from parent and do
parentObjectName.childObjectName.EventName += myMethod;
or yes escalade as you call it.
In some situation you can create a parent class by deriving (inheriting)
from the child class. Events are then inherited too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top