TNG / ArchUnitNET

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

Restrict Access of a Type/Layer to Certain Types/Layers #209

Closed mneundorfer closed 1 year ago

mneundorfer commented 1 year ago

From the Java ArchUnit implementation, I am aware of the onlyBeAccessed rule, which allows to define a set of types/classes which can have exclusive access to another one.

So far, I did not find a counterpart in ArchUnit.NET. As far as I could see there only exists ShouldOnlyUse, but no negating ShouldOnlyBeUsedBy. Is this something that just is not possible with ArchUnit.NET at the moment, or did I just not stumble across the right solution so far...?

Thanks for any hints!

MSinstein commented 1 year ago

Hi @mneundorfer ,

I also don't find a method corresponding to onlyBeAccessed. Would a rule like Types().That().DependOnAny(TargetLayer).And().AreNot(TargetLayer).Should().Be(AccessLayer) work for your use case?

Disclaimer: I haven't tested the rule, please verify that it actually does what you need

mneundorfer commented 1 year ago

Hi @MSinstein ,

Thanks for the feedback - apparently, however, it does not work as (I would) expected. Given your example, if I have an AbcLayer also referencing the TargetLayer, the tests still run green.

mneundorfer commented 1 year ago

... works as expected, thanks for the hint! For anyone wondering why I thought it was not working in the first place - don't forget to call the .Check() afterwards... 🤦