Best practise in business models for dependencies


#1

Hello,

i have a general question.
Let’s assume a Mapper class in my business model. Somewhere in the class i have to get a new Transfer. The classic way you would do is “new XyzTransfer()”. I can see this everywhere in the core code.

But everywhere else in Spryker the factory is used to instantiate new objects.

So, what is the best way for that as long as there is no possible $this->getFactory() call available in the Model?

I can think of two ways

  1. Inject the factory to business model?

  2. Stay with “new” keyword in business models

First seems dirty and is maybe violating the SRP.
Second seems not flexible enough to test.

Any other ideas?

What is your recommendation and why?

Best regards


#2

Hi!

Since Transfers are dumb data containers and come without dependencies it is ok to create and fill them within your business logic.
The models created in the factory (potentially) come with dependencies. Those you want to have in a central (per module) place, so you can easily see and control their dependencies.

Cheers


#3

@lazystar thanks for fast response

Okay, that dependent logic is the critical point. So on the other hand, a model with dependencies should be injected via constructor (also in the factory)?

Best regards.


#4

Exactly, since Factories are our mechanism of Dependency Injection, that we favour for its explicitness!