Adding a module as dependency of another is a good practice?

Viewed 166

I'm creating a search engine for products, but the problem is that my User class is in another module, that serves just for user information and authentication. I want to retrieve user information in this search module, and copying the User class isn't a good idea, because if i need to change it, i will need to change it in two places. I saw that you can add a module as a dependency of another, but is that a good practice? Or is there another better way of doing it?

2 Answers

That's indeed the best practice - classes and packages should be separated to modules, and a module that needs to use classes or interfaces from another module could (and should!) depend on it.

You should break into different modules if some components can be grouped together.

If you can do that, then it's considered a best practice to define a module which encapsulates this group of components. Then if some other module needs some of those components it just imports the module that has encapsulated them.

Consider defining a shared module where you can put your User class or anything else that you want. But that module will made more sense if it also had shared components.

Some simple model classes can go directly into a shared package without defining a module for them.

Related