public IEnumerable<Violation> Verify(VerificationContext context, Node node)
 {
     if(this.unusedCommands.Contains((CommandNode)node))
     {
         yield return new UnusedCommandViolation((CommandNode) node);
     }
 }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var methodNode = (MethodNode)node;

            var forbiddenMethods = new[]
            {
                typeof (DateTime).GetProperty("Now").GetMethod,
                typeof (DateTime).GetProperty("UtcNow").GetMethod,
                typeof (DateTimeOffset).GetProperty("Now").GetMethod,
                typeof (DateTimeOffset).GetProperty("UtcNow").GetMethod,
            };

            var violatingInstructions = from instruction in methodNode.Method.GetInstructions()
                where instruction.IsCall()
                let callee = (MethodBase) instruction.Operand
                where forbiddenMethods.Contains(callee)
                select instruction;

            foreach (var violatingInstruction in violatingInstructions)
            {
                var sourceLocation = FindSourceLocation(violatingInstruction, methodNode);

                yield return new UsesDateTimeNowViolation(node, sourceLocation);
            }
        }
        public void Initialize(VerificationContext context, Graph graph)
        {
            this.aggregateForMethod = new Dictionary<MethodNode, AggregateNode>();

            var bfs = new LambdaBreadthFirstSearch<Node, Link>
            {
                AvailableTargets = n => n.OutboundLinks
                    .Where(l => l is HasOneEntityLink || l is HasManyEntityLink)
                    .GroupBy(x => x.Target)
                    .Union(n.InboundLinks.Where(l => l is ContainedInLink && l.Target is EntityNode && l.Source is MethodNode).GroupBy(x => x.Source))
            };

            var aggregates = graph.Nodes.OfType<AggregateNode>();

            foreach (var node in aggregates)
            {
                bfs.HandlingNode = (n, _) =>
                {
                    var methodNode = n as MethodNode;
                    if (methodNode != null && !methodNode.Method.IsSpecialName)
                    {
                        this.aggregateForMethod[methodNode] = node;
                    }
                };

                bfs.Walk(node);
            }
        }
 public IEnumerable<Violation> Verify(VerificationContext context, Node node)
 {
     foreach (var queryExecutionLink in node.OutboundLinks.OfType<QueryExecutionLink>().GroupBy(x => (QueryNode)x.Target).Select(x => x.Key))
     {
         yield return new CommandHandlerExecutesQueryViolation((CommandHandlerNode) node, (QueryNode) queryExecutionLink);
     }
 }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var v = new PathVerify();

            v.Walk(node);

            return v.Violations;
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var typeNode = (TypeNode)node;

            return VerifyNotSettingField(context, typeNode)
                .Union(VerifyNotSettingProperty(context, typeNode))
                .Union(VerifyNoNotPrivatePropertySetters(context, typeNode))
                .Union(VerifyNoWritableFields(context, typeNode))
                ;
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var containers = node.InboundLinks.OfType<ReferenceLink>().Select(x => x.Source).Distinct();
            var backReferences = containers.SelectMany(x => x.OutboundLinks).OfType<ReferenceLink>()
                .Where(x => x.Target == node);

            foreach (var backReference in backReferences)
            {
                yield return new BidirectionalReferenceViolation(backReference.Source, backReference.Target);
            }
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var property = (PropertyNode) node;

            var referencedAggregate = this.aggregates.FirstOrDefault(x => x.Type == property.Property.PropertyType);

            if (referencedAggregate != null)
            {
                yield return new DirectAggregateReferenceViolation(node, referencedAggregate);
            }
        }
        private IEnumerable<Violation> VerifyNoWritableFields(VerificationContext context, TypeNode typeNode)
        {
            var violatingFields = from field in typeNode.InboundFrom<FieldNode, ContainedInLink>()
                                  where !field.Field.IsInitOnly
                                  select field;

            foreach (var field in violatingFields)
            {
               yield return new ImmutableTypeHasWritableFieldViolation(typeNode, field);
            }
        }
        private IEnumerable<Violation> VerifyNotSettingProperty(VerificationContext context, TypeNode typeNode)
        {
            var violatingMethods = from method in typeNode.InboundFrom<MethodNode, ContainedInLink>()
                                   where method.OutboundLinks.OfType<SetPropertyLink>().Any()
                                   select method;

            foreach (var method in violatingMethods)
            {
                yield return new ImmutableTypeSetsPropertyOutsideOfConstructorViolation(typeNode, method);
            }
        }
        private IEnumerable<Violation> VerifyNoNotPrivatePropertySetters(VerificationContext context, TypeNode typeNode)
        {
            var violatingProperties = from property in typeNode.InboundFrom<PropertyNode, ContainedInLink>()
                                      where property.Property.CanWrite && !property.Property.SetMethod.IsPrivate
                                      select property;

            foreach (var property in violatingProperties)
            {
                yield return new ImmutableTypeHasNonPrivateSetterViolation(typeNode, property);
            }
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var count = node.Annotation<CommandExecutionCount>();

            if (count == null || count.HighestCount <= 1)
            {
                yield break;
            }

            yield return new MethodExecutesMoreThanOneCommandViolation(node);
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Node node)
        {
            var calledMethods = node.OutboundLinks.OfType<MethodCallLink>().Select(x => (MethodNode)x.Target);

            foreach (var calledMethod in calledMethods)
            {
                if (IsInvalidCall((MethodNode)node, calledMethod))
                {
                    yield return new DoNotCallEntityMethodsFromOutsideOfAggregateViolation((MethodNode)node, calledMethod);
                }
            }
        }
        public IEnumerable<Violation> Verify(VerificationContext context, Graph graph)
        {
            var finder = new FindCycles<Node, Link>()
            {
                NodeFilter = x => x is TypeNode,
                OutboundTargets = x => x.OutboundLinks.OfType<DependencyLink>().GroupBy(y => y.Target)
            };

            var cycles = finder.Find(graph);

            foreach (var cycle in cycles)
            {
                yield return new CycleInDependenciesViolation(cycle.OfType<TypeNode>());
            }
        }
        public void Initialize(VerificationContext context, Graph graph)
        {
            var entryPoint = graph.LookupNode<ApplicationEntryPoint>(ApplicationEntryPoint.NodeId);

            this.unusedCommands = new HashSet<CommandNode>(graph.Nodes.OfType<CommandNode>());

            var bfs = new LambdaBreadthFirstSearch<Node, Link>
            {
                HandlingNode = (node, links) =>
                {
                    var commandNode = node as CommandNode;
                    if (commandNode != null && links.OfType<ExecuteCommandLink>().Any())
                    {
                        this.unusedCommands.Remove(commandNode);
                    }
                }
            };

            bfs.Walk(entryPoint);
        }
Example #16
0
        private void RunRules()
        {
            this.verificator = new Verificator();
            this.verificator.RegisterConventionsFrom(this.conventionAssemblies.ToArray());

            this.verificator.StartingRule += (s, e) =>
            {
                Log.Debug("Starting rule {0}", e.Rule.GetType().Name);
                this.Report.VerificationRule(e.Rule);
            };

            this.verificator.NodeVerified += (s, e) =>
            {
                this.Report.NodeVerification(e.Rule, e.Node, e.Violations);
            };

            this.verificator.GraphVerified += (s, e) =>
            {
                this.Report.GraphVerification(e.Rule, e.Violations);
            };

            this.verificator.FinishedRule += (s, e) => Log.Info("Finished rule {0}", e.Rule.GetType().Name);

            this.verificationContext = new VerificationContext();

            foreach (var rule in this.runlist.Elements.Where(x => x.IsRule))
            {
                this.verificator.AddRule(rule.Type);
            }

            try
            {
                this.verificator.Verify(this.verificationContext, this.modelBuilder);
            }
            catch (Exception e)
            {
                Log.Fatal("Error running rules", e);
            }

            ReportViolations(this.verificationContext.Violations);
        }
 public void Initialize(VerificationContext context, Graph graph)
 {
     this.aggregates = graph.Nodes.OfType<AggregateNode>().ToList();
 }