Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to the "using" directive keyword in C#?

I just finished watching an episode of Bob Martin at NDC where he said "using" directives in C# at the top of a page are bad because of the tight coupling they create/imply between components.

What way are there to use external .dlls without adding a project reference and a using statement?

I remember V6 used to let you create an object by the string of the ProgId--I'm not sure that's the technique I'm looking for, but it's an example of a language that didn't need a project reference to use a dll.

EDIT: Here is a link to the conference. Sorry I don't have the exact quote or minute in the presenation, I'm going by memory.

like image 507
MatthewMartin Avatar asked Sep 10 '09 11:09

MatthewMartin


People also ask

What is the purpose of the using directive?

The primary function of the using directive is to make types within a namespace available without qualification to the user code. It considers the set of namespaces and types which are defined in referenced assemblies and the project being compiled.

What is using () in C#?

The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.

What is global using in C#?

The global using directive is a new feature that was recently added to C# language in C# version 10.0. This feature allows developers to declare a namespace globally in any one of the files in a project and the global declared namespace is imported and available to all files in the application.

What is the use of using statement in C# with example?

The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.


2 Answers

I believe Bob Martin is actually referring to early versus late binding.

In .NET late binding is possible through reflection and more specifically the Activator class that allows creation of a type in an external assembly using a filename or assembly name.

Normally, using directives (not the using statement) go hand in hand with directly referencing an external assembly. ie. You add a reference to an assembly and then add using directives to avoid needing to type the full namespace hierarchy when you use the external types.

So if you find your code has a large number of using directives at the top, it is possible that you are referencing many other types directly and so increasing the coupling/dependency of your code on these types.

I would guess this is why Bob is referring to them as bad. The answer to the question "is this actually bad?" is a very subjective and context dependent one.

In general though, de-coupling of components is almost always a good goal to aim for in designing software. This is because it allows you to change parts of your system with minimal impact on the rest of the system. Having read one or two of Bob Martins books, I would expect this is what he is getting at.

like image 105
Ash Avatar answered Sep 30 '22 19:09

Ash


It's not the using statement itself that is bad - it's if you get too many of them.

A statement such as using System; is rarely a problem in itself, but if you have lots (I'd say more than 3-6, depending on which ones) in the same code file, it could be an indication of tight coupling.

You could just as well apply a similar rule of thumb to the number of references in a project itself.

The solution to tight coupling is programming to interfaces and Dependency Injection (DI).

The ProgId way of doing things that you can remember from VB was simply COM in action. In essence, you used that ProgId to get a reference to an instance that implemented the desired interface. The downside was that this only worked when the COM object was universally registered. Remember dll hell?

You can still apply the same principle using certain flavors of DI, only that now the interface is a .NET type and not defined in IDL, and you need some sort of DI Container to supply the concrete implementation.

like image 35
Mark Seemann Avatar answered Sep 30 '22 19:09

Mark Seemann