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

3,109 views

Published on

Spring Day 2016 のスライドです。

Published in: Software
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,109
On SlideShare
0
From Embeds
0
Number of Embeds
2,105
Actions
Shares
0
Downloads
12
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

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.

×