[Proposal] Update Namespaces to Match Folder Structure
See original GitHub issueUpdate Namespaces to Match Folder Structure
- Proposed
- Prototype
- Implementation: Not Started
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests: Not Started
- Sample: Not Started
- Documentation: Not Started
Link to Discussion
Discussion on Namespaces Matching Folder Structure
- https://github.com/CommunityToolkit/Maui/pull/214/files#r768099127
Discussion on Moving Enums to a folder called
Enums
, and updating their namespaces accordingly - https://github.com/CommunityToolkit/Maui/pull/214/files#r767382464
Summary
This Proposal solves two problems:
- Ensures a file’s namespace matches its folder hierarchy
- Moves Enums to their own File in an
Enums
folder
Because this is a breaking change that requires XAML users to add additional xmlns
, this Proposal will be placed on hold until a Toolkit-wide namespace is introduced in #144, e.g. xmlns:toolkit="http://schemas.microsoft.com/dotnet/2021/maui/tookit"
.
Motivation
Currently, both CommunityToolkit.Maui
and CommunityToolkit.Maui.Core
use the root folder for their namespace, but nested folders are not included.
E.g. /Behaviors/EventToCommandBehavior.shared.cs
uses namespace CommunityToolkit.Maui.Behaviors;
E.g. /Behaviors/Validators/CharactersValidationBehavior.shared.cs
also uses namespace CommunityToolkit.Maui.Behaviors;
This root-folder namespace matches the precedent set in Xamarin.CommunityToolkit
, however, we would like to follow the industry-standard of matching the folder hierarchy.
Detailed Design
- Update Namespaces to match folder structure
- E.g.
/Behaviors/Validators/CharactersValidationBehavior.shared.cs
will becomenamespace CommunityToolkit.Maui.Behaviors.Validators;
- E.g.
- Add
IDE0130
toDirectoryBuild.props
<WarningsAsErrors>nullable,CS1574,IDE0130</WarningsAsErrors>
- This ensures that all future PRs will have namespaces that match the file’s folder hierarchy
- Create
Enums
Folder, and move eachenum
types into their own file in this folder - Currently, all single-use Enums are placed in the same file as the class that uses them
Drawbacks
- Breaks precedence set by Xamarin.CommunityToolkit
- Will require XAML users to set an additional
xmlns
- This is averted by holding this Proposal until #144 is completed
- Will require C# users to declare an additional
using CommunityToolkit.Maui.*
namespace
Alternatives
There are two other alternatives:
- Matching the Assembly namespace
- Every file in
CommunityToolkit.Maui
would usenamespace CommunityToolkit.Maui
- Every file in
CommunityToolkit.Maui.Core
would usenamespace CommunityToolkit.Maui.Core
- Matching
Xamarin.CommunityToolkit
’s Precedence of Root-Folder Namespaces
Unresolved Questions
None that I can think of
Issue Analytics
- State:
- Created 2 years ago
- Comments:28 (17 by maintainers)
Top GitHub Comments
Good question!
Looks like there are 3 Options being discussed:
CommunityToolkit.Maui
would usenamespace CommunityToolkit.Maui
CommunityToolkit.Maui.Core
would usenamespace CommunityToolkit.Maui.Core
Xamarin.CommunityToolkit
’s Precedence of Root-Folder NamespacesLet’s do a vote:
Edit: Adding
EmailValidationBehavior
Example 👇Option 1 ❤️
Option 2 🚀
Option 3 👀
In the Windows Community Toolkit we’ve found that flattening namespaces down helps with reducing the amount of usings required in discovering what’s available to use. This is also important for anything that needs to be included in XAML as having a bunch of xmlns is confusing and unwieldly, so we’ve been trying to reduce that as well.
@Sergio0694 any insights from the .NET Community Toolkit you’d want to share?