Search code examples
angularangularjs-scope

angular2 when to create a component?


The Project is going too big now. I have created several components some of them are full of code some of them are having less code. I would like to know when do we create a new component and for a component when do we need to move on and create a additional sub component for the additional functionality. Thanks


Solution

  • is opinion based answer. So what will follow is my vision of your question and not universal answer.

    But when we talk about angular and architecture pattern provided by Angular, We can distinct 3 mains cases :

    • Service
    • middleware Component
    • UI Component

    (To keep it simple i will not add to my explication Pipe and Directive)

    Let start by the most easier case, UI Component, is normal component who don't do any traitment, just take Input for configuration and Output some Event. Goal is to have peace of application easy to change / reuse. This part should be strongly uncoupled from your business logic.

    Because you have uncoupled your UI, you need Middleware component or container component (pick up your prefered name :D), who will do the bridge between your UI component and your business Logic.

    And all business logics (and not only your ajax call) have to be embed on your service. Why ? because :

    • you open the possibility to reuse this business logic on another part of your project (Another UI who will need existing method).
    • You have better control of how service is provided inside your application (single instance, many instance, replace existing provider by the new version, ...).
    • Easier cross component communication.
    • Frameworkless implementation (if you remove @Injectable decorator, you can move this class to any other technology)

    If you keep in mind all this "Main category", you can respect SOLID principle.

    If you want to dig more : you have awesome conference from Todd Motto