2

I need some advice how to structure my code in magento 2. I am working on an ERP interface to import and export products, customers and orders.

So I need some helper classes to take care of some common tasks. Some of these tasks are so generic that need them in any case (import, export, products, customers... you name it).

Example: a logger function will be needed in any of those use cases.

Other functions are more product specific or more customer specific.

My thinking might be antiquated but in M1 I used to create a base helper that get extended by a generic import helper which gets extended by a customer import helper or a product import helper. So no matter what helper I use, I always have the base functions available at all times.

I can do that in M2 but it is cumbersome. Whenever I extend the base helper with new functions I usually have to add new classes to the constructor (DI). But since all other helpers extend the base helper those constructors need to have the same classes as parameters. This is borderline idiotic in my opinion.

So what would be a proper way to do it in magento 2 without a constructor massacre?

Of course I could put everything into ONE class but that is not my idea of clean code.

1 Answer 1

0

In my opinion, Helper is an anti-pattern. But, we're still using it...and I'm still using it.

Magento 2 - What does the AbstractHelper do, and is it required?

Think about another class: Model. We can move the Helper logic to Model class. For example, we usually use Helper to get the config. We can move that part to Model (ex: \Magento\Paypal\Model\Config, \Magento\Tax\Model\Config).

And, surely, we can add the generic class to Model. Ex: \Magento\ImportExport\Model\Export, \Magento\ImportExport\Model\Import

1
  • Using models does not solve the constructor issue.
    – omsta
    Mar 1, 2019 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.