Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export interface IUser {
- firstName?: string;
- lastName?: string;
- age?: number;
- weight?: number
- }
- const userDto: IUser = { // DTO (data transfer object)
- firstName: 'Cory',
- lastName: 'Rylan',
- age: 100,
- weight: 150
- };
- // mock API data instance for User
- export class User {
- private _firstName: string;
- private _lastName: string;
- private _age: number;
- get firstName() {
- return this._firstName;
- }
- get lastName() {
- return this._lastName;
- }
- get fullName() {
- return `${this.firstName} ${this.lastName}`;
- }
- get age() {
- return this._age;
- }
- constructor(firstName: string, lastName: string, age: number) {
- this.setName(firstName, lastName);
- this.setAge(age);
- }
- setName(firstName: string, lastName: string) {
- if (this.validName(firstName) && this.validName(lastName)) {
- this._firstName = firstName;
- this._lastName = lastName;
- }
- }
- setAge(age: number) {
- if (age >= 18) {
- this._age = age;
- } else {
- throw new Error('User age must be greater than 18');
- }
- }
- private validName(name: string) {
- if (name.length > 0 && /^[a-zA-Z]+$/.test(name)) {
- return true
- } else {
- throw new Error('Invalid name format');
- }
- }
- }
- const user = new User('John', 'Doe', 18);
- user.setName('Jack', 'Doe');
- console.log(user.fullName); // Jack Doe
- const user2 = new User('John', 'Smith', 17);
- // Error: User age must be greater than 18
- -----------
- export interface IUser {
- firstName?: string;
- lastName?: string;
- age?: number;
- weight?: number
- }
- export class User {
- private state: IUser = { };
- get firstName() {
- return this.state.firstName;
- }
- get lastName() {
- return this.state.lastName;
- }
- get fullName() {
- return `${this.firstName} ${this.lastName}`;
- }
- get age() {
- return this.state.age;
- }
- constructor(firstName: string, lastName: string, age: number) {
- this.setName(firstName, lastName);
- this.setAge(age);
- }
- setName(firstName: string, lastName: string) {
- if (this.validName(firstName) && this.validName(lastName)) {
- this.state.firstName = firstName;
- this.state.lastName = lastName;
- }
- }
- setAge(age: number) {
- if (age >= 18) {
- this.state.age = age;
- } else {
- throw new Error('User age must be greater than 18');
- }
- }
- private validName(name: string) {
- if (name.length > 0 && /^[a-zA-Z]+$/.test(name)) {
- return true
- } else {
- throw new Error('Invalid name format');
- }
- }
- }
- // We move the internal private properties to be on a single private internal state property. By moving the
- // internal private properties onto a single object, we can use the state property to easily copy or hydrate the // class with the data from our API. Let’s look at the hydration logic that will copy our data over.
- export interface Type<T> extends Function {
- new (...args: any[]): T;
- }
- export class DomainConverter {
- static fromDto<T>(domain: Type<T>, dto: any) {
- const instance = Object.create(domain.prototype);
- instance.state = dto;
- return instance as T;
- }
- static toDto<T>(domain: any) {
- return domain.state as T;
- }
- }
- const userDto: IUser = {
- firstName: 'Cory',
- lastName: 'Rylan',
- age: 27,
- weight: 150
- };
- const user: User = DomainConverter.fromDto<User>(User, userDto);
- const userData: IUser = DomainConverter.toDto<User>(user);
- console.log(userData);
- // { firstName: "Cory", lastName: "Rylan", age: 27, weight: 150 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement