Esempio n. 1
0
        /// <inheritdoc/>
        public void AddAspect(string aspectName, IAspect aspect, IReadOnlyDictionary <JoinPoint, int> sequenceDict)
        {
#if DEBUG
            if (string.IsNullOrWhiteSpace(aspectName))
            {
                throw new ArgumentNullException("aspectName");
            }

            if (aspect == null)
            {
                throw new ArgumentNullException("aspect");
            }

            if (sequenceDict == null || !sequenceDict.Any())
            {
                throw new ArgumentNullException("sequenceDict");
            }
#endif
            if (aspects.ContainsKey(aspectName))
            {
                throw new ArgumentException($"aspect with id {aspectName} already exists.", "aspectName");
            }

            var pointCut = aspect.PointCut;
            if (pointCut == null || !pointCut.Any())
            {
                throw new ArgumentException($"aspect {aspectName} doesn't contain any join point", "aspect");
            }

            if (!JoinPointCollectionComparer.PointCutEquals(pointCut, sequenceDict.Keys.ToList().AsReadOnly()))
            {
                throw new ArgumentException($"Sequence dictionary doesn't match join points of aspect {aspectName}", "sequenceDict");
            }

            foreach (var sequenceItem in sequenceDict)
            {
                var key = sequenceItem.Key;
                if (joinPoints.ContainsKey(key) && joinPoints[key].Values.Any(i => i.Sequence == sequenceItem.Value))
                {
                    throw new ArgumentException($"Sequence {sequenceItem.Value} of join point {key} has been occupied already.", "sequenceDict");
                }
            }

            readOnly.Assert(false);
            var joinPointDict = new Dictionary <JoinPoint, JoinPointInfo>();
            foreach (var joinPoint in pointCut)
            {
                var flag = AdviceValidator.Validate(joinPoint, aspect.GetAdvice(joinPoint));
                joinPointDict.Add(joinPoint, new JoinPointInfo {
                    Sequence = sequenceDict[joinPoint], ParameterFlag = flag
                });
            }

            aspects.Add(aspectName, aspect);

            foreach (var joinPoint in pointCut)
            {
                if (!joinPoints.ContainsKey(joinPoint))
                {
                    joinPoints.Add(joinPoint, new Dictionary <string, JoinPointInfo>());
                }

                joinPoints[joinPoint].Add(aspectName, joinPointDict[joinPoint]);
            }
        }