Scoped C# - What you need to know (Guide)
Tags: csharp, dotnet, tutorial, programming
Published: 2023-09-11 10:48:47

Hey there, C# enthusiasts and pros alike! Buckle up as we dive deep into the ocean of C# to uncover the true essence of ‘Scoped C#’. This journey promises to be exciting, immersive, and informative. Just like Captain Nemo’s Nautilus, we are heading for a deep dive. Ready?

## Understanding the Concept of Scoped C# #

Scoped C# is a versatile feature that allows programmers to manage the lifetimes of objects, encapsulating them within blocks of code or methods. The main essence of ‘Scoped C#’ lies in controlling the accessibility and duration of variables and objects. Let’s dig deeper into this concept, unveiling the myriad facets of it as we go along.

A scope in C# can be defined as a block of code where a variable or a function has its existence. It’s like a house where your variables and functions live. When we talk about scoped C#, it’s about how and where in the ‘house’ these variables and functions are defined and used.

```csharp
//class-level scope: variable can be accessed by all methods within the class
public class SampleClass
{
    int classLevelVariable = 1;

    void MethodOne()
    {
        int methodOneVariable = classLevelVariable; // OK
    }

    void MethodTwo()
    {
        int methodTwoVariable = classLevelVariable; // Also OK
    }
}
```

Here, classLevelVariable is defined at the class level and hence, its scope extends to all methods within the SampleClass.

### Importance of Scoped C# #

Understanding C# scoping is imperative for any C# developer. Not only it helps us control the visibility and lifespan of your variables and objects, but it also aids in maintaining code neatness and reducing errors.

When we talk about scoping, variables are like your belongings and scopes are like rooms in a house. You wouldn’t want your personal belongings scattered all over the house, would you? The same concept applies in programming. Scoped C# ensures that each variable and object stays confined to its appropriate block (room), facilitating organized and error-free code.

```csharp
public void SampleMethod()
{
    // Local scope inside a method
    int localVariable = 1;

    if (true)
    {
        // Block scope inside the 'if' statement
        int blockVariable = localVariable; // OK
    }

    // Error: blockVariable doesn't exist here
    int anotherVariable = blockVariable; 
}
```

In this example, the blockVariable is defined within the ‘if’ block and is not accessible outside it. Trying to use blockVariable outside the ‘if’ block results in an error. This prevents misuse and creates clear, compartmentalized (or “scoped”) code.

```csharp
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Perform database operations
    var command = new SqlCommand(queryString, connection);
    command.ExecuteNonQuery();
} 
// Connection is automatically closed as we leave the defined scope
```

Here, the scope is clearly defined for the SqlConnection object. The database connection is automatically closed once we step out of the using block. This way, Scoped C# not only makes your code manageable and efficient but also ensures optimal use of resources by automatically handling the disposal of objects saving you from potential memory leaks.

As you can see, learning the ropes of scoping in C# is more than just the icing on the cake — it’s more like the entire recipe!

## Introduction to AddScoped C# #

The term ‘AddScoped C#’ might seem like techno-jargon at first, but once you understand it, you’ll see it’s rather straightforward. It’s a method that comes from ASP.NET Core’s built-in service container. It is used for setting up services to be used via dependency injection.

When we say AddScoped, it means a new instance of the specified service is created and shared within each individual request to the application. That’s how it helps in managing resource utilization and isolation during the processing of HTTP requests.

Here’s a little secret: _AddScoped_ is like the tailor of the programming world – just like a tailor stitches each outfit uniquely for each customer order, AddScoped creates a unique service instance for each unique request.

```csharp
public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IMyService, MyService>();
}
```

By invoking ‘AddScoped’ in the ‘ConfigureServices’ method, you’re telling .NET to add a scoped service of type ‘IMyService’ that, when requested, will return an instance of ‘MyService’.

### Use Cases of AddScoped C# #

It’s easy to get lost in the world of codes and methods. That’s why it’s important to understand where and how to use our newly acquired AddScoped knowledge. In C#, whenever we need to maintain a consistent behavior within a single transaction or a single HTTP request, that’s where AddScoped comes into play. Let’s say we are processing an HTTP Request in an MVC application and we have a service that retrieves user data from a database.

```csharp
services.AddScoped<IUserDataService, UserDataService>();
```

This code registers the UserDataService using an interface IUserDataService with a scoped lifetime. This means, within the scope of a single request, whenever IUserDataService is injected or used, it will provide the same instance of UserDataService. This ensures data consistency within a single transaction or request.

Let’s look at another scenario. Suppose you have a shopping cart functionality and you want each user or client to maintain their own cart-contents during their session or while their request is being processed.

```csharp
services.AddScoped<IShoppingCartService, ShoppingCartService>();
```

Here, the ShoppingCartService instance created for each unique request (or user) will be different, ensuring a unique, private cart for each user.

In essence, AddScoped proves to be a game-changer in scenarios demanding consistency per request and user-specific behavioral patterns. It simplifies resource management and enables smoother application performance. It’s like having a personal assistant entrusted with the task of remembering and managing specific chores for you; it’s that efficient!


### Implementing C# File Scoped Namespace in Projects

Time to dive into the practical world and iron out the theory with some concrete examples of file scoped namespace.

The traditional namespace declaration looked something like this:

```csharp
namespace Project
{
    class MyClass
    {
    }
}
```

In the above lines of code, we’ve created a namespace ‘Project’ and a class ‘MyClass’. You can see ‘MyClass’ is indented as it is inside the namespace ‘Project’ braces.

While nothing is wrong with this approach, C# 10 presents the file-scoped namespace, allowing for a sleeker declaration.

```csharp
namespace Project;
class MyClass
{
}
```

As you can notice, the class declaration is at the same indent level as the namespace declaration itself. Not a single character more than necessary. You can immediately notice how it removes the extra indentations and reduces the clutter in the code, making it more readable.

This is the power of the “C# file scoped namespace”. It serves to be a crucial organizational tool that works wonders for larger files where there are multiple classes, as it significantly cuts down on unnecessary indentation.

Imagine having a large piece of code that spans multiple pages; decluttering and tidiness that file-scoped namespace offers can be a real lifesaver. The less clutter there is, the easier it is to read and maintain the code. So next time you’re working on a large project, consider using the file-scoped namespace feature to give your code a cleaner and neater look and enhance readability.

C# Scoped Services form a significant part of the ASP.NET core services. Generally, three types of service lifetimes exist in ASP.NET Core: Singleton, Transient, and Scoped. Scoped services hold a unique position as they are created once per client request (connection). This allows you to share resources within a request, making your programming more efficient.

### Advantages of C# Scoped Services

There are several advantages to using Scoped services in C#. Here are a few of them with corresponding code examples.

-   **Share Resources** – Scoped services allow you to instantiate a service once and share it across your application. This can be advantageous when several parts of complex applications need to communicate with each other, as shown below:

```csharp
public void ConfigureServices(IServiceCollection services)
{
    // Registering our service with AddScoped 
    services.AddScoped<IMyService, MyService>();
    // Now MyService can be shared and utilized within the application scope
}
```

In this code example, we’ve added to the collection of services. This service can now be shared and utilized within the application scope.

-   **Less Memory Consumption** – Since the service is created once for each user request, rather than each call within the request, there is less memory consumption as compared to Transient services. This aids in creating applications that are memory efficient.

```csharp
public class MyController : Controller
{
    private readonly IMyService _myService; // Scoped service instance

    // The instance is injected through a constructor
    public MyController(IMyService myService)
    {
        _myService = myService;
    }

    public IActionResult Index()
    {
        _myService.DoThings(); // Usage of the same instance initiated per request
        return View();
    }
}
```

In this code example, any usage of the object in an action method would refer to the same instance of throughout the entire request.

-   **Better Data Consistency** – If a service is retrieving and working on data, Scoped services provide better data consistency since the same instance of the service is used through a single request.

```csharp
public class MyActionFilter : IActionFilter
{
    private readonly IMyDataService _myDataService;

    public MyActionFilter(IMyDataService myDataService)
    {
        _myDataService = myDataService;
    }

    public void OnActionExecuting(ActionExecutingContext context)
    {
        var data = _myDataService.RetrieveData();
        // The same instance of MyDataService is used for retrieving data
    }

    public void OnActionExecuted(ActionExecutedContext context) { }
}
```

In this case, the instance of is kept consistent throughout the request, ensuring that all data operations are synchronized.

However, just like the two sides of a coin, C# Scoped Services also have certain limitations. So be aware and conscious of how and where you are using them.

### Disadvantages of C# Scoped Services

-   **Not Suitable for Multi-Threaded Environments** – In a multi-threaded environment, Scoped services can lead to problems since they are not thread safe. Multiple threads can potentially modify service data simultaneously, creating a data inconsistency.
-   **Limited Lifetime** – Scoped services have a lifetime only for the duration of a single request. The service must be created again for every new request, which can possibly introduce additional computation.
-   **Potential Memory Leaks** – If Scoped services are not properly disposed of, they can create memory leaks. The developer must ensure that the services are correctly disposed of after the request finishes.

What’s important to take away from this discussion is that choosing scopes for your services is critical. Their choice and utilization depends on the lifecycle required, whether they involve database communication, or whether concurrent access is possible, among others. In conclusion, a [solid](https://www.bytehide.com/blog/solid-principles-in-csharp) understanding of Scoped services and their applicability in C# programming will certainly allow more efficient and coherent coding practices.

### A Closer Look at C# CreateScope Method

As a C# developer, frequently you might find yourself in a situation where you need to create your own scope for services. This is particularly the case when you’re handling background tasks or hosted services. The in C# is an apt method for such situations.

### Boost Your Coding Practices with CreateScope C# #

The ‘CreateScope’ method provided by the .NET core’s built-in IoC container enables you to create a new DI (Dependency Injection) scope within your application. Here’s a simple code snippet to illustrate this:

```csharp
    using (var scope = serviceProvider.CreateScope())
    {
        var service = scope.ServiceProvider.GetService<MyService>();
        // Utilize the service
    }
```

In the code snippet above, the method is employed to orchestrate a new Dependency Injection (DI) scope. Within this scope, we can call to access any scoped services. The statement ensures our scope is properly disposed of at the end of the block.

So I guess our journey is coming to an end, my friend. This tour of the awesome island of ‘Scoped C#’ must have been enriching, wasn’t it? As it’s said, knowledge is best put to use when shared. So, why not pass along the wisdom and see the magic of ‘Scoped C#’ spreading in the coding universe. Happy coding!