-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity/ValueObject: receive Props Instead of Identifier #4
Comments
TypeScript can not implements generics type. type Props = {
id: Identifier<string>
}
class MyEntity extends Entity<Props>{} |
BREAKING CHANGE: This change require props object always on Entity #4
I've tried to apply this pattern. Props
return new AppSession({
- ...(this as AppSessionProps),
+ ...this.props
+ hatebuId: hatebu.props.id
}); Cons
This is ugly. :( - const lastUpdatedDate = hatebu.bookmark.lastUpdated;
+ const lastUpdatedDate = hatebu.props.bookmark.props.lastUpdated; We can avoid this cons to define getter function to |
import { Entity, Identifier, ValueObject } from "../src";
import * as assert from "assert";
class ShoppingCartItemIdentifier extends Identifier<string> {
}
interface ShoppingCartItemProps {
id: ShoppingCartItemIdentifier;
name: string;
price: number;
}
class ShoppingCartItem extends Entity<ShoppingCartItemProps> implements ShoppingCartItemProps {
id: ShoppingCartItemIdentifier;
name: string;
price: number;
constructor(props: ShoppingCartItemProps) {
super(props);
this.id = props.id;
this.name = props.name;
this.price = props.price;
}
}
interface ShoppingCartItemCollectionProps {
items: ShoppingCartItem[];
}
class ShoppingCartItemCollection extends ValueObject<ShoppingCartItemCollectionProps> implements ShoppingCartItemCollectionProps {
items: ShoppingCartItem[];
constructor(props: ShoppingCartItemCollectionProps) {
super(props);
this.items = props.items
}
}
class ShoppingIdentifier extends Identifier<string> {
}
interface ShoppingCartProps {
id: ShoppingIdentifier;
itemsCollection: ShoppingCartItemCollection;
}
class ShoppingCart extends Entity<ShoppingCartProps> implements ShoppingCartProps {
id: ShoppingIdentifier;
itemsCollection: ShoppingCartItemCollection;
constructor(props: ShoppingCartProps) {
super(props);
this.id = props.id;
this.itemsCollection = props.itemsCollection;
}
}
describe("ShoppingCart", () => {
it("should have own property and props", () => {
const shoppingCart = new ShoppingCart({
id: new ShoppingCartItemIdentifier("shopping-cart"),
itemsCollection: new ShoppingCartItemCollection({
items: []
})
});
assert.ok(Array.isArray(shoppingCart.itemsCollection.items));
assert.strictEqual(shoppingCart.itemsCollection.items, shoppingCart.props.itemsCollection.props.items)
});
}); |
Add docs |
TypeScript support declaretion merging
interface UserData {
name: string;
birthday: Date;
regdate: Date;
social: string;
passwordHash: string;
isAdmin: boolean;
}
interface User extends UserData {}
class User {
constructor(data: UserData) {
Object.assign(this, data);
}
deservesCake() {
return this.birthday.getDate() == new Date().getDate() || this.isAdmin;
}
} This approach has a limitation. |
I've noticed that I often write a entity like following:
This
Hatebu
entity has id and Props type.We can use
Props
type insteadofId
.Becaseue, Alwasys
Props
includesid
.Props
Copyable
insterface just useProps
typeCons
id
is fixed nameThe text was updated successfully, but these errors were encountered: