Skip to content

Repository files navigation

TagBites.ComponentModel.Composition

NugetBuildLicense

A lightweight export container for plugin-based .NET applications. The container discovers types marked with the MEF [Export] attribute and gives access to them by contract type, contract name or URI.

Install

dotnet add package TagBites.ComponentModel.Composition

Targets netstandard2.0. Only dependency is System.ComponentModel.Composition, used for the standard [Export] attribute.

Why TagBites.ComponentModel.Composition?

MEF (CompositionContainer) composes full object graphs with imports, exports and lifetime policies. This library solves a smaller problem: it keeps a registry of exported types and creates instances on demand. In exchange it offers:

  • Explicit lifecycle. Assemblies are loaded and unloaded on demand. Unloading an assembly removes its exports and restores the ones it replaced.
  • Stable identity. Every export has a URI. The URI addresses one implementation and allows a plugin to replace an implementation from another assembly.
  • Change notifications. Events report changed contract types when exports are loaded, unloaded, registered or unregistered.
  • Lazy instances. Each export provides a shared instance (created on first use, held by a weak reference) or a new instance per call.

Typical use cases: plugin systems, modular desktop applications, applications with many optional assemblies and a slow cold start.

Usage

Discover exports

publicinterfaceIImporter{stringName{get;}}[Export(typeof(IImporter))]publicclassCsvImporter:IImporter{publicstringName=>"CSV";}[Export(typeof(IImporter))]publicclassXmlImporter:IImporter{publicstringName=>"XML";}
varmanager=newExportComponentManager();manager.LoadAssembly(typeof(CsvImporter).Assembly);foreach(varimporterinmanager.GetExportInstances<IImporter>())Console.WriteLine(importer.Name);// CSV// XML

Shared and new instances

GetExportInstance returns a shared instance. The instance is created on first use and held by a weak reference, so the garbage collector may reclaim it; the next access creates a new one. CreateExportInstance returns a new instance on every call.

varshared=manager.GetExportInstances<IImporter>().First();varagain=manager.GetExportInstances<IImporter>().First();// ReferenceEquals(shared, again) == truevarfresh=manager.CreateExportInstances<IImporter>().First();// ReferenceEquals(shared, fresh) == false

Contract names

A contract name separates exports of the same contract type into groups.

[Export("analytics",typeof(IImporter))]publicclassAnalyticsImporter:IImporter{publicstringName=>"Analytics";}
manager.GetExportInstances<IImporter>("analytics");// AnalyticsImportermanager.GetExportInstances<IImporter>();// exports without a contract namemanager.GetManyExports<IImporter>([null,"analytics"]);// both groups

Export URI

Every export has a Location URI built from the contract type, the contract name and the implementation type: export:{contract}/{name}/{implementation}. A type is identified by its full name with assembly name, or by its [Guid] attribute when present.

varexport=manager.GetExports<IImporter>().First();Console.WriteLine(export.Location);// export:MyApp.IImporter,MyApp/MyApp.CsvImporter,MyAppvarinstance=manager.GetExportInstance<IImporter>(export.Location);// shared CsvImporter instance

A [Guid] attribute makes the identity independent from the type name and assembly. Two implementations in different assemblies with the same [Guid] share the URI, which allows one to replace the other.

Duplicate URIs

When a loaded assembly contains an export whose URI is already registered, the AssemblyExportSettings assembly attribute decides the outcome:

[assembly:AssemblyExportSettings(DuplicateUriHandling=ExportDuplicateUriHandling.OverrideExisting)]
ModeBehavior
SkipCurrentThe new export is ignored. Default.
OverrideExistingThe new export replaces the existing one for URI lookups. Both remain listed for the contract.
RemoveExistingThe existing export is removed. Unloading the new assembly restores it.

Manual registration

Components can be registered without the [Export] attribute. An instance provider supports types without a default constructor.

varcomponent=newExportComponent<IImporter>(null,typeof(IImporter),typeof(CsvImporter));manager.Register(component);manager.Unregister(component);
varcomponent=newExportComponent<IImporter>(null,typeof(IImporter),typeof(DatabaseImporter),null,()=>newDatabaseImporter(connectionString),null);manager.Register(component);

Change notifications

ExportCollectionChanged reports every change with the list of affected contract types. AddNotify subscribes to one contract type.

manager.ExportCollectionChanged+=(s,e)=>Refresh(e.ChangedContractsTypes);manager.AddNotify(typeof(IImporter),OnImportersChanged);manager.RemoveNotify(typeof(IImporter),OnImportersChanged);

Unloading

UnloadAssembly removes all exports of an assembly and restores exports it replaced.

manager.UnloadAssembly(pluginAssembly);

Startup cache

LoadAssembly reflects over all types of an assembly. With many assemblies this cost dominates application startup. UseCache stores the scan result of each assembly in a JSON file, so later starts read one small file per assembly instead. The file name includes the assembly module version id, so a rebuilt assembly bypasses the stale file automatically. An unreadable or corrupted file falls back to reflection.

The serializer is provided by the caller, so the library has no serializer dependency:

varmanager=newExportComponentManager();manager.UseCache(Path.Combine(AppContext.BaseDirectory,"export-cache"),(file,type)=>JsonSerializer.Deserialize(File.ReadAllText(file),type),(file,model)=>File.WriteAllText(file,JsonSerializer.Serialize(model)));foreach(varassemblyinAppDomain.CurrentDomain.GetAssemblies())manager.LoadAssembly(assembly);// After startup, e.g. from a background taskmanager.PrepareCache();

PrepareCache writes files for assemblies loaded without a cache hit and removes stale files of previous builds. The cache stores contract type names; UseCustomTypeResolver replaces the default Type.GetType resolution when needed.

Links

About

A .NET export container and manager designed to simplify access to exported types. Well suited for plugin integration.

Topics

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Used by

Contributors

Languages