commiting code for hexagonal archi(eval article)

This commit is contained in:
rvsathe 2021-03-14 13:58:13 +05:30
parent f58867dedf
commit da32163216
9 changed files with 260 additions and 0 deletions

47
hexagonal-example/pom.xml Normal file
View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>HexagonalLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HexagonalLibrary</name>
<description>HexagonalLibrary</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.HexagonalLibrary</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HexagonalLibrary {
public static void main(String[] args) {
SpringApplication.run(HexagonalLibrary.class, args);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.hexagonalPattern.adapter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Repository;
import com.baeldung.hexagonalPattern.core.domain.Book;
import com.baeldung.hexagonalPattern.ports.LibraryRepo;
@Repository
public class LibraryRepoImpl implements LibraryRepo {
// This class is the actual implementation of the out bound port/adapter.
private HashMap<String, Book> books = new HashMap<String, Book>();
@Override
public void insertBook(Book book) {
// Mock Database call here.
books.put("mock", new Book("mock", "mock", "mock"));
}
@Override
public Book searchBook(String name) {
// TODO Auto-generated method stub
Book b = new Book();
// Some code for retrieval of book from db
return b;
}
@Override
public List<Book> getAllBooks() {
// Fetch all books from db
return books.values().stream().collect(Collectors.toList());
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.hexagonalPattern.adapter;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.hexagonalPattern.core.domain.Book;
import com.baeldung.hexagonalPattern.web.LibraryRestUI;
@RestController
public class LibraryRestController implements LibraryRestUI {
@Override
@RequestMapping("/library")
public void insertBook(Book book) {
// TODO Auto-generated method stub
}
@Override
@GetMapping("/searchBook")
public Book searchBook(@PathVariable String name) {
// TODO Auto-generated method stub
return null;
}
@Override
@GetMapping("/listBooks")
public List<Book> listAllBooks() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.hexagonalPattern.core.domain;
public class Book {
private String name;
private String author_list;
private String isbn13;
/* constructors and getter and setters */
public Book(String string, String string2, String string3) {
// TODO Auto-generated constructor stub
}
public Book() {
// TODO Auto-generated constructor stub
}
/*
* public String getName() { return name; }
*
* public void setName(String name) { this.name = name; }
*
* public String getAuthor_list() { return author_list; }
*
* public void setAuthor_list(String author_list) { this.author_list =
* author_list; }
*
* public String getIsbn13() { return isbn13; }
*
* public void setIsbn13(String isbn13) { this.isbn13 = isbn13; }
*/
}

View File

@ -0,0 +1,38 @@
package com.baeldung.hexagonalPattern.core.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.hexagonalPattern.core.domain.Book;
import com.baeldung.hexagonalPattern.ports.LibraryRepo;
import com.baeldung.hexagonalPattern.ports.LibraryService;
@Service
public class LibraryServiceImpl implements LibraryService {
// This is the class which actually implements the methods from the ports.
// The ports are just for exposing the methods to the outside.
@Autowired
private LibraryRepo bookRepo;
@Override
public void insertBook(Book book) {
// TODO some implementation to insert record in the db or similar
bookRepo.insertBook(book);
}
@Override
public Book lendBook(String name) {
// TODO Auto-generated method stub
return bookRepo.searchBook(name);
}
@Override
public List<Book> getAllBooks() {
// TODO Auto-generated method
return null;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.hexagonalPattern.ports;
import java.util.List;
import com.baeldung.hexagonalPattern.core.domain.Book;
public interface LibraryRepo {
// Outbound Port.
public void insertBook(Book book);
public Book searchBook(String name);
public List<Book> getAllBooks();
}

View File

@ -0,0 +1,15 @@
package com.baeldung.hexagonalPattern.ports;
import java.util.List;
import com.baeldung.hexagonalPattern.core.domain.Book;
public interface LibraryService {
//This is the in bound port.exposes the application to the world.
public void insertBook(Book book);
public Book lendBook(String name);
List<Book> getAllBooks();
}

View File

@ -0,0 +1,24 @@
package com.baeldung.hexagonalPattern.web;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baeldung.hexagonalPattern.core.domain.Book;
public interface LibraryRestUI {
// This is the in bound Adapter
@PostMapping
void insertBook(@RequestBody Book book);
@GetMapping("/{name}")
public Book searchBook(@PathVariable String name);
@GetMapping
public List<Book> listAllBooks();
}