I'm trying to implement the Observer pattern on a school application using PostSharp.
The situation is as follows: I have a Repository that I want to notify each TesterForm (forms that allow manipulation of the data within the Repository) each time a change is made.
This is the aspect I want to use to add the Observable part to my Repository:
[Serializable]
class ObservableAspect : InstanceLevelAspect
{
[IntroduceMember]
List<TesterForm> LT;
[IntroduceMember]
public void notifyChange()
{
foreach (TesterForm x in LT)
{
x.refreshListBoxBuguri();
}
}
[IntroduceMember]
public void Subscribe(TesterForm t)
{
LT.Add(t);
}
}
Then this aspect applied to every method in Repository that changes the data:
[Serializable]
class ObservableNotify : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs args)
{
((Repository)args.Instance).notifyChange();
}
}
And then this aspect applied to my TesterForm constructor so it Subscribes to my Repository as soon as it is created:
class ObserverAspect : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs args)
{
((TesterForm)args.Instance).controller.repository.Subscribe((TesterForm)args.Instance);
}
}
Now, the problem I'm facing is that I have no idea how to call a method that I inject with one aspect, from another aspect (ex: repository.Subscribe from TesterForm) or whether this is even possible.
I have done some research on the PostSharp website, but found no details on such an implementation. Also, Google yielded no useful results.
Thanks in advance for the help!
Additional info: using VS 2013, PostSharp works correctly as I've built other simpler aspects (logging and performance monitoring) that do their job as intended.
Cheers!