Best practise in business models for dependencies



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



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.



@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.


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