The ContentProvider framework provides a mechanism for retrieving string and binary resources from various sources such as files, embedded resources, etc.
[[TOC]]
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 coreContentProviderlibrary. You will typically use the predefined singleton instanceContentManager.Global. Creating new instances is only for advanced scenarios and mocking during testing.ContentManagerprovides multipleRegistermethod overloads to register content sets.To retrieve content sets later on, use the
GetContentSetmethod overloads provided on the sameContentManagerinstance used to register them. -
As dependency injection services: The
ContentProvider.Extensions.DependencyInjectionlibrary providesAddContentextensions onIServiceCollectionto register content sets as injectable services.To retrieve content sets later on, use dependency injection.
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) {}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) {}TBD
TBD
TBD
TBD
TBD
TBD