def deconstruct(self, entity: Entity) -> Dto: """ Defines a way to deconstruct (aka serialize) entity object into simple values. Simple deconstruction may just extract values from fields. Complex cases may need to use another factory to deconstruct objects of dependent types. """ data = asdict(entity) if self.mapped_fields: data = { field: value for field, value in data.items() if field in self.mapped_fields } dto = Dto(data) dto.__id__ = entity.id return dto
def create(self, **kwargs) -> Entity: """ Creates an object compatible with this repo. Uses repo's schema or the klass iff schema not present. NB: Does not inserts the object to the repo. Use `create_and_add` method for that. """ return self.schema.construct(Dto(kwargs))
def dto(self, data): dto = Dto(data) dto.__id__ = 17 return dto
def insert(self, **kwargs) -> Id: id_ = self._get_id() dto = Dto(kwargs) dto.__id__ = id_ self._register[id_] = dto return id_