ArchUnitNET icon indicating copy to clipboard operation
ArchUnitNET copied to clipboard

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

Open max-arshinov opened this issue 2 years ago • 1 comments

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?

max-arshinov avatar Feb 24 '23 13:02 max-arshinov

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.

alexanderlinne avatar Aug 28 '24 19:08 alexanderlinne

@max-arshinov since it is already possible to solve this with the existing syntax I'll close this for now. Feel free to reopen if you have any other questions

alexanderlinne avatar Apr 25 '25 12:04 alexanderlinne