BenMorris / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.
MIT License
1.36k stars 81 forks source link

Help with dependency verification #127

Open freynetraul opened 11 months ago

freynetraul commented 11 months ago

Hello, I have been trying to get the library to check the following:

example: I have the layers

Application, Core, Infrastructure

I need the application layer to depend solely and exclusively on Core, I have tried all of this:

var result2 = Types.InCurrentDomain().That().ResideInNamespace("Application") .Should().OnlyHaveDependenciesOn("Core").GetResult();

var result3 = Types.InCurrentDomain().That().ResideInNamespace("Application") .Should().HaveDependencyOnAny("Core").GetResult();

var result5 = Types.InCurrentDomain().That().AreClasses().And().ResideInNamespace("Application") .ShouldNot().HaveDependenciesOtherThan("Core").GetResult();

but it doesn't work, because in the first case it will throw an error because there are types that do not depend exclusively on Core in Application, in the second and third also it will throw an error, because there are types that do not depend on Core and that's fine, it's really what I want, verify that Application depends solely and exclusively on Core, ignoring those types that do not depend on anything.

NeVeSpl commented 11 months ago

Just do not forget about adding .net "System" namespace, and try this:

var result2 = Types.InCurrentDomain().That().ResideInNamespace("Application")
.Should().OnlyHaveDependenciesOn("Core", "System").GetResult();

or

var result5 = Types.InCurrentDomain().That().AreClasses().And().ResideInNamespace("Application")
.ShouldNot().HaveDependenciesOtherThan("Core", "System").GetResult();
freynetraul13 commented 11 months ago

Just do not forget about adding .net "System" namespace, and try this:

var result2 = Types.InCurrentDomain().That().ResideInNamespace("Application")
.Should().OnlyHaveDependenciesOn("Core", "System").GetResult();

or

var result5 = Types.InCurrentDomain().That().AreClasses().And().ResideInNamespace("Application")
.ShouldNot().HaveDependenciesOtherThan("Core", "System").GetResult();

Ok, let's say I map all those dependencies explicitly on which I exclusively want to depend, what about all the new nugets that come out in the development process, would I have to be editing this line of code every time?

NeVeSpl commented 11 months ago

There is no magic, how the library is supposed to know which dependency is good and which one is bad? You have two options:

Usually, the one that is shorter and more stable is a better choice.