How should dependencies be managed across a modular application?

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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật