Closed flowluap closed 1 year ago
@jorgebodega At first look the instance looks good, but there is no way to execute (neiter eager, nor lazy) before execution of self.
So how would you propose to build this the intended way?
Hi! Some points:
attrs
is a protected function, so it cannot be accessed from an instance. I suppose you know, but just in case.attrs
is just to set the default values for mandatory or desired values.Subfactory
class is just a helper, is not mandatory to use. Just helps to avoid to take control of create
/make
.Subfactory
is optional.I removed context
because I see no use case. Just some different cases:
If you need to pass the context in every execution, just use them as parameters:
new ProductVariantFactory().create({ productId: product.id })
If you need a context for evey execution you can just override the factory constructor and use like:
export class ProductVariantFactory extends Factory<MProductVariant> {
protected entity = MProductVariant;
protected dataSource = CoreDataSource;
constructor(private context: YourContext) {}
protected attrs(context): FactorizedAttrs<MProductVariant> {
const product = this.context.product ? this.context.product : new SingleSubfactory(ProductFactory,{})
return {
id: generateTestUuid(),
productId: product.id
};
}
};
Or maybe superset the functions:
export class ProductVariantFactory extends Factory<MProductVariant> {
...
public customCreate(...originalParams, context: YourContext) {
return this.create({
...
})
}
};
But I would not reccomend neither of those. I think the best way is to use a InstanceAttribute, but if you comment me more, I could help better.
Hope it helps!
We have the following (simplified) case:
In the past we had the following factory, based on the context:
If i want to replicate this with your lib, i see the possibility to use a SubFactory. Is there a way to input parameters into the factory, that then will let you decide to form a SubFactory or use the passed parameters like that:
Thanks for your efforts!