R
R
REDkiy2019-08-27 14:51:43
Python
REDkiy, 2019-08-27 14:51:43

How to organize the project in such a way that when developing modules, it does not require the project itself?

In a Python project, I'm trying to follow the MVC approach. The project has grown and adding functionality to it to solve specific problems has become unprofitable. I decided to try adding the ability to create modules.
I made a modules folder and import its contents based on the names of the subfolders. That is, you create a folder with a specific name, and through this name, the module is imported and activated.
The module pulls controllers from the main project for its work. Access to the database is also through controllers.
How best to distribute the main project, so that it is convenient to write modules. Everything is simple for me, everything is in one repository. But what if there is no access to the repository, but only a truncated copy of the project?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris Korobkov, 2019-08-27
@BorisKorobkov

The module pulls controllers from the main project for its work.

Something is wrong with the business logic. Usually on the contrary, the project uses a module (the brain gives the command to the legs to go, and not the legs control the brain).
It is more correct to write such a module so that it does not depend on your project. He must have access to the database. Install it via requirements.txt
If the module cannot be made independent, then at least let it use an interface that must be provided to third-party developers.

I
Ilya Chichak, 2019-09-08
@ilya_chch

You contradict yourself:
It turns out that your module will not be able to work without a project. Generally.
It seems to me that you should make modules smaller so that they solve only some specific small task.
Look in the direction of how the Middleware mechanism in Django is arranged - there is a list of middleware and the request is sequentially passed to each of them, and the modified request returned is passed to the next one.
Regarding access to the database, Django is also a good example. During initialization, a connection to the database is opened and placed in some configuration storage, from where it can be taken by modules and used. So, with separate development of modules, it will be possible to put your own, but in the assembled project the global one will be used, which will reduce the time to open the connection.
In order to build the development of such modules - designate a clear interface for each type of module. For example, the modules responsible for working with the user receive a user object as input and also give the user object, but either do something along the way, or somehow modify it.

V
Vladimir Bondarev, 2019-09-09
@vladimirbondarev

Well, here, in my opinion, there is only one answer - this is SOLID and clean architecture . This approach will allow you to develop a testable and extensible application.
For example, the fact that your application accesses the database from the controllers is no longer good, since to test the business logic you will have to use the infrastructure, namely http. On a good note, you should have a business logic layer that is injected into the controller, and a business logic layer should be injected into, for example, a data access layer. Thus, you can test any layer independently of each other, since you will have loose coupling of system components.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question