angular-architects / nx-ddd-plugin

Nx plugin for structuring a monorepo with domains and layers
313 stars 56 forks source link

Question / Feature Request > generate ngrx code with domain or feature lib #10

Closed pascalbe-dev closed 3 years ago

pascalbe-dev commented 4 years ago

While many apps probably do work fine with simple BehaviorSubjects within the facades, it would be great to be able to scaffold ngrx code when generating a (domain?) library via a flag, so it's easier for developers to generate that code in a consistent manner across a monorepo.

While technically, this shouldn't be too hard by using the nx ngrx schematics, I'm wondering how to structure the code reasonably. Nx recommends having feature level states, however, the domain-driven setup we have with this plugin requires the state of all feature libs for a domain to live inside the domain library. Would you have separate Feature-States within Domain-Feature-Modules and import them in the specific Feature-Modules or would you add all ngrx Stuff to the Domain-Module?

Do you have any examples following some best practices about structuring an ngrx managed state with the library structure of the nx-ddd-plugin? The demo apps all make use of BehaviorSubjects only to manage the state.

manfredsteyer commented 4 years ago

Hi Pascal,

in this case, I would add state management to the domain layer. If it has a facade, the facade shields details of state management from the consumer.

I've implemented this here within a workshop in the booking domain:

https://github.com/manfredsteyer/workshop-fest-2020-05/tree/master/libs/booking/domain/src/lib

Best wishes, Manfred

pascalbe-dev commented 4 years ago

Hi Manfred, it sounds reasonable to hide the state management details behind the facade.

The only thing, which might be a bit of work, if you implement it that way, is handling route navigation. Nx has a utility operator for it which requires having access to the component, which handles the route (see here). Using this utility would not be possible then since we do not want the dependency from domain lib to feature lib implementing the routes and components.

Two options:

I'd love to hear your take on that navigation handling topic and how to cleanly implement that in this context.

Apart from that, I'm also trying to prepare a PR here. Your suggestion with the ngrx option for the domain schematic sounds great.

manfredsteyer commented 4 years ago

I'd go with the 2nd option and call a method provided by the facade in the component or in a resolver.

Looking forward to your PR.

If you need any information, don't hesitate to reach out.

pascalbe-dev commented 3 years ago

Nice, this seems to be added in 1.1.0 👍 great work @peterbsmith2