Duplicated using directives in multiple files
Asked Answered
S

2

5

I have got 5 C# files that have 20 using directives in common. I want to get rid of this code duplication, especially because these 20 using directives belong logically together. In C or C++ I would have created an extra header file containing these 20 include files. This extra header file acts as a layer then to include the 20 other files in one go.

Unfortunately I don't know how to do this in C#. Any suggestions?

Symphysis answered 1/7, 2014 at 14:32 Comment(5)
Create a function and call it from everywhere?Gluteus
If these 20 namespaces all "belong" together then why do you have 20 namespaces in the first place? Why is the code not all in the same namespace?Theretofore
@Servy, these are all external namespaces, i.e. namespaces outside my own program.Symphysis
Maybe they represent 20 different databases. Just answer his question.Exodus
Now of course you can use global usings (which were not available when this question was asked).Floriaflorian
E
14

You can't do this in C# since C# does not have a preprocessor, and the C# compiler, although it supports some syntax that looks like preprocessor syntax, is handled by the compiler itself, and the compiler doesn't support include directives.

Also, if even one file actually requires 20 using statements it sounds to me as though you have put too much responsibility into one class. Perhaps this would be better served by refactoring and restructuring the codebase, thus reducing the number of required using directives instead?

Or, you could look at it from a different angle. You say that these 20 using directives all "belong logically together", why then 20 namespaces? Sounds to me as you've spread out some functionality into too many namespaces.

So either way I would look at restructuring the code.

Epineurium answered 1/7, 2014 at 14:34 Comment(3)
+1. (I've changed question to use "directive" instead of "statment")Chanachance
Thanks Lasse. The 20 statements were just to make my point. The actual duplication is 14 statements.Symphysis
Doesn't matter, to me it sounds like you should restructure the code, but this is for you to decide. The real answer here is that the C# compiler does not use a preprocessor, and does not handle include directives. As such, you're either going to have to reduce the number of using directives, or live with the duplication. To be honest, if that's the only thing wrong with these files I would leave it be.Epineurium
I
1

Simple answer. If your code files are using 20 common using statements, they should not be separated into 20 statements. Also, I wouldn't worry that much over that kind of code duplication. It's not hitting your runtime performance in any way, so it's not really related to DRY paradigm.

Indicative answered 1/7, 2014 at 14:36 Comment(3)
I wouldn't say DRY was related to improving runtime performance. It it more related to maintainability, reuse, and having the functionality only defined in a single place. In fact placing common code in a separate class/function is likely to make it slightly less performant as it has to be called from there rather than just compiled inline.Nonu
DRY is most definitely not only linked to runtime performance.Epineurium
In this case you're right, although IDE like VisualStudio is allowing you to hide whole using section and change the namespaces on-the-fly while refactoring, so I wouldn't really bother either. Of coruse it depends heavily on how complicated your project is so my advice may be wrong here. Still, it's a question worth upvoting.Indicative

© 2022 - 2024 — McMap. All rights reserved.