2

I have quite a big project with various RCLs and each contains a myriad of components that end up being consumed from the root app.

The problem im finding is as I get more components I need to change how they are structured on the file system, which in turn means every project that uses them needs to update the _imports.razor to have the SPECIFIC namespace where the component lives, but its getting really tiresome every time I want to move a component about I need to refactor 5 different projects imports to get it working again.

So is there a way to just have it bulk import off a root namespace or some other mechanism to be able to get away from explicit granular namespacing?

Here is an example of what I mean:

@using Foo.Core.Components;
@using Foo.Core.Components.Elements;
@using Foo.Core.Components.Elements.Select;
@using Foo.Core.Components.Tasks;
@using Foo.Core.Components.Tasks.Utility;
@using Foo.Core.Components.Triggers;
@using Foo.Core.Components.Triggers.Utility;

That is just a small example and that is in multiple projects and they have their own namespaces etc so its all a bit unmanageable at scale, I was hoping I could do something like:

@using Foo.Core.Components.*;

Or some other mechanism to achieve the same thing, as this way the file system layout of the components would have no bearing on their importing.

== Update 1 ==

Just to provide some clarity, there is a similar question here Importing nested namespaces automatically in C# however this is using c# syntax not razor syntax

Given that this razor has a lot of helper methods and is processed differently to regular C# the question was more wondering if there was any special razor helpers/syntax to allow us to do it, regardless of the restrictions of normal c# files.

It seems like there isnt but for anyone else coming here there are 2 work arounds I have used to date:

  1. Give each component a MANUAL namespace i.e @namespace Foo.Core.Component then it doesnt matter what folder structure its in, however this causes issues if you use spiffy refactoring tools as they will try to correct the namespace for you constantly.

  2. Turning off namespace provider options for folders, this one is the nicer way of doing it as it doesnt confuse the refactoring tools, however it needs everyone understanding that each folder within there ideally needs to manually have namespace provider turned off.

This was why I was wondering if there was a 3rd option that would allow us to deal with it at namespace level rather than in the component/projects.

Grofit
  • 17,693
  • 24
  • 96
  • 176
  • Does this answer your question? [Importing nested namespaces automatically in C#](https://stackoverflow.com/questions/9023465/importing-nested-namespaces-automatically-in-c-sharp) – Connor Stoop Aug 10 '22 at 11:06
  • Well it means I cant use a wildcard :D I have 2 work arounds at the moment, one is to add namespaces to each component explicitly making it root level, but that has the downside of refactor > adjust namespaces always tries to fix it. The other work around is making folders not provide namespaces but whoever makes the components needs to remember to do this, so I was just wondering if there was some Razor utility method or syntax which could do it. – Grofit Aug 10 '22 at 11:10
  • I have added a bit more context in the update to say why I don't think this is a duplicate of that question, as that question linked pertains to C# syntax/files not Razor syntax/files, which I appreciate PROBABLY have no difference in functionality however its not outside the realms of possibility that there could be a utility method/helper or some syntax that the razor processor does for you which isnt available in regular C# – Grofit Aug 10 '22 at 11:17
  • I' facing the same issue for a RCL that I want to make public through nuget, and I don't want people to be needing to add 150 usings everytime they want to use one of my components somewhere. Have you managed to find a more "generic" solution? – Angevil Feb 20 '23 at 08:43

0 Answers0