Home > Software design >  Can you share models between features with clean architecture?
Can you share models between features with clean architecture?

Time:09-29

I have the following folder structure for my application

app
    core
    features
        feature1
            domain
                entities
                    entity1
                    entity2
                    entity3
                    entity4
                    entity5
                    entity6
            data
                models
                    model1
                    model2
                    model3
                    model4
                    model5
                    model6
            presentation
        feature2
            domain
                entities
                    entity1
                    entity2
                    entity3
                    entity4
                    entity5
                    entity6
            data
                models
                    model1
                    model2
                    model3
                    model4
                    model5
                    model6
            presentation

Model 1 to 6 for both features are exactly the same and more are coming as the application scales. This is becoming hard to maintain. Does clean architecture allow for sharing models and entities across the multiple features? Would that be done through the core folder?

CodePudding user response:

I'm assuming you're following the enter image description here

Then, inside all of these top-level directories (excluding /core - that's specifically for things like API clients, routers, etc.), there's folders for each feature. Ex: authentication, settings, posting, etc.

Then, here's the important bit, in each of these feature-split directories (ex: /domain, /presentation, etc.) we have a sub-directory called /shared that resembles what each folder needs to look like, except it just contains functionality that's categorized as (example) domain or data. This stuff is then split between all features.

For example, if I have an app that allows users to post content, I'm going to create the post entity (using ResoCoder terminology) inside the /posts feature. Except, UH OH, I need to have it displayed inside the /feed feature as well! This is then a perfect case for /shared inside the general /domain directory.

Let me know if this helps, or if you have any further questions!

CodePudding user response:

Does clean architecture allow for sharing models and entities across the multiple features?

I think DRY principles should be applied whether you use clean architecture or not.

As for the answer:

I think you could abstract your shared models and entities into separate modules or packages. If it's all dart code, I suggest choosing packages. You can place it inside the root project (Monorepo) or separate it to another repository, this way you could achieve modularity by abstracting the all layer of shared dependency (abstract class, interface, clients, or maybe repositories) out of the main application

There is a good video about this topic on Google I/O'19, it is about Android but you can get great insight and applied to general mobile development. I suggest you give it a try

  • Related