Using a delegate

J

Jon

I am a little problem deciding when to use delegates and when not to. Is the
following a good example?

I have an order class and a messenger class (this does things like send
emails, pages, faxes). Currently we create the order and in the creation
function, we call other functions in sequence that do things that are not
order specific (other things may send emails besides the order being
created). Would it be better to create a delegate for the order creation
"event" and have the messenger class subscribe to it so it knows to send off
an email without the create function directly calling it? What's the real
benefit of doing that?

Example, an order is created like below...
***FormOrder
Private Sub CreateButton_Click()
Dim o as Order
o = new order (var,var,var)
End Sub
***FormOrder


Class Order
Function Create (foo,bar,boo) as String
insert some stuff into DB
'If needed, send an email
Messenger.CreateEmail (OrderNum, Time, etc)
Me.InsertCreatedLog(OrderNum, Time, etc)

Return OrderNum
End Function
End Class

Class Messenger
Public Shared Function CreateEmail ()
'Insert records into DB for the email processor to pick up
End Function
End Class


If using a delegate is the preferred way to go, can someone direct me to
some non-cryptic info on how to create the delegate and subscribe to/use it?

Thanks much
 
G

Guest

Using delegates/ events are a way to make an object independent of other
objects in the system. You should use them to report user intiated or
intermmediate events. In your case there really is no reason to wait for the
Create to complete and then fire an event. Instead, just let the create
return back and then pass the new Order to your Email object. I wouldn't
email right from the Create either as this is starting to break the
encapsulation of your Order object. Emailing an order is something that
should be performed by an Email object instead.
 
J

Jon

Thanks. If I wait for the create to return back, then I'm calling the create
email function from the button click event, right? Is that correct practice?
Shouldn't I be calling the create email from the business logic layer?
Sometimes this gets a bit confusing!
 
G

Guest

Yes. The form click would send the mail using YOUR Email object. (A business
object). I just view creating the order and emailing it as access to 2
seperate business objects. If you add too much functionality to your Order
object it starts to become too dependent and coupled to a fixed set of
functionality. This will make the object overly complex and not very
re-useable.
 

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