943,840 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1819
  • VB.NET RSS
Jun 25th, 2009
0

Model View Controller in VB.Net

Expand Post »
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)
VBNet Syntax (Toggle Plain Text)
  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.

VBNet Syntax (Toggle Plain Text)
  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

VBNet Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jun 26th, 2009
0

Re: Model View Controller in VB.Net

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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Mifare Card Contactless
Next Thread in VB.NET Forum Timeline: connect sql server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC