/// <inheritdoc /> public void InjectPropertyDependencies(MethodAspect aspect) { if (_dependencyProvider == null) { _dependencyProvider = _dependencyProviderHolder.Get(); } if (_dependencySelector == null) { _dependencySelector = _dependencySelectorHolder.Get(); } foreach (var propertyDependency in _dependencySelector.SelectPropertyDependencies(aspect.GetType())) { object service; if (propertyDependency.Dependency.Transparent) { service = propertyDependency.Dependency.ServiceKey == null ? _dependencyProvider.GetTransparentService(propertyDependency.Property.PropertyType) : _dependencyProvider.GetTransparentNamedService( propertyDependency.Property.PropertyType, propertyDependency.Dependency.ServiceKey); } else { service = propertyDependency.Dependency.ServiceKey == null ? _dependencyProvider.GetService(propertyDependency.Property.PropertyType) : _dependencyProvider.GetNamedService( propertyDependency.Property.PropertyType, propertyDependency.Dependency.ServiceKey); } propertyDependency.PropertySetter(aspect, service); } }
/// <inheritdoc /> public void Finalize(MethodAspect methodAspect) { if (methodAspect is IDisposable ds) { ds.Dispose(); } }
/// <inheritdoc /> public MethodBoundaryAspect[] CreateBoundaryAspects(IInvocationSignature signature) { if (_aspectDeclarationCollector == null) { _aspectDeclarationCollector = _aspectDeclarationCollectorHolder.Get(); } if (_aspectOrderStrategy == null) { _aspectOrderStrategy = _orderStrategyHolder.Get(); } if (_aspectFinalizer == null) { _aspectFinalizer = _aspectFinalizerHolder.Get(); } if (_dependencySelector == null) { _dependencySelector = _dependencySelectorHolder.Get(); } var methodBoundaryAspects = new List <MethodBoundaryAspect>(); var declarations = _aspectDeclarationCollector.CollectAspectDeclarations <MethodBoundaryAspect>(signature); foreach (var aspect in _aspectOrderStrategy.Order(declarations.Select(d => d.MethodAspect))) { var existingAspect = methodBoundaryAspects.Find(aspect.Equals); // Если у текущего аспекта приоритет выше, чем равного тому, // что уже есть в коллекции, то заменяем его на новый if (existingAspect != null && aspect.Order < existingAspect.Order) { methodBoundaryAspects.Remove(existingAspect); } else if (existingAspect == null) { methodBoundaryAspects.Add(aspect); } } for (var i = 0; i < methodBoundaryAspects.Count; i++) { var currentAspect = methodBoundaryAspects[i]; currentAspect.InternalOrder = currentAspect.Order + i + 1; currentAspect.InternalId = Guid.NewGuid(); currentAspect.HasDependencies = _dependencySelector.HasDependencies(currentAspect.GetType()); currentAspect.IsFinalizable = _aspectFinalizer.IsFinalizable(currentAspect); currentAspect.IsInitializable = ReflectedMethod.IsOverriden( MethodAspect.GetInitializeMethod(currentAspect)); } return(methodBoundaryAspects.ToArray()); }
/// <inheritdoc /> public MethodInterceptionAspect CreateInterceptAspect(IInvocationSignature context) { if (_aspectDeclarationCollector == null) { _aspectDeclarationCollector = _aspectDeclarationCollectorHolder.Get(); } if (_aspectFinalizer == null) { _aspectFinalizer = _aspectFinalizerHolder.Get(); } var aspectDeclarations = _aspectDeclarationCollector .CollectAspectDeclarations <MethodInterceptionAspect>(context) .ToArray(); if (aspectDeclarations.Length > 1) { throw new IvorySharpException( $"Допустимо наличие только одного аспекта типа '{typeof(MethodInterceptionAspect)}'. " + $"На методе '{context.Method.Name}' типа '{context.DeclaringType.FullName}' задано несколько."); } if (aspectDeclarations.Length == 0) { return(BypassMethodAspect.Instance); } if (_dependencySelector == null) { _dependencySelector = _dependencySelectorHolder.Get(); } var declaration = aspectDeclarations.Single(); declaration.MethodAspect.MulticastTarget = declaration.MulticastTarget; declaration.MethodAspect.InternalId = Guid.NewGuid(); declaration.MethodAspect.HasDependencies = _dependencySelector.HasDependencies(declaration.MethodAspect.GetType()); declaration.MethodAspect.IsFinalizable = _aspectFinalizer.IsFinalizable(declaration.MethodAspect); declaration.MethodAspect.IsInitializable = ReflectedMethod.IsOverriden( MethodAspect.GetInitializeMethod(declaration.MethodAspect)); return(declaration.MethodAspect); }
/// <inheritdoc /> public bool IsFinalizable(MethodAspect methodAspect) { return(methodAspect is IDisposable); }