1

Let's say that we have a structure like this:

Application
    -- Modules
        --Module1
            -- Controller
            -- PublicHelper 

        --Module2
           -- Controller
           -- PublicHelper

Whereby a module's Public Helper can provide helper functions.

In nearly every module helper, the helper needs to access another module's public helper methods.

Let's say for instance, in a PHP application:

Module1 provides functionality to create a sale, and as part of the

class Module1PublicHelper extends AbstractModuleHelper {

    public function createSale($customerId, $param, $param)
    {
         $userPublicHelper = // grab an instance of the user public helper
         $currentUser = $userPublicHelper->getCurrentUser();
    }
}

class Module2PublicHelper extends AbstractModuleHelper {

    public function getCurrentUser()
    {
         //do something
         return $user;
    }
}

In this situation, the method needs to obtain an instance, either new or whatever of the user public helper.

Given that all of Module Public Helper classes are instantiated with a minimum set of constructor params, e.g. EntityManager, what would be the best way to get a copy of it?

Obviously, we can't really inject the user public helper class into the class containing createSale

One solution would be to use a service locator or registry, however, testing the application isn't exactly easy.

1
  • Sooo what's up?
    – andho
    Jan 6, 2014 at 8:17

1 Answer 1

2

In most cases, this could be just two layers of the same module. The difference between layers and modules are that as layers are on top of each other, the higher layers is allowed to ask for information from the lower layer.

If you really have two modules, Module1 can have it's own stripped down version of User which helps to just keep track of the identity of User, or any other information Module1 might need. This will be difficult if you have a data driven application.

Having Module1 depend on Module2 is bad design.

Now let me try to explain this further with your example.

You have a createSale method. The purpose of this method is to create a sale. What I would do in this case is assume that the $customerId that is send to the method is correct from which ever collaborator that called it.That is why you need to completely separate your Model layer from the Application layer.

Consider the following structure.

Application
    -- Modules
        --Module1
            -- Controller
            -- PublicHelper 

        --Module2
           -- Controller
           -- PublicHelper
Model
    -- Sales
        -- SalesManager
        -- Sale
    -- Users
        -- UserManager
        -- User

Now with the following Module1Controller I hope you can understand the setup.

class Module1Controller {
    public function createSalesAction() {
        $userId = $this->getPost('user-id');

        // UserManager is injected to application level
        $user = $this->userManager->getUser($userId);

        // SalesManager is injected to application level
        $this->salesManager->createSale($user->getId(), $param1, $param2);
    }

    public function giveCredit() {
        $userId = $this->getPost('user-id');
        $credit = $this->getPost('credit');

        $this->salesManager->setCredit($userId, $credit);
    }
}

Then a sample Sales Manager might help

class SalesManager {
    public function setCredit($userId, $credit) {
        $credit = new Credit($userId, $credit);
        $this->creditRepo->save($credit);
    }

    public function createSale($userId, $amount, $param2) {
        if (!$this->hasCredit($userId, $amount)) {
            throw new CustomerDontHaveCredit($userId, $amount);
        }

        $sale = new Sale($userId, $amount, $param1);
        $this->salesRepo->save($sale);
    }

    private function hasCredit($userId, $amount) {
        // get the credit
        // get sales if need and check if he has credit

        return true;
    }
}

Sales module is completely oblivious of User module and vice versa. Furthermore, the actual development of Sales and Users module can be completely separate from the actual application. Meaning you can mock the SalesManager and UserManager in the app for testing.

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.