System.Composition.Convention 10.0.12

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package System.Composition.Convention --version 10.0.12
                    
NuGet\Install-Package System.Composition.Convention -Version 10.0.12
                    
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="System.Composition.Convention" Version="10.0.12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="System.Composition.Convention" Version="10.0.12" />
                    
Directory.Packages.props
<PackageReference Include="System.Composition.Convention" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add System.Composition.Convention --version 10.0.12
                    
#r "nuget: System.Composition.Convention, 10.0.12"
                    
#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.
#:package System.Composition.Convention@10.0.12
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=System.Composition.Convention&version=10.0.12
                    
Install as a Cake Addin
#tool nuget:?package=System.Composition.Convention&version=10.0.12
                    
Install as a Cake Tool

About

System.Composition.Convention is part of the Managed Extensibility Framework (MEF) 2.0, a composition library for .NET that enables dependency injection through attributes or conventions.

This package simplifies the process of applying consistent patterns for part exports, imports, and metadata by using convention-based configurations. It is useful for scenarios where you want to avoid repetitive attribute-based decoration and instead define conventions for registering types in your composition container.

Key Features

  • Configure exports, imports, and metadata for parts using conventions rather than attributes.
  • Allows defining conventions through a fluent API, making configuration more flexible and readable.

How to Use

Configure parts for composition without using attributes.

using System.Composition.Convention;
using System.Composition.Hosting;

var conventions = new ConventionBuilder();

// Apply conventions: any class that implements ILogger will be exported as ILogger
conventions
    .ForTypesDerivedFrom<ILogger>()
    .Export<ILogger>();

var configuration = new ContainerConfiguration()
    .WithPart<FileLogger>(conventions)
    .WithPart<ConsoleLogger>(conventions);

using CompositionHost container = configuration.CreateContainer();
    
var loggers = container.GetExports<ILogger>();

foreach (var logger in loggers)
{
    logger.Log("Hello, World!");
}
// FileLogger: Hello, World!
// ConsoleLogger: Hello, World!

public interface ILogger
{
    void Log(string message);
}

public class FileLogger : ILogger
{
    public void Log(string message) => Console.WriteLine($"FileLogger: {message}");
}

public class ConsoleLogger : ILogger
{
    public void Log(string message) => Console.WriteLine($"ConsoleLogger: {message}");
}

Main Types

The main types provided by this library are:

  • System.Composition.Convention.ConventionBuilder
  • System.Composition.Convention.PartConventionBuilder
  • System.Composition.Convention.ParameterImportConventionBuilder

Additional Documentation

Feedback & Contributing

System.Composition.Convention is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.