|
| 1 | +## About |
| 2 | + |
| 3 | +<!-- A description of the package and where one can find more documentation --> |
| 4 | + |
| 5 | +`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. |
| 6 | + |
| 7 | +This package simplifies the process of applying consistent patterns for part exports, imports, and metadata by using convention-based configurations. |
| 8 | +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. |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +<!-- The key features of this package --> |
| 13 | + |
| 14 | +* Configure exports, imports, and metadata for parts using conventions rather than attributes. |
| 15 | +* Allows defining conventions through a fluent API, making configuration more flexible and readable. |
| 16 | + |
| 17 | +## How to Use |
| 18 | + |
| 19 | +<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package --> |
| 20 | + |
| 21 | +Configure parts for composition without using attributes. |
| 22 | + |
| 23 | +```csharp |
| 24 | +using System.Composition.Convention; |
| 25 | +using System.Composition.Hosting; |
| 26 | + |
| 27 | +var conventions = new ConventionBuilder(); |
| 28 | + |
| 29 | +// Apply conventions: any class that implements ILogger will be exported as ILogger |
| 30 | +conventions |
| 31 | + .ForTypesDerivedFrom<ILogger>() |
| 32 | + .Export<ILogger>(); |
| 33 | + |
| 34 | +var configuration = new ContainerConfiguration() |
| 35 | + .WithPart<FileLogger>(conventions) |
| 36 | + .WithPart<ConsoleLogger>(conventions); |
| 37 | + |
| 38 | +using CompositionHost container = configuration.CreateContainer(); |
| 39 | + |
| 40 | +var loggers = container.GetExports<ILogger>(); |
| 41 | + |
| 42 | +foreach (var logger in loggers) |
| 43 | +{ |
| 44 | + logger.Log("Hello, World!"); |
| 45 | +} |
| 46 | +// FileLogger: Hello, World! |
| 47 | +// ConsoleLogger: Hello, World! |
| 48 | +
|
| 49 | +public interface ILogger |
| 50 | +{ |
| 51 | + void Log(string message); |
| 52 | +} |
| 53 | + |
| 54 | +public class FileLogger : ILogger |
| 55 | +{ |
| 56 | + public void Log(string message) => Console.WriteLine($"FileLogger: {message}"); |
| 57 | +} |
| 58 | + |
| 59 | +public class ConsoleLogger : ILogger |
| 60 | +{ |
| 61 | + public void Log(string message) => Console.WriteLine($"ConsoleLogger: {message}"); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Main Types |
| 66 | + |
| 67 | +<!-- The main types provided in this library --> |
| 68 | + |
| 69 | +The main types provided by this library are: |
| 70 | + |
| 71 | +* `System.Composition.Convention.ConventionBuilder` |
| 72 | +* `System.Composition.Convention.PartConventionBuilder` |
| 73 | +* `System.Composition.Convention.ParameterImportConventionBuilder` |
| 74 | + |
| 75 | +## Additional Documentation |
| 76 | + |
| 77 | +<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> |
| 78 | + |
| 79 | +* [API documentation](https://learn.microsoft.com/dotnet/api/system.composition.convention) |
| 80 | +* [Managed Extensibility Framework (MEF)](https://learn.microsoft.com/dotnet/framework/mef/) |
| 81 | + |
| 82 | +## Related Packages |
| 83 | + |
| 84 | +<!-- The related packages associated with this package --> |
| 85 | + |
| 86 | +* [System.Composition](https://www.nuget.org/packages/System.Composition) |
| 87 | +* [System.Composition.AttributedModel](https://www.nuget.org/packages/System.Composition.AttributedModel) |
| 88 | +* [System.Composition.Hosting](https://www.nuget.org/packages/System.Composition.Hosting) |
| 89 | +* [System.Composition.Runtime](https://www.nuget.org/packages/System.Composition.Runtime) |
| 90 | +* [System.Composition.TypedParts](https://www.nuget.org/packages/System.Composition.TypedParts) |
| 91 | + |
| 92 | +## Feedback & Contributing |
| 93 | + |
| 94 | +<!-- How to provide feedback on this package and contribute to it --> |
| 95 | + |
| 96 | +System.Composition.Convention is released as open source under the [MIT license](https://licenses.nuget.org/MIT). |
| 97 | +Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime). |
0 commit comments