Help on delagate strange behaviour ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y2FsZGVyYXJh?=

    Help on delagate strange behaviour ???

    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.Platefor m.DB"

    // decalaration
    public event DBEventHandler RaiseDBInsertEv ent;

    public Boolean Insert()
    {

    DBEventArgs m_dbArgs = new DBEventArgs(Occ uredAt,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)
    {
    RaiseDBInsertEv ent(OccuredAt,D BAction.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.Platefor m.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.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);

    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.Inser t

    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


  • Doker

    #2
    Re: Help on delagate strange behaviour ???

    For that in my tets form I set the rgistration as follow
    >
    Nomos.Plateform .DB.Action action=new Nomos.Plateform .DB.Action() ;
    action.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);
    >
    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.Inser t
    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.

    Comment

    • =?Utf-8?B?Y2FsZGVyYXJh?=

      #3
      Re: Help on delagate strange behaviour ???

      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.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);

      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

      "Doker" wrote:
      For that in my tets form I set the rgistration as follow

      Nomos.Plateform .DB.Action action=new Nomos.Plateform .DB.Action() ;
      action.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);

      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.Inser t
      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.
      >

      Comment

      • =?Utf-8?B?Y2FsZGVyYXJh?=

        #4
        Re: Help on delagate strange behaviour ???

        Hmm I think tehre is maybe a confusion ...

        the following line is not part of the code

        "AssmParent ->AssmDB.Inser t"

        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





        "Doker" wrote:
        For that in my tets form I set the rgistration as follow

        Nomos.Plateform .DB.Action action=new Nomos.Plateform .DB.Action() ;
        action.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);

        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.Inser t
        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.
        >

        Comment

        • =?Utf-8?B?Y2FsZGVyYXJh?=

          #5
          Re: Help on delagate strange behaviour ???

          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 ?



          "Doker" wrote:
          For that in my tets form I set the rgistration as follow

          Nomos.Plateform .DB.Action action=new Nomos.Plateform .DB.Action() ;
          action.RaiseDBI nsertEvent+= new InsertDelegate( InsertOccuredMe thod);

          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.Inser t
          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.
          >

          Comment

          • Doker

            #6
            Re: Help on delagate strange behaviour ???

            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.

            Comment

            Working...