Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is this kind of encapsulation right for the event and delegate?

Is this kind of encapsulation right for the event and delegate?

Scheduled Pinned Locked Moved C#
sysadminooptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Salil Khedkar
    wrote on last edited by
    #1

    Sorry for a lengthy post, but I am very new to events and delegates and want you guys opinion on this. :) Is this kind of encapsulation ideal? I have encapsulated the entire event functionality (the related event and delegate) inside the publisher class itself, and I think this is making it a lot cleaner that the MSDN example. (no offense) using System; namespace EventsAndDelegates { #region The Suscriber public class WebClient { // Note the mathcing signature with the delegate public void ShowStatus(ServerClass ContentServer, ServerClass.ExecutionCompleteEventArgs ServerEventArgs) { Console.WriteLine("Execution is finished. Result = " + ServerEventArgs.ExecutionResult); } } #endregion #region The Publisher public class ServerClass { // Delegate for event handling public delegate void ExecutionCompleteHandlerDelegate(ServerClass ContentServer, ExecutionCompleteEventArgs ContentServedEventArgs); // Event of type the delegate define above public static event ExecutionCompleteHandlerDelegate EventExecutionComplete; // Event argument public class ExecutionCompleteEventArgs : System.EventArgs { public int ExecutionResult; // Constructor accepts Returncode public ExecutionCompleteEventArgs(int ReturnCode) { ExecutionResult = ReturnCode; } } // This class will fire this event when it occurs public void OnEventExecutionComplete(ExecutionCompleteEventArgs ServerEventArgs) { // (Only) if somebody has subscribed to the event, notify them if(EventExecutionComplete!=null) EventExecutionComplete(this, ServerEventArgs); } // The method which will cause the event public void ServerBigTask() { // Server does something big and on finish raises the event OnEventExecutionComplete(new ExecutionCompleteEventArgs(10)); } } #endregion #region The Framework class AppClass { [STAThread] static void Main(string[] args) { Console.WriteLine("Main started..."); ServerClass ContentServer = new ServerClass(); WebClient IEExplorer = new WebClient(); ServerClass.EventExecutionComplete+= new ServerClass.ExecutionCompleteHandlerDelegate(IEExplorer.ShowStatus); ContentServer.ServerBigTask(); Console.WriteLine("Main ended."); Console.ReadLine(); } } #endregion }

    H 1 Reply Last reply
    0
    • S Salil Khedkar

      Sorry for a lengthy post, but I am very new to events and delegates and want you guys opinion on this. :) Is this kind of encapsulation ideal? I have encapsulated the entire event functionality (the related event and delegate) inside the publisher class itself, and I think this is making it a lot cleaner that the MSDN example. (no offense) using System; namespace EventsAndDelegates { #region The Suscriber public class WebClient { // Note the mathcing signature with the delegate public void ShowStatus(ServerClass ContentServer, ServerClass.ExecutionCompleteEventArgs ServerEventArgs) { Console.WriteLine("Execution is finished. Result = " + ServerEventArgs.ExecutionResult); } } #endregion #region The Publisher public class ServerClass { // Delegate for event handling public delegate void ExecutionCompleteHandlerDelegate(ServerClass ContentServer, ExecutionCompleteEventArgs ContentServedEventArgs); // Event of type the delegate define above public static event ExecutionCompleteHandlerDelegate EventExecutionComplete; // Event argument public class ExecutionCompleteEventArgs : System.EventArgs { public int ExecutionResult; // Constructor accepts Returncode public ExecutionCompleteEventArgs(int ReturnCode) { ExecutionResult = ReturnCode; } } // This class will fire this event when it occurs public void OnEventExecutionComplete(ExecutionCompleteEventArgs ServerEventArgs) { // (Only) if somebody has subscribed to the event, notify them if(EventExecutionComplete!=null) EventExecutionComplete(this, ServerEventArgs); } // The method which will cause the event public void ServerBigTask() { // Server does something big and on finish raises the event OnEventExecutionComplete(new ExecutionCompleteEventArgs(10)); } } #endregion #region The Framework class AppClass { [STAThread] static void Main(string[] args) { Console.WriteLine("Main started..."); ServerClass ContentServer = new ServerClass(); WebClient IEExplorer = new WebClient(); ServerClass.EventExecutionComplete+= new ServerClass.ExecutionCompleteHandlerDelegate(IEExplorer.ShowStatus); ContentServer.ServerBigTask(); Console.WriteLine("Main ended."); Console.ReadLine(); } } #endregion }

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      It's not a bad design, but keep in mind that encapsulation != nested classes. Nesting classes (or any other types, including delegates) is typically when you don't want to share a type or even make it non-public. In the extremely dynamic world of application development, that's a pretty poor assumption. You should design your object model to be extensible and assume little when possible. To that your delegate and related EventArgs derivative will only be used for this class is probably not a safe assumption. Besides - do you really want your clients to have to type all that every time they want to use it? Making your object model easy to use is also important in many libraries and applications. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      S 1 Reply Last reply
      0
      • H Heath Stewart

        It's not a bad design, but keep in mind that encapsulation != nested classes. Nesting classes (or any other types, including delegates) is typically when you don't want to share a type or even make it non-public. In the extremely dynamic world of application development, that's a pretty poor assumption. You should design your object model to be extensible and assume little when possible. To that your delegate and related EventArgs derivative will only be used for this class is probably not a safe assumption. Besides - do you really want your clients to have to type all that every time they want to use it? Making your object model easy to use is also important in many libraries and applications. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        S Offline
        S Offline
        Salil Khedkar
        wrote on last edited by
        #3

        Got it... ;) Thanks a lot... :rose: Salil Khedkar [^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups