Skip to content

Commit

Permalink
Issue patterns-group#135: Add new pointcut to DelegateInterceptor: Pr…
Browse files Browse the repository at this point in the history
…oceed
  • Loading branch information
jbatte47 committed Dec 7, 2013
1 parent 068c29a commit 7ce3837
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
94 changes: 68 additions & 26 deletions src/Patterns/Interception/DelegateInterceptor.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#region FreeBSD

// Copyright (c) 2013, John Batte
// Copyright (c) 2013, The Tribe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
// the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

using System;

using Castle.DynamicProxy;

using Patterns.ExceptionHandling;

namespace Patterns.Interception
Expand Down Expand Up @@ -51,22 +49,64 @@ public class DelegateInterceptor : IInterceptor
/// whether or not an exception was thrown.
/// </param>
/// <param name="onError">The function to execute when an error occurs.</param>
/// <param name="proceed">The action to execute. Only set this if proceeding as normal is not desired.</param>
public DelegateInterceptor(Action<IInvocation> after = null, Action<IInvocation> before = null,
Func<IInvocation, bool> condition = null, Action<IInvocation> @finally = null,
Func<IInvocation, Exception, ExceptionState> onError = null)
Func<IInvocation, Exception, ExceptionState> onError = null, Action<IInvocation> proceed = null)
{
Initialize(after, before, condition, @finally, onError);
Initialize(after, before, condition, @finally, onError, proceed);
}

protected virtual Action<IInvocation> After { get; set; }
/// <summary>
/// Gets or sets the action to execute. Only set this if proceeding as normal is not desired.
/// </summary>
/// <value>
/// The action to execute.
/// </value>
public virtual Action<IInvocation> Proceed { get; set; }

protected virtual Action<IInvocation> Before { get; set; }
/// <summary>
/// Gets or sets the action to execute after proceeding. This action will not run
/// if the invocation throws an exception.
/// </summary>
/// <value>
/// The action to execute after proceeding.
/// </value>
public virtual Action<IInvocation> After { get; set; }

protected virtual Func<IInvocation, bool> Condition { get; set; }
/// <summary>
/// Gets or sets the action to execute before proceeding.
/// </summary>
/// <value>
/// The action to execute before proceeding.
/// </value>
public virtual Action<IInvocation> Before { get; set; }

protected virtual Action<IInvocation> Finally { get; set; }
/// <summary>
/// Gets or sets the interception condition. If this condition returns <c>false</c>,
/// the invocation proceeds with no further interception.
/// </summary>
/// <value>
/// The interception condition.
/// </value>
public virtual Func<IInvocation, bool> Condition { get; set; }

/// <summary>
/// Gets or sets the action to execute at the end of the interception, regardless of
/// whether or not an exception was thrown.
/// </summary>
/// <value>
/// The action to execute at the end of the interception.
/// </value>
public virtual Action<IInvocation> Finally { get; set; }

protected virtual Func<IInvocation, Exception, ExceptionState> OnError { get; set; }
/// <summary>
/// Gets or sets the function to execute when an error occurs.
/// </summary>
/// <value>
/// The function to execute when an error occurs.
/// </value>
public virtual Func<IInvocation, Exception, ExceptionState> OnError { get; set; }

/// <summary>
/// Intercepts the specified invocation.
Expand All @@ -84,7 +124,8 @@ public virtual void Intercept(IInvocation invocation)
{
if (Before != null) Before(invocation);

invocation.Proceed();
Action<IInvocation> proceed = Proceed ?? (call => call.Proceed());
proceed(invocation);

if (After != null) After(invocation);
}
Expand All @@ -107,13 +148,14 @@ public virtual void Intercept(IInvocation invocation)
}

private void Initialize(Action<IInvocation> after, Action<IInvocation> before, Func<IInvocation, bool> condition,
Action<IInvocation> @finally, Func<IInvocation, Exception, ExceptionState> onError)
Action<IInvocation> @finally, Func<IInvocation, Exception, ExceptionState> onError, Action<IInvocation> proceed)
{
Condition = condition;
Before = before;
After = after;
Finally = @finally;
OnError = onError;
Proceed = proceed;
}
}
}
5 changes: 5 additions & 0 deletions src/code-patterns-master.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ImplicitlyCapturedClosure/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBeProtected_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedAutoPropertyAccessor_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseIsOperator_002E2/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VirtualMemberNeverOverriden_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=cleansweep/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="cleansweep"&gt;&lt;AspOptimizeRegisterDirectives&gt;True&lt;/AspOptimizeRegisterDirectives&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_BOTH&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_EXPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;VBOptimizeImports&gt;True&lt;/VBOptimizeImports&gt;&lt;VBShortenReferences&gt;True&lt;/VBShortenReferences&gt;&lt;VBReformatCode&gt;True&lt;/VBReformatCode&gt;&lt;VBFormatDocComments&gt;True&lt;/VBFormatDocComments&gt;&lt;JsInsertSemicolon&gt;True&lt;/JsInsertSemicolon&gt;&lt;JsReformatCode&gt;True&lt;/JsReformatCode&gt;&lt;JsFormatDocComments&gt;True&lt;/JsFormatDocComments&gt;&lt;CssAlphabetizeProperties&gt;True&lt;/CssAlphabetizeProperties&gt;&lt;CssReformatCode&gt;True&lt;/CssReformatCode&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">cleansweep</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright (c) $CURRENT_YEAR$, The Tribe&#xD;
Expand Down

0 comments on commit 7ce3837

Please sign in to comment.