From f2a3288ae13e86a28e885bcd8073a783c9ea29ce Mon Sep 17 00:00:00 2001 From: James Stumme Date: Sat, 24 Oct 2015 22:57:03 -0500 Subject: [PATCH] Changed CompositAroundInvoke to public and added AroundInvoke providers and .gitignore for visual studio. --- .gitignore | 197 ++++++++++++++++++ .../CompositeAroundInvoke.cs | 20 +- .../CompositeAroundInvokeProvider.cs | 64 ++++++ .../LinFu.AOP.Interfaces.csproj | 2 + .../SimpleAroundInvokeProvider.cs | 55 +++++ 5 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 src/LinFu.AOP.Interfaces/CompositeAroundInvokeProvider.cs create mode 100644 src/LinFu.AOP.Interfaces/SimpleAroundInvokeProvider.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..243b0ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,197 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +Help/ + +# Visual Studo 2015 cache/options directory +.vs/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding addin-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +*.[Cc]ache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt diff --git a/src/LinFu.AOP.Interfaces/CompositeAroundInvoke.cs b/src/LinFu.AOP.Interfaces/CompositeAroundInvoke.cs index 8572e22..1339482 100644 --- a/src/LinFu.AOP.Interfaces/CompositeAroundInvoke.cs +++ b/src/LinFu.AOP.Interfaces/CompositeAroundInvoke.cs @@ -3,10 +3,17 @@ namespace LinFu.AOP.Interfaces { - internal class CompositeAroundInvoke : IAroundInvoke + /// + /// Represents a class that can wrap itself around any given method call. + /// + public class CompositeAroundInvoke : IAroundInvoke { private readonly IList _aroundInvokeList = new List(); + /// + /// Constructs a composit around invoke. + /// + /// An collection of to public CompositeAroundInvoke(IEnumerable aroundInvokeList) { if (aroundInvokeList == null) @@ -22,7 +29,11 @@ public CompositeAroundInvoke(IEnumerable aroundInvokeList) } } - + /// + /// This method will be called immediately after the actual method call is executed. + /// + /// The associated with the method call. + /// The value returned from the actual method call. public void AfterInvoke(IInvocationInfo context, object returnValue) { foreach (var invoke in _aroundInvokeList) @@ -31,6 +42,11 @@ public void AfterInvoke(IInvocationInfo context, object returnValue) } } + /// + /// This method will be called just before the actual method call is executed. + /// + /// The associated with the method call. + /// public void BeforeInvoke(IInvocationInfo context) { foreach (var invoke in _aroundInvokeList) diff --git a/src/LinFu.AOP.Interfaces/CompositeAroundInvokeProvider.cs b/src/LinFu.AOP.Interfaces/CompositeAroundInvokeProvider.cs new file mode 100644 index 0000000..9c64c5f --- /dev/null +++ b/src/LinFu.AOP.Interfaces/CompositeAroundInvokeProvider.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LinFu.AOP.Interfaces +{ + class CompositeAroundInvokeProvider : IAroundInvokeProvider + { + /// + /// Gets or sets the valud indicating the Predicates that will determine weather or not + /// the method should be intercepted by the associated . + /// + public IDictionary> MethodInvokesAndPredicates { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// + /// Dictionary of and predicates to determine if a method should be intercepted. + /// + /// + /// Specifing null for the aroundInvokeAndPredicates predicate will use the default ShouldReplace predicate + /// in . + /// + public CompositeAroundInvokeProvider(IDictionary> aroundInvokeAndPredicates) + { + if (aroundInvokeAndPredicates != null) + { + MethodInvokesAndPredicates = new Dictionary>(); + foreach (var invokeAndPredicate in aroundInvokeAndPredicates) + { + MethodInvokesAndPredicates.Add(invokeAndPredicate.Key, invokeAndPredicate.Value ?? ShouldReplace); + } + } + } + + /// + /// Determines whether or not a particular method body should be invoked around at runtime. + /// + /// The context surrounding the method call. + /// Returns true. + public virtual bool ShouldReplace(IInvocationInfo context) + { + return true; + } + + #region IAroundInvokeProvider Members + /// + /// Gets the surrounding implemetation for a given invocation context. + /// + /// The context surrounding the method call. + /// The interceptor that will invoke around the method at runtime. + public IAroundInvoke GetSurroundingImplementation(IInvocationInfo context) + { + var invokes = new HashSet(); + foreach (var invokePredicate in MethodInvokesAndPredicates.Where(x => x.Value != null && x.Value(context))) + { + invokes.Add(invokePredicate.Key); + } + return (invokes.Count > 0) ? new CompositeAroundInvoke(invokes) : null; + } + #endregion + } +} diff --git a/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj b/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj index b2a3d15..e54f494 100644 --- a/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj +++ b/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj @@ -65,6 +65,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/src/LinFu.AOP.Interfaces/SimpleAroundInvokeProvider.cs b/src/LinFu.AOP.Interfaces/SimpleAroundInvokeProvider.cs new file mode 100644 index 0000000..5b66c69 --- /dev/null +++ b/src/LinFu.AOP.Interfaces/SimpleAroundInvokeProvider.cs @@ -0,0 +1,55 @@ +using System; + +namespace LinFu.AOP.Interfaces +{ + /// + /// Represents the simplest possible implementation + /// that will allow the user to use the original around invoke implementation as part + /// of the interceptor call. + /// + public class SimpleAroundInvokeProvider : IAroundInvokeProvider + { + /// + /// Gets or sets the value indicating the Predicate that will determine whether or not + /// the method should be intercepted. + /// + /// The interceptor predicate. + public Func MethodInvokePredicate { get; set; } + + private readonly IAroundInvoke _around; + + /// + /// Initializes a new instance of the class. + /// + /// The around invoke interceptor. + public SimpleAroundInvokeProvider(IAroundInvoke around) + { + _around = around; + MethodInvokePredicate = ShouldReplace; + } + + /// + /// Determines whether or not a particular method body should be invoked around at runtime. + /// + /// The context surrounding the method call. + /// Returns true. + public virtual bool ShouldReplace(IInvocationInfo context) + { + return true; + } + + #region IAroundInvokeProvider Members + /// + /// Gets the surrounding implemetation for a given invocation context. + /// + /// The context surrounding the method call. + /// The interceptor that will invoke around the method at runtime. + public IAroundInvoke GetSurroundingImplementation(IInvocationInfo context) + { + if (MethodInvokePredicate == null || !MethodInvokePredicate(context)) + return null; + return _around; + } + #endregion + } +}