这是一个加载的问题,但我会尝试在某种程度上回答它。我强烈建议查看 Vaughn Vernon 的github 示例。他是《实施领域驱动设计》一书的作者。
您提供的 SO 链接中很好地定义了服务的定义。因此,我将只为您提供示例代码来消化该描述。
以在身份访问域中配置租户为例。
以下是 Vaughn 的 github 中的一些非常清晰的示例:
这是租户域对象:
package com.saasovation.identityaccess.domain.model.identity;
public class Tenant extends extends ConcurrencySafeEntit {
public Tenant(TenantId aTenantId, String aName, String aDescription, boolean anActive) {
...
}
public Role provisionRole(String aName, String aDescription) {
...
}
public void activate(){}
public void deactivate(){}
....
}
租户存储库:
package com.saasovation.identityaccess.domain.model.identity;
public interface TenantRepository {
public void add(Tenant aTenant);
}
租户域服务:
package com.saasovation.identityaccess.domain.model.identity;
public class TenantProvisioningService {
private TenantRepository tenantRepository;
public Tenant provisionTenant(
String aTenantName,
String aTenantDescription,
FullName anAdministorName,
EmailAddress anEmailAddress,
PostalAddress aPostalAddress,
Telephone aPrimaryTelephone,
Telephone aSecondaryTelephone) {
Tenant tenant = new Tenant(
this.tenantRepository().nextIdentity(),
aTenantName,
aTenantDescription,
true); // must be active to register admin
this.tenantRepository().add(tenant);
}
}
这是一个应用服务:
package com.saasovation.identityaccess.application;
@Transactional
public class IdentityApplicationService {
@Autowired
private TenantProvisioningService tenantProvisioningService;
@Transactional
public Tenant provisionTenant(ProvisionTenantCommand aCommand) {
return
this.tenantProvisioningService().provisionTenant(
aCommand.getTenantName(),
aCommand.getTenantDescription(),
new FullName(
aCommand.getAdministorFirstName(),
aCommand.getAdministorLastName()),
new EmailAddress(aCommand.getEmailAddress()),
new PostalAddress(
aCommand.getAddressStateProvince(),
aCommand.getAddressCity(),
aCommand.getAddressStateProvince(),
aCommand.getAddressPostalCode(),
aCommand.getAddressCountryCode()),
new Telephone(aCommand.getPrimaryTelephone()),
new Telephone(aCommand.getSecondaryTelephone()));
}
}