www.pudn.com > OracleServiceManager.zip > ServiceSubject.cs


using System; 
using System.Collections; 
using System.Diagnostics; 
using System.ServiceProcess; 
 
namespace kae.ServiceStatePublisher 
{ 
	///  
	/// ServiceSubject is the publisher establishing the client contract or interface.  ServiceSubject sends a notification to its observers, or subscribers, when its state changes. 
	///  
	///  
	/// ServiceSubject is part of the Observer Pattern implementation for the ServiceStatePublisher library.  The .NET Framework supports this pattern through its built-in delegate and event mechanism. 
	/// For more information on the state pattern see Design Patterns: Elements of Reusable Code by Gamma, et al. 
	///  
	public class ServiceSubject : ServiceContext 
	{ 
		public event EventHandler ServerChanged; 
		public event EventHandler ServiceChanged; 
		public event EventHandler StateChanged; 
 
		private string _serverName; 
 
		public ServiceSubject() : base() { } 
 
		public ServiceSubject( ServiceController service) 
		{ 
			Controller = service; 
		} 
 
		public virtual string ServerName 
		{ 
			get { return _serverName; } 
			set 
			{ 
				_serverName = value; 
				OnServerChanged( EventArgs.Empty); 
				if (Controller != null && _serverName != Controller.MachineName) 
					Controller = null; 
			} 
		} 
 
		public override ServiceController Controller 
		{ 
			get { return _controller; } 
			set 
			{ 
				_controller = value; 
				_state = QueryServiceState(); 
				OnServiceChanged( EventArgs.Empty); 
			} 
		} 
 
		public override ServiceState State 
		{ 
			get { return _state; } 
			set 
			{ 
				_state = value; 
				OnStateChanged( EventArgs.Empty); 
			} 
		} 
 
		public virtual void RefreshServices() 
		{ 
			OnServerChanged( EventArgs.Empty); 
		} 
 
		protected virtual void OnServerChanged( System.EventArgs e) 
		{ 
			if (ServerChanged != null) 
				ServerChanged( this, e); 
		} 
 
		protected virtual void OnServiceChanged( System.EventArgs e) 
		{ 
			if (ServiceChanged != null) 
				ServiceChanged( this, e); 
		} 
 
		protected virtual void OnStateChanged( System.EventArgs e) 
		{ 
			if (StateChanged != null) 
				StateChanged( this, e); 
		} 
	} 
}