[TYPO3-mvc] Storing an object from a repository into session and get it back

bernd wilke t3ng at bernd-wilke.net
Fri Jun 26 12:24:27 CEST 2015


Am 26.06.15 um 11:31 schrieb Jan Kornblum:
> Hi Viktor,
>
>>> Assuming there are only "productId" and "quantity" given as a
>>> "basketItem"s property, would it break DDD ideas if i would add a
>>> getter getProduct() to the "basketItem" model and inside this getter
>>> i fetch the "product" directly from the repository?
>
>> Well, I don't see any OOP or DDD violation here =)
>> You have a model, that gives back different model by getProduct()
>> method. Retrieveing of this model is internal and doesn't matter how
>> it was done: by Extbase or by yourself.
>
> Thanks. But for any reasons my approach doesn't fetch the original (and
> maybe modified in between, compared to the product which had been put
> into basket before) product. I think my approach before was generally
> bad. So i modified it to just store product IDs:
>
> class Basket {
>      protected $basketItems;
>      function getItems() {}
>      function addItem() {}
>      function removeItem() {}
>      function getTotalAmount() {}
>      function getTotalPrice() {}
> }
>
> class BasketItem {
>      protected $productId;
>      protected $quantity;
> }
>
> class BasketService {
>      function getBasket() { //fetches the basket object from session and
> creates if not yet exists }
>      function setBasket() { //stores the basket object back to session }
>      function addItem() { //fetches from session, adds an item to
> basket, stores back to session }
>      function removeItem() { //see above }
> }
>
> But *where* do i merge the stored product IDs with the real product
> objects now? I whish to be able to do something like this in controller:
>
> $this->basketService->getBasket()->getTotalAmount(); // not possible as
> only product IDs are stored
> $this->basketService->getBasket()->getItems(); // same as above
>
> Should both methods getTotalAmount() and getItems() be rather
> implemented into the BasketService class instead of the basket directly?
> Doing all the repository operations in this service class instead of the
> (session stored) Basket object sounds better to me, but am i right? What
> would you do this like?
>
> Kind regadrds, Jan
>
I think BasketService->getBasket has to fetch the list of Ids (with 
count) and create a fresh Basket, adding each IDs as new Item.
in this way the basket should be the same than build up from new by the 
user.
Reinstanciating basket just from sessiondata would need to have 
information about all members and they must be processed in the construct()


bernd
-- 
http://www.pi-phi.de/cheatsheet.html


More information about the TYPO3-project-typo3v4mvc mailing list