Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov authored and NikolayPianikov committed Oct 27, 2018
1 parent 02cff84 commit 4399899
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 314 deletions.
8 changes: 4 additions & 4 deletions Docs/IoC.Interception_net40.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Docs/IoC.Interception_net47.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Docs/IoC.Interception_netcoreapp1.0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Docs/IoC.Interception_netcoreapp2.1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Docs/IoC.Interception_netstandard1.3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 51 additions & 47 deletions IoC.Interception.Source/IoC.Interception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace IoC.Features.Interception
internal interface IInterceptorRegistry
{
[NotNull]
IDisposable Register([NotNull] Predicate<IBuildContext> filter, [NotNull] [ItemNotNull] params IInterceptor[] interceptors);
IDisposable Register([NotNull] Predicate<Key> filter, [NotNull] [ItemNotNull] params IInterceptor[] interceptors);
}
}

Expand All @@ -168,18 +168,20 @@ namespace IoC.Features.Interception
{
using System;
using System.Collections.Generic;
// ReSharper disable once RedundantUsingDirective
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Castle.DynamicProxy;
// ReSharper disable once RedundantUsingDirective

// ReSharper disable once ClassNeverInstantiated.Global
internal class InterceptorBuilder : IInterceptorRegistry, IBuilder
{
private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator();
private readonly List<InterceptorsInfo> _interceptors = new List<InterceptorsInfo>();

public IDisposable Register(Predicate<IBuildContext> filter, params IInterceptor[] interceptors)
public IDisposable Register(Predicate<Key> filter, params IInterceptor[] interceptors)
{
if (filter == null) throw new ArgumentNullException(nameof(filter));
if (interceptors == null) throw new ArgumentNullException(nameof(interceptors));
Expand All @@ -202,10 +204,10 @@ public Expression Build(Expression bodyExpression, IBuildContext buildContext)
{
lock (_interceptors)
{
var proxyGeneratorExpression = buildContext.AppendValue(ProxyGenerator);
var proxyGeneratorExpression = Expression.Constant(ProxyGenerator);
foreach (var interceptors in _interceptors)
{
if (interceptors.Accept(buildContext))
if (interceptors.Accept(buildContext.Key))
{
bodyExpression = interceptors.Build(bodyExpression, buildContext, proxyGeneratorExpression);
}
Expand All @@ -227,16 +229,16 @@ private class InterceptorsInfo
private static readonly MethodInfo CreateClassProxyWithTargetMethodInfo = ProxyGeneratorTypeInfo.GetDeclaredMethods(nameof(ProxyGenerator.CreateClassProxyWithTarget)).First(i => i.GetParameters().Select(j => j.ParameterType).SequenceEqual(FuncTypes));
#endif

private readonly Predicate<IBuildContext> _filter;
private readonly Predicate<Key> _filter;
private readonly IInterceptor[] _interceptors;

public InterceptorsInfo(Predicate<IBuildContext> filter, IInterceptor[] interceptors)
public InterceptorsInfo(Predicate<Key> filter, IInterceptor[] interceptors)
{
_filter = filter;
_interceptors = interceptors;
}

public bool Accept(IBuildContext buildContext) => _filter(buildContext);
public bool Accept(Key key) => _filter(key);

public Expression Build(Expression bodyExpression, IBuildContext buildContext, Expression proxyGeneratorExpression)
{
Expand All @@ -250,9 +252,9 @@ public Expression Build(Expression bodyExpression, IBuildContext buildContext, E
var interfaces = typeInfo.ImplementedInterfaces.ToArray();
#endif

var typeExpression = buildContext.AppendValue(type);
var interfacesExpression = buildContext.AppendValue(interfaces);
var interceptorsExpression = buildContext.AppendValue(_interceptors);
var typeExpression = Expression.Constant(type);
var interfacesExpression = Expression.Constant(interfaces);
var interceptorsExpression = Expression.Constant(_interceptors);
var args = new[] {typeExpression, interfacesExpression, bodyExpression, interceptorsExpression};
if (isInterface)
{
Expand All @@ -278,6 +280,7 @@ namespace IoC.Features
{
using System;
using System.Linq;
// ReSharper disable once RedundantUsingDirective
using System.Reflection;
using System.Runtime.CompilerServices;
using Castle.Core.Internal;
Expand All @@ -298,14 +301,29 @@ public static class FluentInterception
/// <returns>The binding token.</returns>
[MethodImpl((MethodImplOptions) 256)]
[NotNull]
public static IDisposable Intercept([NotNull] this IContainer container, [NotNull] Predicate<IBuildContext> filter, [NotNull] [ItemNotNull] params IInterceptor[] interceptors)
public static IDisposable Intercept([NotNull] this IContainer container, [NotNull] Predicate<Key> filter, [NotNull] [ItemNotNull] params IInterceptor[] interceptors)
{
if (container == null) throw new ArgumentNullException(nameof(container));
if (filter == null) throw new ArgumentNullException(nameof(filter));
if (interceptors == null) throw new ArgumentNullException(nameof(interceptors));
return container.Resolve<IInterceptorRegistry>().Register(filter, interceptors);
}

/// <summary>
/// Registers interceptors.
/// </summary>
/// <param name="container">The target container.</param>
/// <param name="interceptors">The set of interceptors.</param>
/// <returns>The binding token.</returns>
[MethodImpl((MethodImplOptions) 256)]
[NotNull]
public static IDisposable Intercept<T>([NotNull] this IContainer container, [NotNull] [ItemNotNull] params IInterceptor[] interceptors)
{
if (container == null) throw new ArgumentNullException(nameof(container));
if (interceptors == null) throw new ArgumentNullException(nameof(interceptors));
return container.Intercept(new Key(typeof(T)), interceptors);
}

/// <summary>
/// Registers interceptors.
/// </summary>
Expand All @@ -319,64 +337,50 @@ public static IDisposable Intercept([NotNull] this IContainer container, Key key
{
if (container == null) throw new ArgumentNullException(nameof(container));
if (interceptors == null) throw new ArgumentNullException(nameof(interceptors));
return container.Resolve<IInterceptorRegistry>().Register(ctx =>
return container.Resolve<IInterceptorRegistry>().Register(targetKey =>
{
if (ctx.Key.Equals(key))
if (targetKey.Equals(key))
{
return true;
}

var type = ctx.Key.Type;
var curType = key.Type;
var targetType = targetKey.Type;
var interceptedType = key.Type;
#if NET40
var isGenericType = type.IsGenericType;
if (!isGenericType) {
var isGenericTargetType = targetType.IsGenericType;
if (!isGenericTargetType)
{
return false;
}

var curIsGenericType = curType.IsGenericTypeDefinition || curType.GetGenericArguments().Any(i => i.GetAttribute<GenericTypeArgumentAttribute>() != null);;
var interceptedIsGenericType = interceptedType.IsGenericTypeDefinition || interceptedType.GetGenericArguments().Any(i => i.GetAttribute<GenericTypeArgumentAttribute>() != null);
#else
var typeInfo = type.GetTypeInfo();
var isGenericType = typeInfo.IsGenericType;
if (!isGenericType) {
var targetTypeInfo = targetType.GetTypeInfo();
var isGenericTargetType = targetTypeInfo.IsGenericType;
if (!isGenericTargetType) {
return false;
}

var curTypeInfo = curType.GetTypeInfo();
var curIsGenericType = curTypeInfo.IsGenericTypeDefinition || curTypeInfo.GenericTypeArguments.Any(i => i.GetAttribute<GenericTypeArgumentAttribute>() != null);
var interceptedTypeInfo = interceptedType.GetTypeInfo();
var interceptedIsGenericType = interceptedTypeInfo.IsGenericTypeDefinition || interceptedTypeInfo.GenericTypeArguments.Any(i => i.GetAttribute<GenericTypeArgumentAttribute>() != null);
#endif

if (curIsGenericType)
if (!interceptedIsGenericType)
{
return false;
}

#if NET40
var genericTypeDefinition = type.GetGenericTypeDefinition();
var curGenericTypeDefinition = curType.GetGenericTypeDefinition();
var genericTypeDefinition = targetType.GetGenericTypeDefinition();
var curGenericTypeDefinition = interceptedType.GetGenericTypeDefinition();
#else
var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
var curGenericTypeDefinition = curTypeInfo.GetGenericTypeDefinition();
var genericTypeDefinition = targetTypeInfo.GetGenericTypeDefinition();
var curGenericTypeDefinition = interceptedTypeInfo.GetGenericTypeDefinition();
#endif
return new Key(genericTypeDefinition, ctx.Key.Tag).Equals(new Key(curGenericTypeDefinition, key.Tag));
}

return false;
return new Key(genericTypeDefinition, targetKey.Tag).Equals(new Key(curGenericTypeDefinition, key.Tag));
},
interceptors);
}

/// <summary>
/// Registers interceptors.
/// </summary>
/// <param name="container">The target container.</param>
/// <param name="interceptors">The set of interceptors.</param>
/// <returns>The binding token.</returns>
[MethodImpl((MethodImplOptions) 256)]
[NotNull]
public static IDisposable Intercept<T>([NotNull] this IContainer container, [NotNull] [ItemNotNull] params IInterceptor[] interceptors)
{
if (container == null) throw new ArgumentNullException(nameof(container));
if (interceptors == null) throw new ArgumentNullException(nameof(interceptors));
return container.Intercept(new Key(typeof(T)), interceptors);
}
}
}

Expand Down
Loading

0 comments on commit 4399899

Please sign in to comment.