AlirezaMahDev.Extensions.File
0.0.1-alpha7
dotnet add package AlirezaMahDev.Extensions.File --version 0.0.1-alpha7
NuGet\Install-Package AlirezaMahDev.Extensions.File -Version 0.0.1-alpha7
<PackageReference Include="AlirezaMahDev.Extensions.File" Version="0.0.1-alpha7" />
<PackageVersion Include="AlirezaMahDev.Extensions.File" Version="0.0.1-alpha7" />
<PackageReference Include="AlirezaMahDev.Extensions.File" />
paket add AlirezaMahDev.Extensions.File --version 0.0.1-alpha7
#r "nuget: AlirezaMahDev.Extensions.File, 0.0.1-alpha7"
#:package AlirezaMahDev.Extensions.File@0.0.1-alpha7
#addin nuget:?package=AlirezaMahDev.Extensions.File&version=0.0.1-alpha7&prerelease
#tool nuget:?package=AlirezaMahDev.Extensions.File&version=0.0.1-alpha7&prerelease
AlirezaMahDev.Extensions.File
Project Description
This project provides a robust implementation of file access operations for the AlirezaMahDev.Extensions ecosystem. It implements the interfaces defined in AlirezaMahDev.Extensions.File.Abstractions, offering thread-safe, high-performance file handling with support for dependency injection and configuration.
Key Features
- Thread-Safe File Operations: Implements thread-safe file access with proper locking mechanisms
- Dependency Injection: Seamless integration with .NET's DI container
- Configuration Support: Flexible configuration through
FileOptions - Resource Management: Proper disposal of file handles and resources
- Asynchronous Support: Full async/await pattern implementation
- File Locking: Ensures data integrity with proper file locking
Core Components
FileService
Implements IFileService to provide file access instances:
Access(string name): Gets or creates a file accessor for the specified file- Thread-safe and singleton-scoped by default
FileAccess
Implements IFileAccess to provide thread-safe file operations:
Access(): Performs read operations on the fileReplace(): Replaces the entire file content atomicallyChange(): Modifies file content with a transformation- Implements
IDisposableandIAsyncDisposablefor proper resource cleanup
FileBuilder
Implements IFileBuilder for configuring file services:
- Configures the file storage directory
- Registers required services with the DI container
- Ensures the storage directory exists
Usage Example
Registration
// In Startup.cs or Program.cs
services.AddFile(builder =>
{
builder.OptionsBuilder.Configure(options =>
{
options.Path = "./App_Data/Files";
});
});
Usage in Application
public class MyService
{
private readonly IFileService _fileService;
public MyService(IFileService fileService)
{
_fileService = fileService;
}
public async Task<string> ReadFileContentAsync(string fileName)
{
var fileAccess = _fileService.Access(fileName);
return await fileAccess.AccessAsync(async (stream, ct) =>
{
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync(ct);
});
}
public async Task WriteFileContentAsync(string fileName, string content)
{
var fileAccess = _fileService.Access(fileName);
await fileAccess.ReplaceAsync(async (stream, ct) =>
{
using var writer = new StreamWriter(stream);
await writer.WriteAsync(content);
});
}
}
Dependencies
Project Dependencies
- AlirezaMahDev.Extensions.File.Abstractions: Defines the core interfaces
- AlirezaMahDev.Extensions.ParameterInstance: For parameter instance management
- AlirezaMahDev.Extensions: Core extension infrastructure
Performance Considerations
- File Locking: Uses
FileShare.Noneto prevent concurrent access issues - Resource Management: Implements
IDisposableandIAsyncDisposablefor proper cleanup - Thread Safety: Uses
SemaphoreSlimfor thread synchronization - Caching: Maintains file stream instances for performance
Error Handling
The implementation handles various file system errors and provides appropriate exceptions:
IOException: For file access issuesObjectDisposedException: For operations on disposed objectsUnauthorizedAccessException: For permission issues
Configuration Options
FileOptions
Path: The base directory for storing files (default:./.data)
Example configuration:
{
"File": {
"Path": "./App_Data/Files"
}
}
Best Practices
- Always dispose: Use
usingstatements orawait usingwithIFileAccess - Handle exceptions: Wrap file operations in try-catch blocks
- Use async/await: Prefer asynchronous methods for better scalability
- Configure paths: Set appropriate paths for your environment
- Consider file locking: Be aware of file locking behavior in concurrent scenarios
- AlirezaMahDev.Extensions.File.Abstractions: Implements the file abstractions defined in this project
- AlirezaMahDev.Extensions.ParameterInstance: Uses parameter-based instance factories for managing file access instances
- AlirezaMahDev.Extensions: Inherits base builder implementations for consistent configuration patterns
Why these dependencies?
- File.Abstractions: Provides the contracts that this project implements
- ParameterInstance: Enables efficient caching and reuse of file access objects based on file paths
- Extensions: Provides the builder pattern infrastructure for service registration
Key Components
FileService
The main service implementation that manages file access objects. It uses a factory pattern to create and cache file access instances.
FileAccess
Concrete implementation of IFileAccess, providing actual file I/O operations including:
- Reading and writing file content
- File stream management
- File metadata operations
FileBuilder
Builder implementation for configuring file services in the dependency injection container, following the standard builder pattern.
FileAccessFactory
Factory for creating and managing FileAccess instances with caching support to avoid redundant file operations.
Extension Methods
- FilebaseExtensions: Extension methods for file-related operations
- FileAccessExtensions: Extension methods for
IFileAccessobjects to simplify common file operations
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AlirezaMahDev.Extensions (>= 0.0.1-alpha7)
- AlirezaMahDev.Extensions.File.Abstractions (>= 0.0.1-alpha7)
- AlirezaMahDev.Extensions.ParameterInstance (>= 0.0.1-alpha7)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AlirezaMahDev.Extensions.File:
| Package | Downloads |
|---|---|
|
AlirezaMahDev.Extensions.File.Data
Package Description |
|
|
AlirezaMahDev.Extensions.File.Json
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-alpha7 | 408 | 12/8/2025 |
| 0.0.1-alpha | 273 | 12/7/2025 |