Advertisement
Advertisement

More Related Content

Viewers also liked(20)

Advertisement

Similar to Grailsでドメイン駆動設計を実践する時の勘所(20)

Advertisement

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

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

  7. 
 
 

 
 
 

  8. grails {
 exploded = true
 plugins {
 compile project(':core-domain-plugin')
 compile project(':sub-domain-plugin')
 }
 } includeFlat 'core-domain-plugin'
 includeFlat 'sub-domain-plugin'
  9. class IsbnUserType implements UserType {
 
 @Override
 int[] sqlTypes() {
 return [StringType.INSTANCE.sqlType()]
 }
 
 @Override
 Class returnedClass() {
 return Isbn.class
 } }
  10. package com.example.domain class Isbn {
 private String isbnCode
 }
  11. class Book {
 String title
 Isbn isbn
 BigDecimal standardUnitPrice
 
 static mapping = {
 isbn type: IsbnUserType
 } 
 String toString() {
 title
 }
 }
  12. 
 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"
 :
 }

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

  15. 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()
 }
  16. <?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>

  17. package com.example
 
 class BookRepositoryService { 
 List<Book> list() { Book.findAll() } :
 }
  18. package com.example.domain
 
 interface BookRepository { 
 List<Book> list()
 Book get(Long id)
 
 }
  19. 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() }
 :
 }
Advertisement