TNG / ArchUnitNET

A C# architecture test library to specify and assert architecture rules in C# for automated testing.
Apache License 2.0
933 stars 61 forks source link

TypeDoesNotExistInArchitecture: When using 'ImplementInterface' #140

Closed Der-Kraken closed 2 years ago

Der-Kraken commented 2 years ago

Hi,

using the method ImplementInterface(...) throws a TypeDoesNotExistInArchitecture if no type was found in target architecture. If there is at least 1 type existing then that exception is not thrown.

ArchRuleDefinition.Classes()
    .That().ImplementInterface(typeof(IAnyInterface))
    .GetObjects(Architecture);

Curiously, if I use name matching then no exception is thrown:

ArchRuleDefinition.Classes()
    .That().HaveNameEndingWith("yyyyy")
    .GetObjects(Architecture);

Is that an expected behavior? I don't care if my users do not implement that interface, but if they do then I will have some rules for them.

brandhuf commented 2 years ago

Hi,

the error is thrown because first the reference to the type is being resolved and then the implemented interfaces are compared to that reference. If the referenced type is not in the architecture, it can't be resolved an an error is thrown. When you're matching by string you don't get that problem. I can see how this behaviour can be unexpected, especially in your first example. so we might change that in a future release. For now I would only use typeof(...) referencing with types who are definetly in your architecture.