You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Question about Internal Handler Support in Modular Monolith Architecture
Background
After reading issue #216 about modular monolith support, I decided to explore implementing a modular monolith architecture with this library. During my exploration, I encountered an interesting challenge with internal handler classes that I'd like to discuss.
Modular Monolith Structure
I'm implementing a modular monolith where each business domain is encapsulated in its own module/project, following these principles:
Each module contains its own handlers and business logic
Modules should encapsulate their implementation details (using internal classes)
The API layer orchestrates between modules through the mediator pattern
// Application/Features/SomeFeature/SomeHandler.csnamespaceApplication.Features.SomeFeature;internalsealedclassSomeHandler:IRequestHandler<SomeRequest,SomeResponse>{// This works because handler and generated code are in the same assembly}
Key differences:
Handlers are typically in the Application layer
Mediator.SourceGenerator is also in the Application layer
Generated code and handlers are in the same assembly
No cross-assembly visibility issues
Questions for the Author
Is this behavior intentional for modular monoliths? Should handlers always be public when implementing a modular monolith architecture, or is this a limitation that could be addressed?
Modular monolith best practices: Based on issue Modular Monolith support #216 discussions, what's the recommended approach for modular monoliths with this library? Should we:
Always use public handlers (sacrificing encapsulation)?
Use InternalsVisibleTo (current workaround)?
Structure projects differently?
Consider a different registration approach per module?
Future enhancements: Would you consider adding better support for modular monoliths? For example:
Generating registration code in each module's assembly
Providing a configuration option to handle cross-assembly internal types
Module-specific mediator instances that can be composed
Performance considerations: In a modular monolith with many modules, does the current approach of scanning all assemblies have any performance implications during startup?
This question builds upon the modular monolith discussion in #216. While that issue focused on general modular monolith patterns, this specific case highlights a practical challenge when trying to maintain proper encapsulation (using internal classes) in a modular monolith architecture.
Additional Context
Using Mediator version: 3.1.0-preview.22
Target framework: .NET 10.0
The InternalsVisibleTo workaround works but feels like it breaks encapsulation principles
Thank you for this excellent library! Looking forward to your thoughts on this architectural scenario.
Question about Internal Handler Support in Modular Monolith Architecture
Background
After reading issue #216 about modular monolith support, I decided to explore implementing a modular monolith architecture with this library. During my exploration, I encountered an interesting challenge with
internalhandler classes that I'd like to discuss.Modular Monolith Structure
I'm implementing a modular monolith where each business domain is encapsulated in its own module/project, following these principles:
internalclasses)Project Structure
IoC Registration in API Project
Project References
The Issue
When I try to use
internal classfor handlers in modules:I get a compilation error:
Root Cause Analysis
After investigating the source code, I found that:
Apiproject, but handlers are inModule1projectApiproject cannot accessinternaltypes fromModule1projectCurrent Workaround
I can solve this by adding
InternalsVisibleToin each module:This allows the generated code in the
Apiproject to accessinternalhandlers from the modules.Comparison with Clean Architecture
In clean architecture projects (like your samples), this works fine because:
Key differences:
ApplicationlayerMediator.SourceGeneratoris also in theApplicationlayerQuestions for the Author
Is this behavior intentional for modular monoliths? Should handlers always be
publicwhen implementing a modular monolith architecture, or is this a limitation that could be addressed?Modular monolith best practices: Based on issue Modular Monolith support #216 discussions, what's the recommended approach for modular monoliths with this library? Should we:
publichandlers (sacrificing encapsulation)?InternalsVisibleTo(current workaround)?Future enhancements: Would you consider adding better support for modular monoliths? For example:
Performance considerations: In a modular monolith with many modules, does the current approach of scanning all assemblies have any performance implications during startup?
Related to Issue #216
This question builds upon the modular monolith discussion in #216. While that issue focused on general modular monolith patterns, this specific case highlights a practical challenge when trying to maintain proper encapsulation (using
internalclasses) in a modular monolith architecture.Additional Context
3.1.0-preview.22.NET 10.0InternalsVisibleToworkaround works but feels like it breaks encapsulation principlesThank you for this excellent library! Looking forward to your thoughts on this architectural scenario.