Tips on modular application

5 years ago

Etruska
Member | 25
+
0
-

Hi,
with more of my project running on nette, I'm starting to think more about application modularity and maximum reusability. For example photo gallery – I need multiple image upload on each product page, same thing as a plugin to my WYSIWYG editor and also separate photo gallery component, which allows photo folders. All these components may vary a little, but the basics are the same.

At this moment I have many of my components spread out over many directories like AdminModule/Components, AdminModule/Forms, Model/Repositories etc, which is not really handy when copying to new project.
Do you prefer a single component (Nette\Application\UI\Control) composed of lots of smaller ones? I would appreciate any tips or links to helpful articles (English or Czech). Thanks.

5 years ago

srigi
Member | 560
+
0
-

For forms use separate definition of the form in its own file (you probably got this already). But there is one little trick – don't put callback handler into that form file – put it into Controller/Component (outside).

That way, you can reuse form many times and have different behavior depending where form is used.

Another tip:
for uploads I have one global table – files. Then there is a FilesService which manages everything about files. Anytime I need to upload/delete/read (stored) file, I inject FilesService (in app/services folder, Hive\Services namespace) and delegate that work to it.

5 years ago

Etruska
Member | 25
+
0
-

srigi One service for all files sounds good, however there are some operations which are specific to images like resizing, cropping or making thumbnails. Do you have special service for that or is it within FilesService?

Delegating callbacks to components is nice idea, thanks. Although I'm not sure what you meant by “different behavior depending where form is used”. Do you mean like having one form that calls Component A and with different configuration calls Component B when submitted?

5 years ago

srigi
Member | 560
+
0
-

Etruska wrote:

srigi One service for all files sounds good, however there are some operations which are specific to images like resizing, cropping or making thumbnails. Do you have special service for that or is it within FilesService?

For images create a standalone service and use composition (pass FilesService into ImagesService) to do things. Main idea of single files table is, that any business relation "file <- to -> thing" is represented by intermediate M:N table

  • user has avatar(s), filesavatars → users
  • invoices, filesinvoice_fileinvoices

Delegating callbacks to components is nice idea, thanks. Although I'm not sure what you meant by “different behavior depending where form is used”. Do you mean like having one form that calls Component A and with different configuration calls Component B when submitted?

Idea is that you can have (for example) one upload form. And it is doing different thing (by callback handler in Controller) in gallery and different thing in secured administration. Its purpose is only provide upload functionality and deliver uploaded data to some operator.

5 years ago

srigi
Member | 560
+
0
-

Good resource of best practices is sources of official addons site

5 years ago

Etruska
Member | 25
+
0
-

Okay, I get it now. The “file ← to → thing” decomposition seems like a good idea. Thank you for your tips :)

5 years ago

Etruska
Member | 25
+
0
-

srigi I'm going through the code of addons application and it's great source of inspiration. But I can't try out the application itself, because I don't have any data in DB (like users or users_group tables). Is there any SQL dump with some basic data so I don't have to fill it manually and debug?

5 years ago

srigi
Member | 560
+
0
-

There is only basic migration script, see Readme of that github project for details. But if you want some real data, you should contact some of the leading contributors.