Skip to content

Commit 5d0ffc6

Browse files
Initial commit
0 parents  commit 5d0ffc6

38 files changed

+867
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# angular-domain-driven-design
2+
3+
[Edit on StackBlitz ⚡️](https://stackblitz.com/edit/angular-domain-driven-design)

angular.json

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3+
"version": 1,
4+
"newProjectRoot": "projects",
5+
"projects": {
6+
"demo": {
7+
"root": "",
8+
"sourceRoot": "src",
9+
"projectType": "application",
10+
"prefix": "app",
11+
"schematics": {},
12+
"architect": {
13+
"build": {
14+
"builder": "@angular-devkit/build-angular:browser",
15+
"options": {
16+
"outputPath": "dist/demo",
17+
"index": "src/index.html",
18+
"main": "src/main.ts",
19+
"polyfills": "src/polyfills.ts",
20+
"tsConfig": "src/tsconfig.app.json",
21+
"assets": [
22+
"src/favicon.ico",
23+
"src/assets"
24+
],
25+
"styles": [
26+
"src/styles.css"
27+
],
28+
"scripts": []
29+
},
30+
"configurations": {
31+
"production": {
32+
"fileReplacements": [
33+
{
34+
"replace": "src/environments/environment.ts",
35+
"with": "src/environments/environment.prod.ts"
36+
}
37+
],
38+
"optimization": true,
39+
"outputHashing": "all",
40+
"sourceMap": false,
41+
"extractCss": true,
42+
"namedChunks": false,
43+
"aot": true,
44+
"extractLicenses": true,
45+
"vendorChunk": false,
46+
"buildOptimizer": true
47+
}
48+
}
49+
},
50+
"serve": {
51+
"builder": "@angular-devkit/build-angular:dev-server",
52+
"options": {
53+
"browserTarget": "demo:build"
54+
},
55+
"configurations": {
56+
"production": {
57+
"browserTarget": "demo:build:production"
58+
}
59+
}
60+
},
61+
"extract-i18n": {
62+
"builder": "@angular-devkit/build-angular:extract-i18n",
63+
"options": {
64+
"browserTarget": "demo:build"
65+
}
66+
},
67+
"test": {
68+
"builder": "@angular-devkit/build-angular:karma",
69+
"options": {
70+
"main": "src/test.ts",
71+
"polyfills": "src/polyfills.ts",
72+
"tsConfig": "src/tsconfig.spec.json",
73+
"karmaConfig": "src/karma.conf.js",
74+
"styles": [
75+
"styles.css"
76+
],
77+
"scripts": [],
78+
"assets": [
79+
"src/favicon.ico",
80+
"src/assets"
81+
]
82+
}
83+
},
84+
"lint": {
85+
"builder": "@angular-devkit/build-angular:tslint",
86+
"options": {
87+
"tsConfig": [
88+
"src/tsconfig.app.json",
89+
"src/tsconfig.spec.json"
90+
],
91+
"exclude": [
92+
"**/node_modules/**"
93+
]
94+
}
95+
}
96+
}
97+
}
98+
},
99+
"defaultProject": "demo"
100+
}

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "angular",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"@angular/common": "^8.0.0",
7+
"@angular/compiler": "^8.0.0",
8+
"@angular/core": "^8.0.0",
9+
"@angular/forms": "^8.0.0",
10+
"@angular/platform-browser": "^8.0.0",
11+
"@angular/platform-browser-dynamic": "^8.0.0",
12+
"@angular/router": "^8.0.0",
13+
"core-js": "2",
14+
"rxjs": "^6.5.2",
15+
"uuid": "^7.0.2",
16+
"uuidv4": "^6.0.6",
17+
"zone.js": "^0.9.1"
18+
},
19+
"scripts": {
20+
"ng": "ng",
21+
"start": "ng serve",
22+
"build": "ng build",
23+
"test": "ng test",
24+
"lint": "ng lint",
25+
"e2e": "ng e2e"
26+
},
27+
"devDependencies": {
28+
"@angular-devkit/build-angular": "~0.10.0",
29+
"@angular/cli": "~7.0.2",
30+
"@angular/compiler-cli": "~7.0.0",
31+
"@angular/language-service": "~7.0.0",
32+
"@types/node": "~8.9.4",
33+
"@types/jasmine": "~2.8.8",
34+
"@types/jasminewd2": "~2.0.3",
35+
"codelyzer": "~4.5.0",
36+
"jasmine-core": "~2.99.1",
37+
"jasmine-spec-reporter": "~4.2.1",
38+
"karma": "~3.0.0",
39+
"karma-chrome-launcher": "~2.2.0",
40+
"karma-coverage-istanbul-reporter": "~2.0.1",
41+
"karma-jasmine": "~1.1.2",
42+
"karma-jasmine-html-reporter": "^0.2.2",
43+
"protractor": "~5.4.0",
44+
"ts-node": "~7.0.0",
45+
"tslint": "~5.11.0",
46+
"typescript": "~3.1.1"
47+
}
48+
}

src/app/app.component.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
font-family: Lato;
3+
}

src/app/app.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<hello name="{{ name }}"></hello>
2+
<p>
3+
Start editing to see some magic happen :)
4+
</p>
5+
6+
<app-user></app-user>
7+
<app-travel></app-travel>

src/app/app.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'my-app',
5+
templateUrl: './app.component.html',
6+
styleUrls: [ './app.component.css' ]
7+
})
8+
export class AppComponent {
9+
name = 'Angular';
10+
}

src/app/app.module.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NgModule } from '@angular/core';
2+
import { BrowserModule } from '@angular/platform-browser';
3+
import { FormsModule } from '@angular/forms';
4+
5+
import { AppComponent } from './app.component';
6+
import { HelloComponent } from './hello.component';
7+
import { UserComponent } from './user/feature/user.component';
8+
import { TravelComponent } from './travel/feature/travel.component';
9+
10+
@NgModule({
11+
imports: [ BrowserModule, FormsModule ],
12+
declarations: [ AppComponent, HelloComponent, UserComponent, TravelComponent ],
13+
bootstrap: [ AppComponent ]
14+
})
15+
export class AppModule { }

src/app/core/domain/AggregateRoot.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Entity } from './Entity';
2+
import { DomainEvents } from './events/DomainEvents';
3+
import { Observable, BehaviorSubject } from 'rxjs';
4+
import { ActionEvents } from './events/ActionEvents.enum';
5+
import { pipe, UnaryFunction } from 'rxjs';
6+
import { tap, map } from 'rxjs/operators';
7+
8+
export abstract class AggregateRoot<T> extends Entity<T> {
9+
private _domainEvents: any[] = [];
10+
private _domainObserver: Observable<AggregateRoot<T>>;
11+
private _domainBS: BehaviorSubject<AggregateRoot<T>>;
12+
13+
get id() {
14+
return this._id;
15+
}
16+
17+
get domainEvents(): any[] {
18+
return this._domainEvents;
19+
}
20+
21+
get domainObserver(): Observable<AggregateRoot<T>>{
22+
return this._domainObserver;
23+
}
24+
25+
set domainObserver(domainEvent: Observable<AggregateRoot<T>>){
26+
this._domainObserver = domainEvent;
27+
}
28+
29+
get domainBS(): BehaviorSubject<AggregateRoot<T>>{
30+
return this._domainBS;
31+
}
32+
33+
set domainBS(domainEvent: BehaviorSubject<AggregateRoot<T>>){
34+
this._domainBS = domainEvent;
35+
}
36+
37+
protected addDomainEvent (domainEvent: any){
38+
this._domainEvents.push(domainEvent);
39+
DomainEvents.markAggregateForBS(this);
40+
this.logDomainEventAdded(domainEvent);
41+
}
42+
43+
private logDomainEventAdded (domainEvent: any){
44+
const thisClass = Reflect.getPrototypeOf(this);
45+
const domainEventClass = Reflect.getPrototypeOf(domainEvent);
46+
console.log('[Domain Event Created]: ', thisClass.constructor.name);
47+
48+
}
49+
50+
protected dispatchObserver(domainPipe: UnaryFunction<any, any>){
51+
DomainEvents.dispatch(this, ActionEvents.Observable,domainPipe);
52+
}
53+
54+
protected dispatchBS(){
55+
DomainEvents.dispatch(this, ActionEvents.BehaviorSubject);
56+
}
57+
}

src/app/core/domain/Entity.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { v4 as uuid } from 'uuid';
2+
3+
const isEntity = (v: any): v is Entity<any> => {
4+
return v instanceof Entity;
5+
};
6+
7+
export abstract class Entity<T>{
8+
protected readonly _id:string;
9+
public readonly props: T;
10+
11+
constructor (props: T, id?: string){
12+
this._id = id ? id : uuid();
13+
this.props = props;
14+
}
15+
16+
public equals (object?: Entity<T>) : boolean {
17+
if(object == null || object == undefined) {
18+
return false;
19+
}
20+
if (this === object) {
21+
return true;
22+
}
23+
if(!isEntity(object)){
24+
return false;
25+
}
26+
return ;
27+
}
28+
}

src/app/core/domain/UseCase.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Observable } from 'rxjs';
2+
3+
export interface UseCase <EntryObject, ExitObject>{
4+
execute (entry?: EntryObject) : Observable<ExitObject> | Promise<ExitObject> | ExitObject;
5+
}

src/app/core/domain/UseCaseError.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface IUseCaseErrorError {
2+
message: string;
3+
}
4+
5+
export abstract class UseCaseError implements IUseCaseErrorError{
6+
public readonly message: string;
7+
8+
constructor (message: string){
9+
this.message = message;
10+
}
11+
}

src/app/core/domain/ValueObject.ts

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum ActionEvents {
2+
Observable = 'observable',
3+
BehaviorSubject = 'behaviorSubject'
4+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { AggregateRoot } from '../AggregateRoot';
2+
import { BehaviorSubject, Observable, pipe, UnaryFunction } from 'rxjs';
3+
import { tap, map } from 'rxjs/operators';
4+
import { ActionEvents } from './ActionEvents.enum';
5+
6+
interface GlobalBS {
7+
[n: string]: {
8+
behaviourSubject: BehaviorSubject<any>,
9+
observable: Observable<any>,
10+
className: string
11+
}
12+
}
13+
14+
type OptionType = ActionEvents.Observable | ActionEvents.BehaviorSubject;
15+
16+
17+
export class DomainEvents<T> {
18+
private static handlersMap = {};
19+
private static markedAggregates: AggregateRoot<any>[] = [];
20+
private static globalBS: GlobalBS = {};
21+
22+
public static markAggregateForBS<T>
23+
(aggregate: AggregateRoot<T>): void {
24+
if (aggregate instanceof AggregateRoot) {
25+
const aggregateFound = !!this.findAggregateByID(aggregate.id);
26+
if (!aggregateFound) {
27+
aggregate.domainBS =
28+
new BehaviorSubject<AggregateRoot<T>>(aggregate);
29+
30+
aggregate.domainObserver = aggregate.domainBS.asObservable();
31+
32+
this.markedAggregates.push(aggregate);
33+
34+
this.globalBS = {
35+
...this.globalBS,
36+
[aggregate.id]: {
37+
behaviorSubject: aggregate.domainBS,
38+
observable: aggregate.domainObserver,
39+
className: aggregate.constructor.name
40+
}
41+
} as GlobalBS;
42+
}
43+
}
44+
}
45+
46+
public static dispatch<T>
47+
(aggregate: AggregateRoot<T>,
48+
option: OptionType,
49+
pipeCallback?: UnaryFunction<any, any>) {
50+
switch (option) {
51+
case ActionEvents.BehaviorSubject: {
52+
aggregate.domainBS = this.globalBS[aggregate.id][ActionEvents.BehaviorSubject];
53+
aggregate.domainBS.next(aggregate);
54+
return aggregate.domainBS;
55+
}
56+
case ActionEvents.Observable: {
57+
if (pipeCallback) {
58+
aggregate.domainObserver =
59+
this.globalBS[aggregate.id][ActionEvents.Observable].pipe(
60+
pipeCallback
61+
);
62+
return aggregate.domainObserver;
63+
}
64+
}
65+
}
66+
}
67+
68+
private static findAggregateByClassName(className: string){
69+
return Object.values(this.globalBS).filter(aggeg => aggeg.className === className)[0]
70+
}
71+
72+
private static findAggregateByID(id: string): AggregateRoot<any> {
73+
let found: AggregateRoot<any> = null;
74+
for (let aggregate of this.markedAggregates) {
75+
if (aggregate.id === id) {
76+
found = aggregate;
77+
}
78+
}
79+
return found;
80+
}
81+
82+
};

0 commit comments

Comments
 (0)