Categories C#

Global Using Directives in C# 10

We are going to learn here about Global Using Directives in C#. We remove unnecessary verbosity in C# Code.

Let’s dive into a realtime example using my project.

API Controller Before using Globalusings.cs

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Common.Interfaces.Persistance.BusinessRequirement;
using SCM.Domain.Authenticate;
using SCM.Domain;
using SCM.WebAPI.Custom.Interface;
using SCM.Domain.BusinessRequirements;

namespace SCM.WebAPI.Controllers.BusinessRequirement
{   
    [ApiController, ApiVersion("1.0"), Route("[controller]"), Authorize]
    public class BusinessRequirementDocumentController : ControllerBase
    {
        private readonly ILogger<BusinessRequirementDocumentController> _logger;
        private readonly IBusinessRequirementDocumentServiceRepository _repository;
        private readonly IRequestSchemeService _requestschemeservice;
    ...
C#

Add a new class file “GobalUsings.cs”

GlobalUsings.cs file

global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Mvc;

global using SCM.WebAPI.Custom.Interface;

global using SCM.Domain.Authenticate;
global using SCM.Domain;
global using SCM.Domain.BusinessRequirements;

global using SCM.Application.Common.Interfaces.Persistance.BusinessRequirement;
C#

API Controller After using Globalusings.cs

Removing the unnecessary directive references from API Controller file, since we already added it to the GlobalUsings.cs file.

namespace SCM.WebAPI.Controllers.BusinessRequirement
{   
    [ApiController, ApiVersion("1.0"), Route("[controller]"), Authorize]
    public class BusinessRequirementDocumentController : ControllerBase
    {
        private readonly ILogger<BusinessRequirementDocumentController> _logger;
        private readonly IBusinessRequirementDocumentServiceRepository _repository;
        private readonly IRequestSchemeService _requestschemeservice;
C#

Written By

With over a decade of experience in .NET technologies, SQL Server, and Agile methodologies, I am a Lead Software Developer at TEKsystems, where I design, develop, and support web-based applications for a leading market research company. I hold multiple certifications in LINQ, Entity Framework, and other .NET frameworks and tools, demonstrating my proficiency and commitment to continuous learning.

My core competencies include .NET Core, User Defined Functions, Stored Procedures, MVC, Web API, jQuery, and Bootstrap. I have successfully delivered several projects, such as a dashboard for analyzing consumer behavior, a portal for managing surveys and reports, and a tool for automating data quality checks. I am passionate about creating innovative and user-friendly solutions that help clients make informed business decisions. As part of a collaborative and agile team, I contribute to improving the quality, performance, and security of web applications, as well as providing technical support and documentation.

Email: codewithsivablog@gmail.com

More From Author

2 comments

Jaisankar says:

Great, global is handy and avoiding verbose. Good work keep it up. Congratulations .

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like