2

I have a solution with several API projects in various namespaces. I'm looking for a way to validate all the API projects at the same time, to check if any of them has a class (controller) with the same name as another class in any of those projects.

Let's say I have the following classes:

Api1.DogController
Api1.CatController

Api2.BirdController

Now somebody adds a new CatController to Api2 so we have

Api2.BirdController
Api2.CatController

I would like PostShart to throw a build error, because a class called CatController already exists in the Api1 project. And I'd prefer to avoid the need to add any attributes to classes that are supposed to be checked. I'm OK with referencing the constraint in AssemblyInfo file of API projects though.

I tried doing some stuff with ScalarConstraint and Types, like so:

[MulticastAttributeUsage(MulticastTargets.Class)]
    public class ControllerNamesValidation : ScalarConstraint
    {
        public override void ValidateCode(object target)
        {
            var targetType = (Type)target;

            //various experiments with targetType.GetMembers() or targetType.GetNestedTypes() here...
        }
     }

but failed.

  • This should be done via your unit tests, not at runtime ! – Neil Jan 30 '20 at 15:58
  • Why are the APIs in the same repo? Why not separate them? Why do you suspect engineers to create duplicate names? – Judy007 Jan 31 '20 at 15:34
  • @joey It's a big system divided into a couple of dozen modules, many of them very old (written in Classic ASP) and glued together with more modern codebase. Some architectural decisions were made in the past and it is what it is. Duplicate controller names are possible due to sheer volume of code and some functionalities of separate modules being similar to each other. – Witold Litwin Feb 02 '20 at 09:35
  • Sounds like your concern of duplication is valid, especially how large the code base is. I’m not sure if creating a validation script is worthwhile, instead it might make more sense to address the monolith issue. The more validation you put in, the more side effects of the build, and there may be cases where duplicate name makes sense, and this breaks down, – Judy007 Feb 03 '20 at 16:41

1 Answers1

2

PostSharp processes each project separately. It is possible to use ScalarConstraint to enforce your rule solution-wide at build time, but you would need to store the information about used controller names somewhere (e.g. the file system or the registry). This requires synchronization, which might add time to your compilation.

If all of your API libraries are referenced by a single project, you might also achieve the same effect by targeting Assembly instead of Class, and then reflecting on the assembly in the constraint logic (see Assembly.GetReferencedAssemblies). You can filter the processed assemblies by the presence of the custom attribute.

  • I accepted this answer as it answers my question directly, and I apprecieate that, however I think that I'll go the unit tests route. Thanks again. – Witold Litwin Feb 05 '20 at 09:55