Today I was re-factoring a library that I created and share code across multiple platforms (WPF, WF, WP7, WP8).
The idea is that I use inheritance to extend functionality in classes. The top class expose couple of events to the client, this event is raised in a method that is passed down to the base class using the constructors that accept some parameters.
The object is basically used as a singleton.
Example:
public class SomeHandler : BaseHandler
{
public event EventHandler<Custom1EventArgs> RequestCompleted = delegate {};
public event EventHandler<Custom2EventArgs> ActionHandled = delegate { };
protected SomeHandler()
: base(new CustomUtil(), new CustomRepository(),
new AnotherRepository(ADelegateMethod),
AnotherDelegateMethod, AnotherOneDelegateMethod) {}
#region [ Singleton ]
public static SomeHandler Instance
{ get { return Nested.instance;} }
class Nested
{
internal static readonly SomeHandler instance = new SomeHandler ();
}
#endregion
private static void ADelegateMethod(Custom1EventArgs args)
{
Instance.RequestCompleted (Instance, args);
}
private static void AnotherDelegateMethod(
Custom2EventArgs args, CustomObject result)
{
// Do stuff....
AnotherCustomObject cusObj = new AnotherCustomObject(args);
Instance.ActionHandled (Instance, cusObj);
}
private static void AnotherOneDelegateMethod(CustomObject result)
{
// Do some stuff...
}
}
OK, if you have noticed, I need to mark the delegate methods as static because the inject is happened in the constructor parameters. But this forced me to make the events static. To workaround I relied to the fact that the user is always using the Instance
singleton instance of my object, though the object can be initialized if they want, sealed
is not an option at the moment because it is also used as base inherited class to another class in a specific special implementation.
Is it bad to change the events to static? It doesn't seem proper to me, what do you think? Can this design get better?
Actually I use the delegates as a part of code that needs to be executed in a specific time from other objects new AnotherRepository(ADelegateMethod)
or the BaseHandler
class so i can basically provide the client with information.