raising an event . .. and getting a callback? to the originating object

S

sloan

I"m trying to figure out what concept I'm missing here, or if its not a good
idea .. or what.

Here is my example.. code is below.

I have an employee class.
It has an event that can be raised.
In the constructor, I check the to see if the employees birthday is today.
If it is today, then I raise an event.

I have a HumanResources class.
HumanResources creates an Employee class, providing the date of birth.
HR also wires up an event , to handle the case where the dob is today.

Employee raises the event.
HR responds to the event.

The question on the table is :::
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?
Is it a callback? Or what key concept am I missing?

Is this too tight of a coupling? or cyclic reference? or some other
gotcha?

Below is sample code.
Everything is on order I think.. the thing sticking out ... related to my
question is the
SubscribersToMyEventGotTheirEvent() method? aka, I have no idea if I'm
barking up the right tree here.
Most times I see the sender as "object sender"...
I guess I could cast it .. or change its object type. But that seems like
tight coupling.
I think java has a ICallback interface or something (long time since I
java'ed)




public class Employee

{
public delegate void BirthdayOccuredEventHandler(object sender,
EmployeeBirthdayArgs args);
public event BirthdayOccuredEventHandler BDayOcurred;


public Employee(int empid , DateTime dateOfBirth)
{
this.CheckForBDay(empid , dateOfBirth);
}

private void CheckForBDay(int empid , DateTime dob)
{

if (null!=BDayOcurred)
{
DateTime now = DateTime.Now;

if( dob.Month == now.Month && dob.Day == now.Day)
{
//today! is your birthday, raise the event

BDayOcurred(this, new EmployeeBirthdayArgs(empid, dob));
}
}

}



public void SubscribersToMyEventGotTheirEvent()
{

// Can I know somehow that my subscribers got their messages successfully,
and nothing exceptioned out??

}



}//End Employee class



public class EmployeeBirthdayArgs : EventArgs {
public EmployeeBirthdayArgs( int empid , DatTime dob ) {
this.EmployeeID = empid;
this.DateOfBirth = dob ;
}

public int EmployeeID; // i'm not using properties to shorten the example
public DateTime DateOfBirth;// i'm not using properties to shorten the
example

} // End EventArgs class



public class HumanResources()
{
public void Go()
{
Employee e = new Employee(123 , DateTime.Now)
e.BDayOcurred += new
BirthdayOccuredEventHandler(RespondToABirthdayEvent);
}



public void RespondToABirthdayEvent(object sender , EmployeeBirthdayArgs
args)
{

Console.Writeline ( arg.EmployeeID.ToString() + " " +
arg.DateOfBirth.ToShortDateString());
//this doesn't exist, but lets put a fake call into something like
"SendCard"
CardFactory.SendCard ( args );
}


}//end human resources
 
M

Marc Gravell

In the constructor, I check the to see if the employees birthday is today.
If it is today, then I raise an event.

An event on that instance? if so, nothing will have had chance to subscribe
yet...
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?

Event invocation (using the default approach) runs by executing the
delegates **on the current thread**; as such, when after you have invoked
the delegate you know that the chain has been invoked and completed.
BDayOcurred(this, new EmployeeBirthdayArgs(empid, dob));

This is dodgy; if you have no subscribers it will throw a null-object
exception; should be something like (following the common OnSomething
pattern):

// could be protected virtual if inheritance is likely
private void OnBDayOccurred(int empid) { // don't need param if available on
a field
BirthdayOccuredEventHandler handler = BDayOcurred;
if(handler!=null) handler(this, new EmployeeBirthdayArgs(empid, dob));
}

Then you just call "OnBDayOccurrent(empid);" at the point you want to use
it.
Employee e = new Employee(123 , DateTime.Now)
e.BDayOcurred += new...

Too late; missed it. You would either need to pass in the delegate into the
ctor (eugh), or (my preferred option) something like:

Employee e = new Employee();
e.BDayOccurred += new...
e.Initialise(123, DateTime.Now);

Although even then it seems an odd way of going about it... perhaps a better
option would be to have the event static on the Employee class; you
subscribe once (independent of any individual Employees), and when the ctor
detects a birthday it invokes the static event, passing the details of the
employee in question (as current). You would need the On... method to accept
the sender and use this in place of "this", but other than that it would
hold some water...

Does this help?

Marc
 
M

Markus Stoeger

sloan said:
I have an employee class.
It has an event that can be raised.
In the constructor, I check the to see if the employees birthday is today.
If it is today, then I raise an event.

As Marc already pointed out, no one will ever get that event. Raising
the event in the constructor is too early, as no one will have had the
chance to subscribe to it yet.
The question on the table is :::
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?

When you raise the event and it returns, you can be sure that all
current subscribers have received it. The problem in your case is that
there cannot be any subscribers yet because you are raising the event in
the constructor.

Is the constructor the only place where you have to check for the
birthday? Or will the event also be raised if the application is running
for a few days and suddenly it figures out that an employee has birthday
today?

I would just add a boolean property to the employee class called
"HasBirthdayToday". So you can write:

Employee e = new Employee(DateTime.Now);

if (e.HasBirthdayToday) {
// celebrate birthday
}

... additionally you can also use the event (for the case that it can
also be raised after a few days).

hth,
Max
 
S

sloan

Ok.. I screwed up using hte Constructor.

That wasn't my main focus.. I was typing out a quick sample, and got that
screwed up.
Employee e = new Employee();
e.BDayOccurred += new...
e.Initialise(123, DateTime.Now);

I see that's the way to do that part of it.

My question still remains about the callback.
 
M

Marc Gravell

My question still remains about the callback.

Really? It has been answered by two respondants already... as long as you
just use the simple event invoke [i.e. somehandler(this, someargs)] then as
soon as that line completes all subscribers have received and processed the
events. Because you just did it! (i.e. it ran on your thread).

If you are doing something more complex (which your code doesn't show), then
yes you might need some kind of callback, but how you would implement this
would depend on the details.

So: why do you think you need the callback? What is this trying to achieve?
(genuine question - not meant critically; without that info its hard to give
a better answer).

Marc
 

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