File-Scoped Namespaces¶
C# 10.0 ile gelen file-scoped namespace özelliği, her dosyada yalnızca bir namespace tanımlamanıza olanak tanır ve gereksiz girintiyi ortadan kaldırır. Ancak, bazı durumlarda doğru kullanım önemlidir.
1. Gereksiz Girinti ile Namespace Kullanımı¶
❌ Yanlış Kullanım: Klasik namespace tanımı ile tüm kodu bir seviye daha girintili yazmak.
namespace MyProject.Services
{
public class ProductService
{
public void GetAll()
{
// ...
}
}
}
✅ İdeal Kullanım: File-scoped namespace ile girintiyi azaltın.
namespace MyProject.Services;
public class ProductService
{
public void GetAll()
{
// ...
}
}
2. Bir Dosyada Birden Fazla Namespace Tanımlamak¶
❌ Yanlış Kullanım: File-scoped namespace ile aynı dosyada birden fazla namespace tanımlamaya çalışmak.
namespace MyProject.Models;
namespace MyProject.DTOs; // Derleme hatası!
public class ProductDto { }
✅ İdeal Kullanım: Her dosyada yalnızca bir namespace kullanın. Farklı namespace’ler için ayrı dosyalar oluşturun.
// Models/Product.cs
namespace MyProject.Models;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
// DTOs/ProductDto.cs
namespace MyProject.DTOs;
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
}
3. Klasik ve File-Scoped Namespace’i Karıştırmak¶
❌ Yanlış Kullanım: Aynı projede bazı dosyalarda klasik, bazılarında file-scoped namespace kullanmak.
// ProductService.cs
namespace MyProject.Services;
public class ProductService { }
// OrderService.cs
namespace MyProject.Services
{
public class OrderService { }
}
✅ İdeal Kullanım: Proje genelinde tutarlı bir stil tercih edin. .editorconfig ile bunu zorunlu hale getirin.
# .editorconfig
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
// ProductService.cs
namespace MyProject.Services;
public class ProductService { }
// OrderService.cs
namespace MyProject.Services;
public class OrderService { }
4. Nested Sınıflarda Yanlış Kullanım¶
❌ Yanlış Kullanım: File-scoped namespace’in nested sınıfları etkileyeceğini düşünmek.
namespace MyProject.Services;
// File-scoped namespace yalnızca dosya seviyesinde çalışır
// Nested sınıflar hâlâ normal şekilde tanımlanır
public class OrderService
{
// Bu sınıf MyProject.Services.OrderService.OrderItem olarak erişilir
public class OrderItem
{
public string ProductName { get; set; }
public int Quantity { get; set; }
}
}
✅ İdeal Kullanım: Nested sınıflar yerine ayrı dosyalar ve aynı namespace altında bağımsız sınıflar tercih edin.
// OrderService.cs
namespace MyProject.Services;
public class OrderService
{
public void ProcessOrder(OrderItem item)
{
// ...
}
}
// OrderItem.cs
namespace MyProject.Services;
public class OrderItem
{
public string ProductName { get; set; }
public int Quantity { get; set; }
}
5. EditorConfig ile Kod Stili Zorunlu Kılma¶
Proje genelinde file-scoped namespace kullanımını .editorconfig ile zorunlu hale getirebilirsiniz.
# .editorconfig
[*.cs]
# File-scoped namespace tercih et (suggestion, warning veya error)
csharp_style_namespace_declarations = file_scoped:warning
Severity seviyeleri:
| Seviye | Açıklama |
|---|---|
suggestion |
IDE’de öneri olarak gösterilir |
warning |
Derleme sırasında uyarı verir |
error |
Derleme sırasında hata verir, kod derlenmez |
6. Mevcut Projeyi Dönüştürme¶
Mevcut bir projeyi file-scoped namespace’e dönüştürmek için Visual Studio veya dotnet format kullanabilirsiniz.
# dotnet format ile otomatik dönüştürme
dotnet format --diagnostics IDE0161
Not: Dönüştürme öncesinde .editorconfig dosyanızda csharp_style_namespace_declarations = file_scoped ayarının tanımlı olduğundan emin olun.