Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide System.Composition.Convention package readme #106782

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/libraries/System.Composition.Convention/src/PACKAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
## About

<!-- A description of the package and where one can find more documentation -->

`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

<!-- The key features of this package -->

* 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

<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package -->

Configure parts for composition without using attributes.

```csharp
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 in this library -->

The main types provided by this library are:

* `System.Composition.Convention.ConventionBuilder`
* `System.Composition.Convention.PartConventionBuilder`
* `System.Composition.Convention.ParameterImportConventionBuilder`

## Additional Documentation

<!-- Links to further documentation. Remove conceptual documentation if not available for the library. -->

* [API documentation](https://learn.microsoft.com/dotnet/api/system.composition.convention)
* [Managed Extensibility Framework (MEF)](https://learn.microsoft.com/dotnet/framework/mef/)

## Related Packages

<!-- The related packages associated with this package -->

* [System.Composition](https://www.nuget.org/packages/System.Composition)
* [System.Composition.AttributedModel](https://www.nuget.org/packages/System.Composition.AttributedModel)
* [System.Composition.Hosting](https://www.nuget.org/packages/System.Composition.Hosting)
* [System.Composition.Runtime](https://www.nuget.org/packages/System.Composition.Runtime)
* [System.Composition.TypedParts](https://www.nuget.org/packages/System.Composition.TypedParts)

## Feedback & Contributing

<!-- How to provide feedback on this package and contribute to it -->

System.Composition.Convention is released as open source under the [MIT license](https://licenses.nuget.org/MIT).
Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime).
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@
<StrongNameKeyId>Microsoft</StrongNameKeyId>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<PackageDescription>Provides types that support using Managed Extensibility Framework with a convention-based configuration model.

Commonly Used Types:
System.Composition.Convention.ConventionBuilder
System.Composition.Convention.ExportConventionBuilder
System.Composition.Convention.ImportConventionBuilder
System.Composition.Convention.PartConventionBuilder
System.Composition.Convention.ParameterImportConventionBuilder</PackageDescription>
<PackageDescription>Provides types that support using Managed Extensibility Framework (MEF) with a convention-based configuration model.</PackageDescription>
<!-- TODO https://github.com/dotnet/runtime/issues/90400: Annotate for nullable reference types -->
<Nullable>disable</Nullable>
<NoWarn>$(NoWarn);nullable</NoWarn>
<!-- TODO: Add package README file: https://github.com/dotnet/runtime/issues/99358 -->
<EnableDefaultPackageReadmeFile>false</EnableDefaultPackageReadmeFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading