Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Iddd domain service

179 visualizaciones

Publicado el

Introducing the domain service concept in "Implementing Domain-Driven Design"

Publicado en: Software
  • Inicia sesión para ver los comentarios

Iddd domain service

  1. 1. SERVICES IDDD CHAPTER 7
  2. 2. THREE MAIN ROLES IN DOMAIN ENTITY VALUE OBJECT DOMAIN SERVICE Unique Identity Stateful Immutable Conceptual whole Replaceability Side-effect free Stateless Domain object interaction Complex process
  3. 3. SOMETIMES, IT JUST ISN’T A THING - ERIC EVANS DOMAIN SERVICE ▸ A STATELESS operation that fulfills a domain-specific task ▸ An operation is not easy to be defined on an Aggregate(Entity) or a Value Object ▸ Make sure the operation name is a part of the UBIQUITOUS LANGUAGE ▸ The only role can invoke repositories in the domain
  4. 4. LET’S SEE AN EXAMPLE
  5. 5. PRODUCT 1..* BACKLOG ITEM public class Product { private Set<BacklogItem> backlogItems public BusinessPriorityTotals businessPriorityTotals() { // Iterating backlogItems and calculating priority totals } } SO FAR SO GOOD
  6. 6. WE CHANGED THE MODEL PRODUCT PRODUCT ID BACKLOG ITEM
  7. 7. public class { public static BusinessPriorityTotals businessPriorityTotals( Set<BacklogItem> backlogItems ) { // Iterating backlogItems and calculating priority totals } }
  8. 8. IS THAT THE BEST CHOICE ?
  9. 9. PRODUCT OR BACKLOG ITEM ?
  10. 10. DOMAIN SERVICE
  11. 11. WHAT IS NOT A DOMAIN SERVICE ▸ Different from application services ▸ Not required to be a remote-capable(SOA, RPC, MoM) Application Service Domain Service
  12. 12. REASONS OF USING DOMAIN SERVICES ▸ Perform a significant business process ▸ Calculate a Value requiring input from more than one domain object ▸ Transform a domain object from one composition to another
  13. 13. PERFORM A SIGNIFICANT BUSINESS PROCESS
  14. 14. IDENTITY AND ACCESS CONTEXT ▸ Users of a system must be authenticated but can be authenticated only if the tenant is active ▸ Passwords must be stored encrypted, not as clear text
  15. 15. boolean authentic = false; Tenant tenant = DomainRegistry.tenantRepository().tenantOfId(aTenantId); if (tenant == null || !tenant.isActive()) { return authentic; } User user = DomainRegistry .tenantRepository() .userWithUsername(aTenantId, aUsername); if (user == null) { return authentic; } authentic = tenant.authenticate(user, aPassword); return authentic;
  16. 16. boolean authentic = false; Tenant tenant = DomainRegistry.tenantRepository().tenantOfId(aTenantId); if (tenant == null || !tenant.isActive()) { return authentic; } User user = DomainRegistry .tenantRepository() .userWithUsername(aTenantId, aUsername); if (user == null) { return authentic; } authentic = tenant.authenticate(user, aPassword); return authentic; Tenant knowing how to encrypt the password Domain logic was leaked to the client
  17. 17. if { … } else { … } Application Layer for … x + y ENCAPSULATE COMPLEX LOGICS IN DOMAIN SERVICE
  18. 18. USING A DOMAIN SERVICE UserDescriptor userDescriptor = DomainRegistry .authenticationService() .authenticate(aTenantId, aUsername, aPassword); YOU CAN RETURN WHAT YOU NEED INSTEAD OF A WHOLE USER FIT OUR UBIQUITOUS LANGUAGE
  19. 19. SEPARATED INTERFACE
  20. 20. Application
 Service Domain Service Domain Layer Application Layer
  21. 21. <interface>
 DomainService Application
 Service Domain Service Implementation Infrastructure Layer Domain Layer Application Layer extends
  22. 22. package saasovation.identityaccess.infrastructure.services; import saasovation.identityaccess.domain.model.identity.AuthenticationService; import … public class DefaultEncryptionAuthenticationService implement AuthenticationService { public UserDescriptor authenticate(TenantId tenantId, String username, String password) { // Checking null value for all of the properties UserDescriptor userDescriptor = null; if (tenant == null || !tenant.isActive()) { return userDescriptor; } String encryptedPassword = DomainRegistry.encryptionService().encryptedValue(password); User user = DomainRegistry.userRepository().userFromAuthenticCredentials( tenantId, username, encryptedPassword ); if (user == null || !user.isEnabled()) { return userDescriptor; } return userDescriptor; } }
  23. 23. IS SEPARATED INTERFACE A NECESSITY ? ▸ Considering the name of your interface
 (ex: DefaultService, AuthenticationServiceImp) ▸ The service in the domain will have multiple implementation ▸ You can still prevent the client from being aware of the implementation by using DI or a Factory
  24. 24. A CALCULATION PROCESS
  25. 25. package saasovation.agile.domain.model.product; import … public class BusinessPriorityCalculator { public BusinessPriorityTotals businessPriorityTotals(Tenant tenant, ProductId productId) { int totalBenefit = 0; int totalPenalty = 0; int totalCost = 0; int totalRisk = 0; Collection<BacklogItem> outstandingBacklogItems = DomainRegistry .backlogItemRepository() .allOutstandingProductBacklogItems(tenant, productId); for (BacklogItem backlogItem: outstandingBacklogItems) { if (backlogItem.hasBusinessPriority()) { BusinessPriorityRatings ratings = backlogItem.businessPriority().ratings(); totalBenefit += ratings.benefit(); totalPenalty += ratings.penalty(); totalCost += ratings.cost(); totalRisk += ratings.risk(); } } BusinessPriorityTotals businessPriorityTotals = new BusinessPriorityTotals( … totalBenefit + totalPenalty ); return businessPriorityTotals; } }
  26. 26. TRANSFORMATION SERVICE
  27. 27. saasovation.identityaccess saasovation.collaboration
  28. 28. <interface>
 CollaboratorService TranslatingCollaborator Service UserInRoleAdapter CollaboratorTranslator extends saasovation.collaboration.infrastructure.services saasovation.collaboration.domain.model.services
  29. 29. MAKE SURE YOU NEED A SERVICE ▸ Do not treat Domain Services as a silver bullet ▸ Using Services overzealously will usually result in the negative consequences of creating an Anemic Domain Model
  30. 30. SWITCH BETWEEN DIFFERENT KINDS OF DEVELOPER TESTING Client Developer Domain Developer
  31. 31. THANK YOU FOR COMING HERE !

×