Class FileSystemHostExtensions
- Namespace
- Invex.FileSystem
- Assembly
- Invex.FileSystem.dll
Provides extension methods for registering the file system services with an Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static class FileSystemHostExtensions
- Inheritance
-
objectFileSystemHostExtensions
Methods
AddRootedFileSystem(IServiceCollection)
Registers the file system services with the dependency injection container.
public static IServiceCollection AddRootedFileSystem(this IServiceCollection serviceCollection)
Parameters
serviceCollectionIServiceCollection
Returns
- IServiceCollection
The same Microsoft.Extensions.DependencyInjection.IServiceCollection for further configuration.
Remarks
Registers the following services:
-
A keyed singleton
"RootedFileSystem"of type System.IO.Abstractions.IFileSystem backed by the real System.IO.Abstractions.FileSystem. This keyed registration exists so that the inner file system can be resolved independently of the outer IRootedFileSystem wrapper if needed. - A singleton IRootedFileSystem whose IPathProvider list is populated from all IPathProvider services registered in the container, sorted by descending Priority (registration order breaks ties).
- A singleton System.IO.Abstractions.IFileSystem (non-keyed) that resolves to the same instance as IRootedFileSystem, so code that depends on the base abstraction automatically gets the rooted file system implementation.
Register path providers with ProvidePath (or by adding IPathProvider
services directly) either before or after calling this method: providers are collected
from the container when IRootedFileSystem is first resolved, so
registration order relative to this call does not matter.
ProvidePath(IServiceCollection, Func<string, IRootedFileSystem, RootedPath?>, int)
Registers a custom path provider using a delegate that also receives the current IRootedFileSystem instance.
public static void ProvidePath(this IServiceCollection serviceCollection, Func<string, IRootedFileSystem, RootedPath?> locate, int priority = 1)
Parameters
serviceCollectionIServiceCollectionlocateFunc<string, IRootedFileSystem, RootedPath>A function that receives a path key and the resolved IRootedFileSystem and returns a RootedPath when it recognises the key, or
nullto defer to the next provider.priorityintThe priority of the provider. Higher values are queried before lower values; providers with equal priority are queried in registration order. Defaults to
1.
Remarks
Prefer this overload when the path depends on another resolved path, for example:
services.ProvidePath((key, fs) => key == "MyOutput"
? fs.GetPath("Root") / "output"
: null);
The IRootedFileSystem is resolved lazily from the System.IServiceProvider when the provider is first invoked, avoiding circular-construction issues at startup.
ProvidePath(IServiceCollection, Func<string, RootedPath?>, int)
Registers a custom path provider using a simple key-to-path delegate.
public static void ProvidePath(this IServiceCollection serviceCollection, Func<string, RootedPath?> locate, int priority = 1)
Parameters
serviceCollectionIServiceCollectionlocateFunc<string, RootedPath>A function that receives a path key and returns a RootedPath when it recognises the key, or
nullto defer to the next provider.priorityintThe priority of the provider. Higher values are queried before lower values; providers with equal priority are queried in registration order. Defaults to
1.
Remarks
Use the overload that also accepts an IRootedFileSystem parameter when the path logic needs to call GetPath(string) for another key (e.g. to build a path relative to the project root) — doing so through a closure over the raw System.IServiceProvider would risk a circular dependency.