Ramstack.FileProviders.Composition 2.1.0-preview.3

This is a prerelease version of Ramstack.FileProviders.Composition.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Ramstack.FileProviders.Composition --version 2.1.0-preview.3                
NuGet\Install-Package Ramstack.FileProviders.Composition -Version 2.1.0-preview.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ramstack.FileProviders.Composition" Version="2.1.0-preview.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ramstack.FileProviders.Composition --version 2.1.0-preview.3                
#r "nuget: Ramstack.FileProviders.Composition, 2.1.0-preview.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ramstack.FileProviders.Composition as a Cake Addin
#addin nuget:?package=Ramstack.FileProviders.Composition&version=2.1.0-preview.3&prerelease

// Install Ramstack.FileProviders.Composition as a Cake Tool
#tool nuget:?package=Ramstack.FileProviders.Composition&version=2.1.0-preview.3&prerelease                

Ramstack.FileProviders.Composition

Represents a .NET library that provides a helper class for flattening and composing IFileProvider instances.

Getting Started

To install the Ramstack.FileProviders.Composition NuGet package in your project, run the following command:

dotnet add package Ramstack.FileProviders.Composition

Flattening Providers

The FlattenProvider method attempts to flatten a given IFileProvider into a single list of file providers.

This is especially useful when dealing with nested CompositeFileProvider instances, which might have been created during different stages of a pipeline or configuration. Flattening helps in removing unnecessary indirectness and improving efficiency by consolidating all file providers into a single level.

var builder = WebApplication.CreateBuilder(args);

// Application pipeline configuration
...

builder.Environment.ContentRootFileProvider = FileProviderComposer.FlattenProvider(
    builder.Environment.ContentRootFileProvider);

Composing Providers

The ComposeProviders method combines a list of IFileProvider instances into a single IFileProvider. During this process, all encountered CompositeFileProvider instances recursively flattened and merged into a single level. This eliminates unnecessary indirectness and streamline the file provider hierarchy.

string packagesPath = Path.Combine(environment.ContentRootPath, "../Packages");
string themesPath   = Path.Combine(environment.ContentRootPath, "../Themes");

environment.ContentRootFileProvider = FileProviderComposer.ComposeProviders(
    // Inject external Modules directory
    new PrefixedFileProvider("/Packages", new PhysicalFileProvider(packagesPath)),

    // Inject external Themes directory
    new PrefixedFileProvider("/Themes", new PhysicalFileProvider(themesPath)),

    // Current provider
    environment.ContentRootFileProvider);

In this example, the ComposeProviders method handles any unnecessary nesting that might occur, including when the current environment.ContentRootFileProvider is a CompositeFileProvider. This ensures that all file providers merged into a single flat structure, avoiding unnecessary indirectness.

Supported versions

Version
.NET 6, 7, 8

Contributions

Bug reports and contributions are welcome.

License

This package is released as open source under the MIT License. See the LICENSE file for more details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.1 130 9/14/2024
2.2.0 95 9/14/2024
2.1.0 108 8/28/2024
2.1.0-preview.3 52 8/27/2024