Model View Controller in VB.Net

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Model View Controller in VB.Net

 
0
  #1
Jun 25th, 2009
Hello All,

I haven't posted a question in sometime, im hoping this is where all my previous help pays off

Basically starting a newish project which must be written in VB.Net (great) but I am implementing the MVC pattern pretty much throughout the entire project as there is a requirement that after the initial project is done the underlying structure (model) will be replaced with one of our own building.

So having not much related to the above I have a question or i suppose more of a debate of how to implement MVC using events or interfaces. A simple example below shows what I mean.

I have a class which holds data (model)
  1. Public Class DataClass
  2.  
  3. Private _name As String
  4.  
  5. Public Property Name() As String
  6. Get
  7. return Me._name
  8. End Get
  9. Set(ByVal value As String)
  10. Me._name = value
  11. End Set
  12. End Property
  13.  
  14. End Class

My form (view) obviously updates according to the data held in this class. In this case the "Name" property. But I understand I can use any of the following two methods attach listeners using the Observer Pattern.

  1. ' ###
  2. ' Example 1: Interface (The way I would program with Java)
  3.  
  4. Public Interface IDataClassListener
  5.  
  6. Sub OnNameChange(ByVal d As DataClass)
  7.  
  8. End Interface
  9.  
  10. Public Class DataClass
  11.  
  12. ' Add a list of listeners for this object
  13. Private _listeners As New List(Of IDataClassListener)
  14. Private _name As String
  15.  
  16. Public Property Name() As String
  17. Get
  18. return Me._name
  19. End Get
  20. Set(ByVal value As String)
  21. Me._name = value
  22. RaiseOnNameChange() ' Raise the event
  23. End Set
  24. End Property
  25.  
  26. ' Method to add a listener to the object,
  27. ' there would be a remove listener method also
  28. Public Sub AddListener(ByVal listener As IDataClassListener)
  29. Me._listeners.Add(listener)
  30. End Sub
  31.  
  32. ' Method to raise the method on all listening objects
  33. Private Sub RaiseOnNameChange()
  34. For Each listener As IDataClassListener In me._listeners
  35. listener.OnNameChange(Me)
  36. Next
  37. End Sub
  38.  
  39. End Class
  40.  
  41. ' Then in my form I could implement the interface and
  42. ' assign it as a listener on the object
  43. Public Class MyForm
  44. Implements IDataClassListener
  45.  
  46. Private _data As DataClass
  47.  
  48. Public Sub New(ByVal data As DataClass)
  49. Me._data = data
  50. Me._data.AddListener(Me)
  51. End Sub
  52.  
  53. Private Sub OnNameChange(ByVal d As DataClass) Implements IDataClassListener.OnNameChange
  54. ' ### Update the display on change
  55. End Sub
  56.  
  57. End Class

  1. ' ###
  2. ' Example 2: Event Driven
  3.  
  4. Public Class DataClass
  5.  
  6. Delegate Sub OnNameChangeHandler(ByVal d As DataClass)
  7. Public Event OnNameChange As OnNameChangeHandler
  8. Private _name As String
  9.  
  10. Public Property Name() As String
  11. Get
  12. return Me._name
  13. End Get
  14. Set(ByVal value As String)
  15. Me._name = value
  16. RaiseEvent Me.OnNameChange(Me) ' Raise the event
  17. End Set
  18. End Property
  19.  
  20. End Class
  21.  
  22. ' Different from example 1, just add a handler to the event
  23. Public Class MyForm
  24.  
  25. Private _data As DataClass
  26.  
  27. Public Sub New(ByVal data As DataClass)
  28. Me._data = data
  29. Dim d As New DataClass.OnNameChangeHandler(AddressOf Me.OnNameChange)
  30. AddHandler _data.OnNameChange, d
  31. End Sub
  32.  
  33. Private Sub OnNameChange(ByVal d As DataClass)
  34. ' ### Update the display on change
  35. End Sub
  36.  
  37. End Class

So ok, I realize both will work, I am more comfortable with the first pattern but is there any reason why I should be using this second way? I cant seem to find a definative answer but some (very few sources) suggest the second but with no reasoning, any help is appreciated.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Model View Controller in VB.Net

 
0
  #2
Jun 26th, 2009
Event delegate Vs Interface based event.
Event delegate is better than interface based event system. You may use static or instance method as a handler with event where as with interface - implements an interface and its methods.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC