TNG / ArchUnitNET

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

How do I check the "App Services / Domain Services" rule? #205

Open max-arshinov opened 1 year ago

max-arshinov commented 1 year ago

image

I want to make sure that Application Services don't call other Application Services and call DomainServices instead.

private IObjectProvider<Class> AppServices = Classes().That().HaveName(@"\S+AppService", true);

//...

Classes().That().Are(AppServices).Should().NotCallAny(
  MethodMembers().That().AreDeclaredIn(AppServices)
);

However, code like this violates this rule

public class MyAppService
{
    public void Smth(){}

    public void SmthElse()
    {
      Smth();
    }
}

Is there a way to exclude "self" from the rule? If not, is there any other workaround?

alexanderlinne commented 1 month ago

Hello @max-arshinov, a workaround at this time would be:

var AppServices = Classes().That().HaveName(@"\S+AppService", true);
foreach (var appService in AppServices.GetObjects(ARCHITECTURE))
{
    Classes()
        .That()
        .Are(AppServices)
        .And()
        .AreNot(appService)
        .Should()
        .NotCallAny(MethodMembers().That().AreDeclaredIn(appService))
        .Check(ARCHITECTURE);
}

or similarly:

var AppServices = Classes().That().HaveName(@"\S+AppService", true);
foreach (var appService in AppServices.GetObjects(ARCHITECTURE))
{
    Classes()
        .That()
        .Are(appService)
        .Should()
        .NotCallAny(
            MethodMembers()
                .That()
                .AreDeclaredIn(AppServices)
                .And()
                .AreNotDeclaredIn(appService)
        )
        .Check(ARCHITECTURE);
}

We have a similar case in #229 where self references lead to such a problem and are working on a solution.