I have a FTPDataTransfer
-class that has a state (FTPDataTransferState
). This class provides a lot of transfer-methods like ReceiveData
(overloaded), SendData
, ...
How can I change the state to Ready
, Transfer
, ... without chaning the value in every transfer-method?
Asked
Active
Viewed 131 times
0

Luca Nate Mahler
- 1,292
- 2
- 13
- 28
-
doesn't the value of `FTPDataTransferState` depend on logic performed in mentioned methods...? – Kuba Wyrostek Aug 13 '12 at 09:29
-
Yes, but it is redundant and uncomfortable... I had to write `state = FTPDataTransferState.Transfer` and at the end `state = FTPDataTransferState.Ready` in every method... – Luca Nate Mahler Aug 13 '12 at 09:33
2 Answers
1
The leading AOP toolkit for .NET is PostSharp. The way it would work in your case is that you'd define a custom attribute specifying the state that should be set when the method is to execute, apply the attribute to the appropriate methods, and define (in one place) the code setting/resetting the state. The toolkit would make this code run when the methods are entered/left.

cynic
- 5,305
- 1
- 24
- 40
1
You can use PostSharp for this. In more detail, the OnMethodBoundaryAspect
is the aspect you want to use. In your case it could look like:
using PostSharp.Aspects;
[Serializable]
public sealed class ReadyOnExit : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs args)
{
var state = (FTPDataTransferState)args.Instance;
state.Transfer(FTPDataTransferState.Ready);
}
}

pjvds
- 938
- 6
- 11