torsdag den 20. januar 2011

Sapit - now with Retry functionality added

New functionality

I have just checked in an updated version of Sapit (Codeplex). Included now is the Retry attribute, that automatically retries failed method calls.

How to use Sapit

I figured i was about time I threw in a little documentation on how to apply Sapit to your code. The table below describes each of the 5 attributes and their parameters.

AtrtibuteParameterMeaning
CacheCaches the result of the method call.
CacheSecondsThe number of seconds to cache the result
CircuitBreakerMonitors the method for exceptions, and if too many consecutive calls fails, it shuts off the method for a period of time.
MaxConsecutive-FailuresThe maximum number of consecutive failures before the method is shut off
ShutOffPeriodSecondsThe number of seconds to leave the method shut off
BehaviorWhen-ShutOffHow to react to method calls when shut off, return af default value or throw an exception
ShutOffReturnValueThe default value (or exception message) to return
ResponseTimeThrottleThrottles away calls to a method when the response time rises above a given threshold
ThrottleThreshold-MillisecondsThe maximum response time before throttling sets in
SkipCallsWhen-ThrottlingHow many calls to throttle away when throttling
BehaviorWhen-ThrottlingHow to react to method calls when throttling, return af default value or throw an exception
ThrottleReturnValueCaches the result of the method callThe default value (or exception message) to return
RetryRetries failed method calls up to a given number of times before giving up.
TimesToRetryThe maximum number of times to retry the metod call before giving up
TimeoutAborts method calls taking longer than a given limit.
TimeoutMillisecondsThe number of milliseconds before aborting the method call

Each of the above attributes are defined in each of the assemblies Sapit.PostSharp.dll, Sapit.Unity.dll and Sapit.Windsor.dll, and they functionality in the assembly Sapit.dll. This means, that when you wish to include Sapit in your project, you only need to add a reference to Sapit.dll and to Sapit.<your IoC>.dll, where <your IoC> is your IoC of choice, PostSharp, Unity or Windsor. After adding these two references, you just apply the appropriate attributes to the methods you want. The only thing left is to configure your IoC (this is only true of Unity and Windsor, since PostSharp simply alters the MSIL after compilation).

Configure your IoC to use Sapit

Unity
Use the following code to configure Unity to use Sapit, where IMyInterface is the interface for the class MyImplementation:
IUnityContainer container = new UnityContainer();
ServiceProvider.IUC = container;
container.AddNewExtension<Interception>();
container.RegisterType<IMyInterface, MyImplementation>();
container.Configure<Interception>().SetInterceptorFor<IMyInterface>(new TransparentProxyInterceptor());

Windsor
Use the following code to configure Windsor to use Sapit, where IMyInterface is the interface for the class MyImplementation:
WindsorContainer ioc = new WindsorContainer();
ServiceProvider.IWC = ioc;
ioc.Register(Component.For<CacheInterceptor<string>>());
ioc.Register(Component.For<CircuitBreakerInterceptor>());
ioc.Register(Component.For<ResponseTimeThrottleInterceptor>());
ioc.Register(Component.For<TimeoutInterceptor>());
ioc.Register(Component.For<RetryInterceptor>());
ioc.Register(Component.For<IMyInterface>()
.ImplementedBy<MyImplementation>()
    .Interceptors<CacheInterceptor<string>>()
    .Interceptors<CircuitBreakerInterceptor>()
    .Interceptors<ResponseTimeThrottleInterceptor>()
    .Interceptors<TimeoutInterceptor>()
    .Interceptors<RetryInterceptor>());

Feedback?

Please let me know if you find bugs in the code, errors in the above documentation, or if there is something unclear.
Also, I am very open to suggestions and requests for improvements and more features.
Enjoy :-)

Ingen kommentarer:

Send en kommentar