1 of 59

2 of 59

3 of 59

4 of 59

5 of 59

6 of 59

eCommerce application from past 15 years

7 of 59

Native

= พื้นเมือง

Cloud Native

= “คลาวไทยบ้าน

= ไทยบ้าน ในภาษาอีสาน

8 of 59

Cloud Native

9 of 59

eCommerce application adopting a cloud-native approach

10 of 59

“ There was a lot of techniques/configuration ”

11 of 59

“ Provide minimal components for resiliency defaults, health checks, telemetry, and integrate with DI ”

12 of 59

Install .NET Aspire

To work with .NET Aspire, you'll need the following installed locally:

13 of 59

14 of 59

15 of 59

16 of 59

17 of 59

18 of 59

.NET Aspire Component

Components are glue libraries that configure an SDK to operate in a cloud environment

  • Leverage configurable resilience patterns such as retries, timeouts, and circuit breakers to maximize availability.
  • Expose health checks enabling applications to track and respond to the remote service’s health.
  • Offer integrated logging, metrics, and tracing using modern .NET abstractions (ILogger, Meter, Activity).
  • Offer extension methods that ‘glue’ the services from the SDK to the DI container with the right lifetime for the types being registered.

19 of 59

dotnet add package Aspire.RabbitMQ.Client --prerelease

+Host Project: var rabbit = builder.AddRabbitMQContainer("rabbit");

+ServiceDefaults Project

Aspire.RabbitMQ.Client

Common service-centric logic

that applies across all the projects

Microsoft.Extensions.ServiceDiscovery

Healthcheck

Logging

20 of 59

.NET Aspire References

21 of 59

เห็นคำว่า Clean แล้วนึกถึงอะไร

22 of 59

23 of 59

Robert C. Martin (Uncle Bob)

24 of 59

Clean Code

“เน้นไปที่รายละเอียดของโค้ด

ที่ทำความเข้าใจง่าย ดูแลรักษาง่าย”

Clean Architecture

เน้นไปที่การออกแบบระบบโดยรวม

ที่เน้นความยืดหยุ่น รองรับการเปลี่ยนแปลง ทดสอบง่าย”

ทั้งสองล้วนจำเป็นในการสร้างซอฟต์แวร์คุณภาพและบำรุงรักษาได้ดีในระยะยาว

25 of 59

Clean Code

26 of 59

27 of 59

28 of 59

Traditional "N-Layer" architecture applications

29 of 59

มันก็ทำงานได้นะ แล้วทำไมเราต้องมี �Clean Architecture

30 of 59

พี่ขอเปลี่ยนฐานข้อมูล

จาก MySQL

เป็น MSSQL

น่าจะใช้เวลาแก้ไม่นาน

ใช่ไหมครับ

ลูกค้า

ผู้น่าร้ากกก

Developer ท่านนึง

31 of 59

32 of 59

“Dividing the software into layers for separation of concerns”

  • Independent of Frameworks
  • Testable
  • Independent of UI
  • Independent of Database
  • Independent of any external agency

33 of 59

Clean Architecture - Uncle Bob

34 of 59

35 of 59

36 of 59

37 of 59

ยังไม่เสร็จ T_T

38 of 59

39 of 59

40 of 59

Dependency Non-Injection

“New is Glue.”

41 of 59

Interface

42 of 59

  • Dependency Inversion Principle:คือ Software Design Principle ที่แนะนำชี้ให้เห็นถึงปัญหาเกี่ยวกับเรื่องของ Dependency แต่ไม่ได้บอกว่าใช้เทคนิคอะไรในการแก้ไขปัญหา
  • Inversion of Control (IoC): คือการนำ Dependency Inversion Principle มากำหนดแนวทางการพัฒนาให้ Components อ้างอิงกับ Abstraction แทนการอ้างอิงกับ Concrete Implementation.
  • Dependency Injection (DI): คือ Design Patternในการนำ IoC มาพัฒนา โดยจะทำการ Inject Concrete Implementation ระหว่าง Components
  • IoC Container (DI Container): คือ Programming Framework ที่ช่วยทำ DI ให้โดยอัฒโนมัติ ซึ่งใน ASP.NET Core นั้นมี Build-In IoC Container (DI Container) ให้เราพร้อมใช้ได้อย่างรวดเร็วเลยครับ ช่วยลดขั้นตอน ลด Code ในการพัฒนา DI ลงไปได้เยอะมาก เพียงแต่เราจะต้องเรียนรู้การจัดการ State ของ Components ในขั้นตอนนี้ที่เรียกว่า Service Lifetimes

43 of 59

Dependency

Injection (DI)

44 of 59

IoC Container (DI Container):

1.Transient Instance จะถูกสร้างใหม่ทุกครั้งที่มีการเรียกใช้งานภายในระบบ

เหมาะสำหรับการทำงานที่ Lightweight, Stateless

2. Scoped Instance จะถูกสร้างใหม่ทุกครั้งที่ Client Request เข้ามานะครับ (1 Connection = 1 Client Request)

เหมาะสำหรับการทำงานที่ต้องการเรื่องของ Stateful เช่น Entity Framework (AddDbContext)

3. Singleton Instance จะถูกสร้างแค่ครั้งแรกที่ถูกเรียก หลังจากนั้น Instance จะคงอยู่ตลอดไปจนกว่าเราจะปิดและเปิดระบบขึ้นมาใหม่ เหมาะสำหรับบางงานที่ต้องการ Reuse Instance เดิมอยู่ตลอด เช่น Logging, Caching

45 of 59

46 of 59

Project

Domain

47 of 59

Project

Core

48 of 59

Project

Infra

49 of 59

Project API

50 of 59

51 of 59

52 of 59

53 of 59

54 of 59

55 of 59

56 of 59

57 of 59

58 of 59

59 of 59