# Entity

Have an id (preferably a GUID rather than a DB generated int because business transactions do not rely on persistence, can be persisted after other operations carried out in model's behavior)

```typescript
import { 
  BaseDomainEntity, 
  Entity, 
  UniqueEntityID, 
  Result
} from 'types-ddd';

interface CarProps extends BaseDomainEntity {
  year: CarYearValueObject;
  color: CarColorValueObject;
}

class Car extends Entity<CarProps> {
  private constructor(props: CarProps) {
    super (props, Car.name)
  }
  
  get year (): CarYearValueObject {
    return this.props.year;
  }
  
  setYear (year: number): void {
    this.props.year = year;
  }
  
  get color (): CarColorValueObject {
    return this.props.color;
  }
  
  setColor (color: CarColorValueObject): void {
    this.props.color = color;
  }
  
  public static isValidYear (year: number): boolean {
    return year > 1920;
  }
  
  public static create (props: CarProps): Result<Car> {
   
    // Business logic validation. Just for example!
    // Some rules should be validated on the value object
    
    if (!Car.isValidYear(props.year.value)) {
        return Result.fail<Car>('Invalid year for a car');
     }
     
    return Result.ok<Car>(new Car(props));
  }
}

const car = Car.create({ ... }).getResult();

```

```
Accessible attributes for car
> car.color
> car.createdAt
> car.deletedAt
> car.equals
> car.id
> car.isDeleted
> car.isValidYear
> car.updatedAt
> car.year
> car.getHashCode
> car.toObject
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alessandroadm.gitbook.io/types-ddd/entity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
