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(RespondToABirthdayEven t);
}
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