For example, I want to have some custom exception handling policy. It would be neat if I could simply decorate the method with an attribute as below.
[ExceptionHandlePolicy]
public void Method(){...}
To achieve this in C#:
- An attribute class implementing the IMessageSink is to be defined;
- The class, to which this attribute is going to be applied, must inherit from ContextBoundObject or MarshalByRefObject.
The other way around, is to use the Action delegate from the .Net Framework to help to build a custom exception handling policy class.
public class RetryPolicy { ... private Action _action; ... public RetryPolicy(Action action) { _action = action; } public void Execute() { int retryCount = 0; if(_action != null) { while(retryCount < MAX_RETRY) { try { _action(); retryCount = MAX_RETRY; } catch(Exception ex) { if(ShouldRetry(ex) && retryCount < MAX_RETRY) i++; else throw; } } } } .... }
To apply my custom retry policy, I can modify the existing data access layer slightly.
public static string GetConfigurationValue(string dbConnString, int configId) { string configValue = string.Empty; RetryPolicy policy = new RetryPolicy( () => { using(TestDAL dal = new TestDAL(dbConnString)) { configValue = dal.GetConfigurationByValue(configId); } }); policy.Execute(); return configValue; }
4 comments:
Is there a way to apply same thing for caching ? I mean AOP style.
Yes, though the AOP style is achieved via message interception.
If it is a web method in a web service that you want to cache, you can annotate the method with the built-in attribute, such as:
[System.Web.Services.WebMethod(CacheDuration=60)]
Actually I have known that already. And it is possible with MVC as well as WebMethod. But need it for example data or business service libraries. Like PostSharp does. But without install something. (Team environment problems...)
Thanks for your answer BTW.
Can I download all source code sample? thx
Post a Comment