tags:

views:

543

answers:

1

I'm new to MVC and Business Objects but I'm struggling to understand how to trigger a refresh on a form when an update occurs in my business logic.

This isn't an issue for 95% of my forms because they call the update from the form that needs to be updated.

However, In some cases an inner form does and update that requires an outer form to reflect this. I've read about Handles, Delegates, AddressOf etc etc and I'm drowning in options.

In simple terms, I need three things to happen 1. Form2 (inner form) has updated info, so the user clicks Save 2. Save calls my Data Access Layer and updates the database 3. Form1 (outer form) has a DataGrid that needs to be notified so it can update via a Sub LoadGrid

It's stage 3 I can't do. How do I trigger a Sub in Form2 (LoadGrid) to do the refresh from an external class. If I can just get this to work I'm sure I'll learn from this for other scenarios.

A: 

I'm not 100% sure over the MVC way of doing this but I have achieved something similar.

In my Business Layer Class

Class BL
public event doneUpdate(o as object, e as eventArgs)

sub method doDBUpdate()

'do your stuff

RaiseEvent doneUpdate(o,e) 
end sub

End Class

Class Form
dim withEvents bl as BL

sub g handles bl.doneUpdate(o as object , e as eventargs)
  'rebind
end sub 

End Class

Now in your Code Behind for the form you can handle this event with a Sub that rebinds your data grid.

Incidentally I bubble the event to a controller which the UI acts on rather than the business layer directly.

Hope this helps

EDIT:

Hi Mitch

The parameters passed from the event to the handler can be anything although Microsoft Coding Convention says that you should pass the object and the event args back.

If you declare your event like this

Public Event myEvent(dim updated as Boolean)

and then have the handles sub have the same signature

Public Sub handler(byval updated as Boolean) handles myClass.myEvent

you should then be able the value of updated (this is only an example)

The only thing you might want to check is that you are instantiating your Business Class as with Events

Dim WithEvents myBL as new BusinessLayer

If this still fails to work (if this in WinForms rather asp.net) you may need to say something like

AddHandler myBl.myEvent AddressOf handler

Hope this helps

Dean
Hi Dean, many thanks, this is the sort of code I was looking for. One thing I'm not clear about though, the "o As Object, e As EventArgs" part. I removed this as I wasn't sure what o and e were supposed to represent in this situation. However, the Handles Sub is never called and I think it's to do with me removing the "o as object , e as eventargs". If this is correct, what do I pass as the object and EventArgs? Apologies if this is a dumb question but many thanks for your help so far.
Mitch
See above for more information
Dean
I feel we're almost there but still no joy. I've ensured that I'm using "ByVal Updated As Boolean" and altered my Business Class to include WithEvents but the Handles Sub is still not responding. The RaiseEvent is definitely being called but that's where the trail goes cold. My business layer is declared as Private Shared WithEvents _ClientDAL As New ClientDAL().
Mitch
I think you may want to post a code example if you can. Have you tried using the AddHandler myBl.myEvent AddressOf handler as suggested?
Dean
Mitch