Skip to content

Commit 3aef7ce

Browse files
committed
docs(readme): update documentation
1 parent 60a3aba commit 3aef7ce

2 files changed

Lines changed: 190 additions & 20 deletions

File tree

README.md

Lines changed: 190 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,15 @@ $ yarn add types-ddd
6060
```
6161
---
6262

63-
## Lib Documentation
63+
## Lib Full Documentation
6464

6565
Check lib documentation on link [Here](https://github.com/4lessandrodev/types-ddd/tree/main/docs)
6666

6767
---
6868

69-
<img src="./readme/ddd.jpg" alt="image" width="100%">
69+
<img src="./readme/cover.png" alt="image" width="100%">
7070

71-
## DDD (Domain Driven Design)
72-
73-
- [ 1. Ubiquitous language](#1-ubiquitous-language)
74-
- [ 2. Rich domain model](#2-rich-domain-model)
75-
- [ 3. Thin domain service working on rich domain models](#3-thin-domain-service-working-on-rich-domain-models)
76-
- [ 4. Layers in a DDD application](#4-layers-in-a-ddd-application)
77-
- [ 5. Entities](#5-entities)
78-
- [ 6. Value objects](#6-value-objects)
79-
- [ 7. Factories](#7-factories)
80-
- [ 8. Aggregates](#8-aggregates)
81-
- [ 9. Repositories](#9-repositories)
82-
- [10. Shared kernel](#10-shared-kernel)
83-
- [11. Domain events](#11-domain-events)
84-
- [12. Anti-corruption layer](#12-anti-corruption-layer)
85-
- [13. Folders structure](#13-folders-structure)
86-
- [14. Available resources](#14-avaliable-resourses)
87-
88-
> This package provide utils file and interfaces to assistant build a complex application with domain driving design and typescript
71+
---
8972

9073
## 1. Ubiquitous language:
9174

@@ -185,3 +168,190 @@ Check lib documentation on link [Here](https://github.com/4lessandrodev/types-dd
185168
- Used to translate models from outside systems or legacy apps to models inside the bounded context and vice versa, and also to ease the communication with legacy services
186169
- Can use service facades and model adapters
187170

171+
172+
173+
---
174+
175+
## 13 - Summary - Basic Usage
176+
177+
Check full documentation on link [Here](https://github.com/4lessandrodev/types-ddd/tree/main/docs)
178+
179+
### Value Object
180+
181+
> A value object is a small, simple object that represents a single value or characteristic, such as a monetary amount or a date. It is characterized by having no identity of its own, meaning it is equal to another value object if its values are equal, regardless of its reference. Value objects are often used in domain-driven design to represent simple entities in the system.
182+
183+
#### Create a value object with business rules.
184+
185+
```ts
186+
187+
import { ValueObject, Ok, Fail, Result } from 'types-ddd';
188+
189+
interface Props {
190+
amount: number;
191+
}
192+
193+
export default class Money extends ValueObject<Props> {
194+
195+
// private constructor
196+
private constructor(props: Props) {
197+
super(props);
198+
}
199+
200+
// any business rule behavior
201+
sum(x: Money): Money {
202+
const { number: Calc } = this.util;
203+
const value = fee.get('amount');
204+
const current = this.props.total.get('amount');
205+
const amount = Calc(current).sum(value);
206+
return new Money({ amount });
207+
}
208+
209+
// any business rule behavior
210+
subtract(x: Money): Money {
211+
const { number: Calc } = this.util;
212+
const value = fee.get('amount');
213+
const current = this.props.total.get('amount');
214+
const amount = Calc(current).subtract(value);
215+
return new Money({ amount });
216+
}
217+
218+
// any business rule
219+
public static isValidProps({ amount }: Props): boolean {
220+
return this.validator.number(amount).isPositive();
221+
}
222+
223+
// shortcut to create a zero value
224+
public static zero(): Money {
225+
return new Money({ amount: 0 });
226+
}
227+
228+
// factory method to create a instance
229+
public static create(amount: number): Result<Money> {
230+
231+
const isValid = this.isValidProps({ amount });
232+
if(!isValid) return Fail("Invalid amount for money");
233+
234+
return Ok(new Money({ amount }));
235+
}
236+
}
237+
238+
```
239+
240+
How to use value object instance
241+
242+
```ts
243+
244+
// operation result
245+
const result = Money.create(500);
246+
247+
// check if provided a valid value
248+
console.log(result.isOk());
249+
250+
> true
251+
252+
// money instance
253+
const money = result.value();
254+
255+
money.get("amount"); // 500
256+
257+
```
258+
259+
---
260+
261+
### Entity
262+
263+
> An entity in domain-driven design is an object that represents a concept in the real world and has a unique identity and attributes. It is a fundamental building block used to model complex business domains.
264+
265+
#### Create an entity with business rules.
266+
267+
```ts
268+
269+
import { Entity, Ok, Fail, Result, UID } from 'types-ddd';
270+
271+
interface Props {
272+
id?: UID;
273+
total: Money;
274+
discount: Money;
275+
fees: Money;
276+
}
277+
278+
export default class Payment extends Entity<Props> {
279+
280+
// private constructor
281+
private constructor(props: Props){
282+
super(props);
283+
}
284+
285+
// any business rule behavior
286+
checkDiscount(value: Money): Money {
287+
const { number: Check } = this.validator;
288+
const total = this.props.total.get('amount');
289+
const discount = value.get('amount');
290+
const isGt = Check(discount).isGreaterThan(total);
291+
if(isGt) return this.props.total;
292+
return value;
293+
}
294+
295+
// any business rule behavior
296+
applyFees(value: Money): Payment {
297+
const props = this.props;
298+
const fees = props.fees.sum(value);
299+
const total = props.total.sum(fees);
300+
return new Payment({ ...props, total, fees });
301+
}
302+
303+
// any business rule behavior
304+
applyDiscount(value: Money): Payment {
305+
const props = this.props;
306+
const result = this.checkDiscount(value);
307+
const discount = props.discount.subtract(result);
308+
const total = props.total.subtract(discount);
309+
return new Payment({ ...props, total, discount });
310+
}
311+
312+
// factory method to create a instance
313+
public static create(props: Props): Result<Payment> {
314+
return Ok(new Payment(props));
315+
}
316+
}
317+
318+
```
319+
320+
How to use entity instance
321+
322+
```ts
323+
324+
// operation result
325+
const total = Money.create(500).value();
326+
const discount = Money.zero();
327+
const fees = Money.zero();
328+
329+
// create a payment
330+
const payment = Payment.create({ total, discount, fees }).value();
331+
332+
// create fee and discount
333+
const fee = Money.create(17.50).value();
334+
const discount = Money.create(170.50).value();
335+
336+
// apply fee and discount
337+
const result = payment.applyFees(fee).applyDiscount(discount);
338+
339+
// get object from domain entity
340+
console.log(result.toObject());
341+
342+
{
343+
"id": "d7fc98f5-9711-4ad8-aa16-70cb8a52244a",
344+
"total": 347,
345+
"discount": 170.50,
346+
"fees": 17.50,
347+
"createdAt":"2023-01-30T23:11:17.815Z",
348+
"updatedAt":"2023-01-30T23:11:17.815Z"
349+
}
350+
351+
```
352+
353+
---
354+
355+
## Lib Full Documentation
356+
357+
Check lib documentation on link [Here](https://github.com/4lessandrodev/types-ddd/tree/main/docs)

readme/cover.png

543 KB
Loading

0 commit comments

Comments
 (0)