Skip to content

JeevanJames/ContentProvider

Repository files navigation

Content Provider

The ContentProvider framework provides a mechanism for retrieving string and binary resources from various sources such as files, embedded resources, etc.

Build status Test status

[[TOC]]

NuGet packages

Name Links Description
ContentProvider NuGet Version NuGet Downloads Core package.
ContentProvider.Extensions.DependencyInjection NuGet Version NuGet Downloads Additional support dependency injection based on Microsoft.Extensions.DependencyInjection.
ContentProvider.Formats.Json NuGet Version NuGet Downloads Extensions to support JSON data.

Getting started

The first step is to register one or more content sets. A content set represents a set of data that can be retrieved from one or more content sources, such as a file, embedded resource, REST API, etc.

The framework provides two ways to register content sets, depending on the type of application:

  • ContentManager: This class is provided in the core ContentProvider library. You will typically use the predefined singleton instance ContentManager.Global. Creating new instances is only for advanced scenarios and mocking during testing.

    ContentManager provides multiple Register method overloads to register content sets.

    To retrieve content sets later on, use the GetContentSet method overloads provided on the same ContentManager instance used to register them.

  • As dependency injection services: The ContentProvider.Extensions.DependencyInjection library provides AddContent extensions on IServiceCollection to register content sets as injectable services.

    To retrieve content sets later on, use dependency injection.

Registering a named content set

A named content set is an untyped content set and is represented by the IContentSet interface that provides core methods to retrieve content.

// Using ContentManager.Global
ContentManager.Register("DbScripts", builder => builder
    .From.FilesInCurrentDirectory(o => o.WithFileExtension("sql"))
    .ThenFrom.ResourcesInExecutingAssembly(o => o.WithFileExtension("sql")));

// Using IServiceCollection
services.AddContent("DbScripts", builder => builder
    .From.FilesInCurrentDirectory(o => o.WithFileExtension("sql"))
    .ThenFrom.ResourcesInExecutingAssembly(o => o.WithFileExtension("sql").WithResourceNamespace("MyNamespace")));

Retrieving a named content set will get you a IContentSet instance.

// Using ContentManager.Global
IContentSet dbScripts = ContentManager.Global.GetContentSet("DbScripts");

// Using dependency injection (IServiceProvider)
IContentSet dbScripts = serviceProvider.GetRequiredKeyedService<IContentSet>("DbScripts");

// Using dependency injection (constructor)
public MyClassCtor([FromKeyedServices("DbScripts")] IContentSet dbScripts) {}

Registering a type content set

A typed content set represents a custom content set class that derives from ContentSet. It can be used to avoid the magic string approach of named content sets and also provide strongly-typed methods to access content (discussed later).

// Simple type content set example
public sealed class DbScriptsContentSet : ContentSet;

Registering a type content set:

// Using ContentManager.Global
ContentManager.Register<DbScriptsContentSet>(builder => builder
    .From.FilesAndResources(o => o
        .WithFileExtension("sql")
        .WithResourceNamespace<DbScriptsContentSet>()));

// Using IServiceCollection
services.AddContent(<DbScriptsContentSet>(builder => builder
    .From.FilesAndResources(o => o
        .WithFileExtension("sql")
        .WithResourceNamespace<DbScriptsContentSet>()));

Retrieving a typed content set will get you an instance of the custom content set class.

// Using ContentManager.Global
DbScriptsContentSet dbScripts = ContentManager.Global.GetContentSet<DbScripts>();

// Using dependency injection (IServiceProvider)
DbScriptsContentSet dbScripts = serviceProvider.GetRequiredService<DbScriptsContentSet>();

// Using dependency injection (constructor)
public MyClassCtor(DbScriptsContentSet dbScripts) {}

Registering a named type content set

TBD

Content sources

TBD

Built-in content sources

TBD

Combining file and resource content sources

TBD

Creating a content source

TBD

Typed content sets

TBD

About

Extensible framework to provide content from various sources to a .NET application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors