Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Grailsでドメイン駆動設計を実践する時の勘所

6,039 views

Published on

Spring Day 2016 のスライドです。

Published in: Software
  • Be the first to comment

Grailsでドメイン駆動設計を実践する時の勘所

  1. 1. 

  2. 2. $ grails create-app helloworld $ cd helloworld $ grails create-domain-class hello $ vi ./grails-app/domain/helloworld/ Hello.groovy
  3. 3. package helloworld class Hello { String message static constraints = { } }
  4. 4. $ grails create-controller hello $ vi ./grails-app/controllers/ helloworld/HelloController.groovy
  5. 5. package helloworld class HelloController { static scaffold = Hello }
  6. 6. $ grails run-app
  7. 7.
  8. 8.
  9. 9.
  10. 10. 
 

  11. 11. 
 
 

 
 
 

  12. 12. grails {
 exploded = true
 plugins {
 compile project(':core-domain-plugin')
 compile project(':sub-domain-plugin')
 }
 } includeFlat 'core-domain-plugin'
 includeFlat 'sub-domain-plugin'
  13. 13.
  14. 14. class IsbnUserType implements UserType {
 
 @Override
 int[] sqlTypes() {
 return [StringType.INSTANCE.sqlType()]
 }
 
 @Override
 Class returnedClass() {
 return Isbn.class
 } }
  15. 15. package com.example.domain class Isbn {
 private String isbnCode
 }
  16. 16. class Book {
 String title
 Isbn isbn
 BigDecimal standardUnitPrice
 
 static mapping = {
 isbn type: IsbnUserType
 } 
 String toString() {
 title
 }
 }
  17. 17. 
 dependencies {
 compile "org.springframework.boot:spring-boot-starter-logging"
 compile "org.springframework.boot:spring-boot-autoconfigure"
 compile "org.grails:grails-core"
 compile "org.springframework.boot:spring-boot-starter-actuator"
 compile "org.springframework.boot:spring-boot-starter-tomcat"
 
 compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1"
 :
 }

  18. 18. package com.example.domain class Isbn {
 private String isbnCode
 }
  19. 19. package com.example.domain
 
 import grails.validation.Validateable
 
 class Book implements Validateable {
 Long id
 String title
 Isbn isbn
 BigDecimal standardUnitPrice Long version
 
 static constraints = {
 }
 }

  20. 20. package com.example.infrastructure
 
 import com.example.domain.Book
 import org.apache.ibatis.annotations.Mapper
 import org.apache.ibatis.annotations.Param
 
 @Mapper
 interface BookMapper {
 void register(@Param("book") Book book)
 List<Book> list()
 }
  21. 21. <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.example.infrastructure.BookMapper">
 
 <resultMap id="product" type="com.example.domain.Book">
 <id property="id" column="id"/>
 <result property=“isbn.isbnCode" column="isbn_code"/>
 <result property="title" column=“title"/> <result property="standardUnitPrice" column=“standard_unit_price”/>
 <result property="version" column="version"/>
 </resultMap>
 
 :
 </mapper>

  22. 22. package com.example
 
 class BookRepositoryService { 
 List<Book> list() { Book.findAll() } :
 }
  23. 23. package com.example.domain
 
 interface BookRepository { 
 List<Book> list()
 Book get(Long id)
 
 }
  24. 24. package com.example.infrastructure
 
 import com.example.domain.Book
 import com.example.domain.BookRepository
 import org.springframework.stereotype.Repository
 
 @Repository
 class BookDatasource implements BookRepository {
 
 @Override List<Book> list() { Book.findAll() }
 :
 }
  25. 25.

×