Bài đăng

Đang hiển thị bài đăng từ 2021

Builder Pattern trong Java/Javascript

I. Giới thiệu Trong nhiều ngôn ngữ lâp trình thì Constructor giúp chúng ta truyền tham số để tạo 1 object mới. Nhưng vấn đề xảy ra là: - Nếu object thay đổi liên tục (thêm hoặc xóa bớt thuộc tính) thì constructor ta cũng phải đổi lại - object cần nhiều constructor có tham số đầu vào khác nhau Việc này dẫn đến code của chúng ta dễ phải thay đổi liên tục. Để khắc phục nhược điểm này Builder Pattern ra đời Với Builder Pattern thì việc tạo 1 instance object trở nên dễ dàng hơn, ngắn gọn hơn và tùy biến được nhiều hơn 1. Java Ví dụ ta có 1 class Employee như sau: package com.entity; public class Employee { private Long id; private String name; private Long departmentId; // Generate getter and setter } Ví dụ ta muốn tạo 2 Employee: - id = 1, name = Nguyễn Văn A - id = 2, name = Nguyễn Văn B, departmentId = 10 Cách 1: Dùng setter ...

ObjectMapper và HibernateModule

Hình ảnh
I. Giới thiệu Object Mapper là class nằm trong bộ thư viện jackson giúp ta có thể chuyển đổi data trong khi lập trình, copy data từ object này sang object kia một cách dễ dàng mà không phải viết từng dòng set get. Ta có bài toán sau: Giờ ta cần chuyển đổi object Employee (entity) sang EmployeeDto (dto) với đầy đủ các thuộc tính trong entity đang có 1. Entity 1.1. Department package com.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter @Entity @Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "DEPARTMENT") public class Department { @Id @Column(name = "ID") ...

Java Spring JPA Specification

Hình ảnh
I. Giới thiệu Spring JPA Specification chứa một bộ api cho phép ta search, filter trong entity một cách dễ dàng. Trong bài này ta có mối quan hệ giữa Employee và Department như sau Giờ chúng ta sẽ dùng Specification để lấy ra danh sách Employee theo tên phòng ban mà ta muốn tìm II. Sử dụng 1. pom.xml <?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> <groupId>com.specification</groupId> <artifactId>specification-test</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties>...

Java Spring - Spring Cloud Config Client

I. Giới thiệu Java Srping cung cấp tính năng cho phép quản lý file config tập trung thông qua các hệ thống quản lý version như git. Việc deploy và thay đổi config sẽ dễ dàng hơn khi cloud config hỗ trợ lấy config theo môi trường. Spring Cloud Config Clientsẽ gọi api từ Cloud Config Server để lấy config theo môi trường. II. Khởi tạo project Cloud Config Client 1. pom.xml <?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 https://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> <v...

Java Spring - Spring Cloud Config Server

I. Giới thiệu Java Srping cung cấp tính năng cho phép quản lý file config tập trung thông qua các hệ thống quản lý version như git. Việc deploy và thay đổi config sẽ dễ dàng hơn khi cloud config hỗ trợ lấy config theo môi trường. Spring Cloud Config Server đứng giữa control version system (git) và Spring Cloud Config Client (các api cần dùng config). II. Khởi tạo project Cloud Config Server 1. pom.xml <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> <groupId>com.cloudconfig</groupId> <artifactId>cloud-config-server</artifactId> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> ...

Java Spring WebClient

I. Giới thiệu Java Spring trước đó đã cung cấp cho chúng ta cách để gọi các api rest thông qua RestTemplate rất dễ dàng qua vài bước. Nhưng các bản Spring mới về sau thì RestTemplate sẽ không được support nữa, thay vào đó là WebClient. WebClient cung cấp thêm nhiều tính năng mới như Non blocking giúp cho việc chương trình không phải chờ đợi. II. Sử dụng Cách sử dụng trong các project spring: 1. File pom.xml 1.1 webflux (chứa WebClient) <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> 1.2 reactor-netty (chứa thư viện mở rộng reactor netty) <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> </dependency> 1.3 over...

Javascript - Order Pattern

I. Giới thiệu Pattern là một phần quan trọng trong lập trình. Nó sẽ giúp cho việc phát triển các chức năng dễ dàng hơn, dễ mở rộng, tránh việc trùng lặp code. Việc order một list các object trong javascript có thể được thực hiện dễ dàng thông qua order pattern dưới đây. II. Sử dụng 1. Function Order Pattern "use strict"; Order.version = 'Order-1.1 10-03-2021'; Order.ASC = 'ASC'; Order.DESC = 'DESC'; function Order(values){ this.values = values; this.meet = function(propertiesOrder){ if(!propertiesOrder || propertiesOrder.length == 0) return this.values; this.values.sort(function(a, b){ for(var i = 0; i < propertiesOrder.length; i++){ var property = propertiesOrder[i].property; var orderType = propertiesOrder[i].orderType; if(a[property] == b[property]) continue...