1

I am working on an e-commerce site. I am following clean architecture with MVVM and Dagger 2.But am getting stuck when we need to communicate in between modules, means i have to call some methods and class from one module another. Actual scenario is - There is one separate module for cart and another for categories(consisting of products based on categories)
I have some set of APIs in cart module (ex. addToCart,fetch cartList,remove cart item etc.)which need to be call in both cart module and categories module(addToCart API need to call from categories module). One way to do it as again writing same code in both module that will increase extra effort and also make both module dependent,thats violate clean architecture approach.

Can anybody suggest me best approach for this in clean architecture.

sweet_vish
  • 141
  • 1
  • 8

1 Answers1

0

You can create a new module with the shared code and add it as dependency in both modules (cart and categories), if you have modularized by layers you can do the same for data layers, so if you need the same APIs in another module you can add it as dependency or split it in new modules if you will use only some parts.

    implementation project(':common_apis_module')

In clean architecture if you do a request from a module it's understood as different usecases, if you need to get the same data from the model in both cases maybe you need share the UseCase as well in a common module.

Create Interfaces for communication between modules in common_apis_module.

    interface CommunicationModule1 {
    fun doSomethingInModule1(someParam: String)
    fun doAnotherThingInModule1(anotherParam: Int)
}

and create another interface for communication in second module equal as above. then you have that common module added in both modules you'll be able to use the interfaces in both modules. I recommend use dagger.

    class OneClassInModule2 : DaggerAppCompatActivity{

    @Inject
    late init var communicationModule1 : CommunicationModule1

}

Second Class:

class OneClassInModule1: CommunicationModule1 {
  override fun doSomethingInModule1(someParam: String){
    // Do Something with the string
  }
}
Bonestack
  • 81
  • 6