Is this kind of encapsulation right for the event and delegate?
-
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 }
-
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 }
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] -
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]Got it... ;) Thanks a lot... :rose: Salil Khedkar [^]