Skip to content

Commit

Permalink
docs: update mapper documentation (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Jul 21, 2022
1 parent 88bcdd8 commit d5155d3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions docs/guides/data-handling-with-mappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ export interface CategoryData {
**category.model.ts**

```typescript
export class Category {
export interface Category {
id: string;
name: string;
transformed: number;
}
```

The mapper should be a class with static methods that will be used in the corresponding service.

In special cases, the mapper class might need some dependencies to transform data correctly.
In that case, you can declare it as an `Injectable` and use Angular's dependency injection mechanism to provide an instance in your service.

> :warning: If the mapper needs no dependencies, always use static methods!
**category.mapper.ts**

```typescript
@Injectable({ providedIn: 'root' })
export class CategoryMapper {
fromData(categoryData: CategoryData): Category {
static fromData(categoryData: CategoryData): Category {
const category: Category = {
id: categoryData.id,
name: categoryData.id,
Expand All @@ -48,7 +54,7 @@ export class CategoryMapper {
return category;
}

fromObject(category: Category): CategoryData {
static fromObject(category: Category): CategoryData {
const categoryData: CategoryData = {
id: category.id,
name: category.id,
Expand Down

0 comments on commit d5155d3

Please sign in to comment.