示例#1
0
    public static IInterceptionConfiguration InterceptMethod(this FluentAspectOptions options,
                                                             MethodInfo method)
    {
        if (null == method)
        {
            throw new ArgumentNullException(nameof(method));
        }

        var methodSignature = method.GetSignature();

        return(options.InterceptMethod(m => m.GetSignature().Equals(methodSignature)));
    }
示例#2
0
 static FluentAspects()
 {
     AspectOptions = new FluentAspectOptions();
     // register built-in ignore interceptors
     AspectOptions
     .NoInterceptType(t => t.Namespace?.StartsWith("WeihanLi.Common.Aspect") == true)
     ;
     // register built-in necessary interceptors
     AspectOptions.InterceptAll()
     .With <TryInvokeInterceptor>();
     AspectOptions.InterceptMethod <IDisposable>(m => m.Dispose())
     .With <DisposableInterceptor>();
 }
示例#3
0
    public static FluentAspectOptions NoInterceptMethod <T>(this FluentAspectOptions options,
                                                            MethodCallExpression methodCallExpression)
    {
        var innerMethod = methodCallExpression.Method;

        if (null == innerMethod)
        {
            throw new InvalidOperationException($"no method found");
        }

        Expression <Func <MethodInfo, bool> > expression = m => m.DeclaringType !.IsAssignableTo <T>();
        var methodSignature = innerMethod.GetSignature();

        expression = expression.And(m => m.GetSignature().Equals(methodSignature));
        return(options.NoInterceptMethod(expression.Compile()));
    }
示例#4
0
    public static IInterceptionConfiguration InterceptPropertySetter <T>(this FluentAspectOptions options,
                                                                         Expression <Func <T, object> > expression)
    {
        var prop = expression.GetProperty();

        if (null == prop)
        {
            throw new InvalidOperationException("no property found");
        }

        if (!prop.CanWrite || prop.SetMethod == null)
        {
            throw new InvalidOperationException($"the property {prop.Name} can not write");
        }

        return(options.InterceptMethod <T>(prop.SetMethod));
    }
示例#5
0
    public static IInterceptionConfiguration Intercept(this FluentAspectOptions options,
                                                       Func <IInvocation, bool> predict)
    {
        if (null == predict)
        {
            throw new ArgumentNullException(nameof(predict));
        }

        if (options.InterceptionConfigurations.TryGetValue
                (predict, out var interceptionConfiguration))
        {
            return(interceptionConfiguration);
        }

        interceptionConfiguration = new InterceptionConfiguration();
        options.InterceptionConfigurations[predict] = interceptionConfiguration;
        return(interceptionConfiguration);
    }
示例#6
0
    public static FluentAspectOptions NoInterceptProperty <T>(this FluentAspectOptions options,
                                                              Expression <Func <T, object> > expression)
    {
        var prop = expression.GetProperty();

        if (null == prop)
        {
            throw new InvalidOperationException("no property found");
        }

        if (prop.GetMethod != null)
        {
            options = options.NoInterceptMethod <T>(prop.GetMethod);
        }
        if (prop.SetMethod != null)
        {
            options = options.NoInterceptMethod <T>(prop.SetMethod);
        }
        return(options);
    }
示例#7
0
 public static IInterceptionConfiguration InterceptMethod <T>(this FluentAspectOptions options,
                                                              Expression <Func <T, object> > method)
 {
     return(options.InterceptMethod <T>(method.GetMethodExpression()));
 }
示例#8
0
 public static FluentAspectOptions WithEnricher <TEnricher>(this FluentAspectOptions options, TEnricher enricher) where TEnricher : IInvocationEnricher
 {
     options.Enrichers.Add(enricher);
     return(options);
 }
示例#9
0
 public static FluentAspectOptions WithEnricher <TEnricher>(this FluentAspectOptions options) where TEnricher : IInvocationEnricher, new()
 {
     options.Enrichers.Add(new TEnricher());
     return(options);
 }
示例#10
0
 public static FluentAspectOptions WithProperty(this FluentAspectOptions options, string propertyName,
                                                Func <IInvocation, object> propertyValueFactory, bool overwrite = false)
 {
     options.Enrichers.Add(new PropertyInvocationEnricher(propertyName, propertyValueFactory, overwrite));
     return(options);
 }
示例#11
0
 public static FluentAspectOptions UseProxyFactory <TProxyFactory>(this FluentAspectOptions options,
                                                                   params object[] parameters) where TProxyFactory : class, IProxyFactory, new()
 {
     options.ProxyFactory = ActivatorHelper.CreateInstance <TProxyFactory>(parameters);
     return(options);
 }
示例#12
0
 public static FluentAspectOptions UseProxyFactory <TProxyFactory>(this FluentAspectOptions options)
     where TProxyFactory : class, IProxyFactory, new()
 {
     options.ProxyFactory = new TProxyFactory();
     return(options);
 }
示例#13
0
 public static bool NoIntercept(this FluentAspectOptions options, Func <IInvocation, bool> predict)
 {
     return(options.NoInterceptionConfigurations.Add(predict));
 }
        public static FluentAspectOptions UseCastleProxy(this FluentAspectOptions options)
        {
            options.ProxyFactory = CastleProxyFactory.Instance;

            return(options);
        }
示例#15
0
 public static IInterceptionConfiguration InterceptDbContextSave(this FluentAspectOptions options)
 {
     return(options.InterceptMethod <DbContext>(m =>
                                                m.Name == nameof(DbContext.SaveChanges) ||
                                                m.Name == nameof(DbContext.SaveChangesAsync)));
 }
示例#16
0
 public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions options,
                                                     Func <MethodInfo, bool> methodPredict)
 {
     options.NoIntercept(invocation => methodPredict(invocation.Method ?? invocation.ProxyMethod));
     return(options);
 }
示例#17
0
 public static IInterceptionConfiguration InterceptType <T>(this FluentAspectOptions options)
 {
     return(options.InterceptMethod(m => m.DeclaringType !.IsAssignableTo <T>()));
 }
示例#18
0
 public static FluentAspectOptions NoInterceptType <T>(this FluentAspectOptions options)
 {
     options.NoInterceptMethod(m => m.DeclaringType !.IsAssignableTo <T>());
     return(options);
 }
示例#19
0
 public static FluentAspectOptions NoInterceptMethod <T>(this FluentAspectOptions options,
                                                         Expression <Action <T> > method)
 {
     options.NoInterceptMethod <T>(method.GetMethodExpression());
     return(options);
 }
示例#20
0
 public static FluentAspectOptions UseProxyFactory(this FluentAspectOptions options, IProxyFactory proxyFactory)
 {
     options.ProxyFactory = proxyFactory;
     return(options);
 }
示例#21
0
 public static IInterceptionConfiguration InterceptAll(this FluentAspectOptions options)
 => options.Intercept(_ => true);
示例#22
0
 public static IInterceptionConfiguration InterceptMethod(this FluentAspectOptions options,
                                                          Func <MethodInfo, bool> methodPredict)
 {
     return(options.Intercept(invocation => methodPredict(invocation.Method ?? invocation.ProxyMethod)));
 }
示例#23
0
 public static FluentAspectOptions InterceptDbContextSaveWithAudit <TDbContext>(this FluentAspectOptions options) where TDbContext : DbContext
 {
     options.InterceptDbContextSave <TDbContext>()
     .With <AuditDbContextInterceptor>();
     return(options);
 }
示例#24
0
 public static FluentAspectOptions UseInterceptorResolver <TResolver>(this FluentAspectOptions options)
     where TResolver : IInterceptorResolver, new()
 {
     options.InterceptorResolver = new TResolver();
     return(options);
 }