Skip to content

Commit

Permalink
Initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Sep 29, 2017
1 parent b571b70 commit 5b29edb
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 2 deletions.
13 changes: 13 additions & 0 deletions VSMefSample/Apple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Composition;

namespace VSMefSample
{
[Export(typeof(Fruit))]
[ExportMetadata("Type", "Apple")]
public class Apple : Fruit
{
public bool IsJuicy { get; set; }

public override string ToString() => $"Apple (IsJuicy: {this.IsJuicy})";
}
}
12 changes: 12 additions & 0 deletions VSMefSample/Fruit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Composition;

namespace VSMefSample
{
public class Fruit
{
[Import]
public Tree GrewFrom { get; set; }

public int SeedCount { get; set; }
}
}
16 changes: 16 additions & 0 deletions VSMefSample/FruitMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace VSMefSample
{
public class FruitMetadata
{
public FruitMetadata(IDictionary<string, object> metadata)
{
this.Type = (string)metadata["Type"];
}

public string Type { get; set; }
}
}
13 changes: 13 additions & 0 deletions VSMefSample/Pear.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Composition;

namespace VSMefSample
{
[Export(typeof(Fruit))]
[ExportMetadata("Type", "Pear")]
public class Pear : Fruit
{
public bool IsDelicious { get; set; }

public override string ToString() => $"Pear (IsDelicious: {this.IsDelicious})";
}
}
39 changes: 38 additions & 1 deletion VSMefSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Composition;

namespace VSMefSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var exportProvider = CreateExportProviderAsync().GetAwaiter().GetResult();
Tree tree = exportProvider.GetExportedValue<Tree>();
var apple1 = (Apple)tree.CreateFruit("Apple");
apple1.IsJuicy = true;
var apple2 = (Apple)tree.CreateFruit("Apple");
var pear = (Pear)tree.CreateFruit("Pear");
pear.IsDelicious = true;

Console.WriteLine(tree);
}

static async Task<ExportProvider> CreateExportProviderAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance);
var parts = await discovery.CreatePartsAsync(Assembly.GetExecutingAssembly(), cancellationToken);
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance)
.AddParts(parts);
var composition = CompositionConfiguration.Create(catalog);
composition.CreateDgml().Save("mefgraph.dgml");
if (!composition.CompositionErrors.IsEmpty)
{
foreach (var error in composition.CompositionErrors.Peek())
{
Console.Error.WriteLine(error.Message);
foreach (var part in error.Parts)
{
Console.Error.WriteLine(" " + part.Definition.Type.FullName);
}
}
}

composition.ThrowOnErrors();
var exportProviderFactory = composition.CreateExportProviderFactory();
return exportProviderFactory.CreateExportProvider();
}
}
}
52 changes: 52 additions & 0 deletions VSMefSample/Tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;

namespace VSMefSample
{
[Export]
public class Tree : IDisposable
{
private readonly List<Export<Fruit>> createdFruit = new List<Export<Fruit>>();

[ImportMany]
public List<ExportFactory<Fruit, FruitMetadata>> FruitFactories { get; set; }

public Fruit CreateFruit(string type)
{
ExportFactory<Fruit> fruitFactory = this.FruitFactories.Single(ff => ff.Metadata.Type == type);
Export<Fruit> export = fruitFactory.CreateExport();
this.createdFruit.Add(export);
return export.Value;
}

public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine("Tree");
foreach (var fruit in this.createdFruit)
{
sb.AppendLine($" {fruit.Value}");
}

return sb.ToString();
}

public void Dispose() => this.Dispose(true);

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
foreach (Export<Fruit> export in this.createdFruit)
{
export.Dispose();
}

this.createdFruit.Clear();
}
}
}
}
7 changes: 6 additions & 1 deletion VSMefSample/VSMefSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFrameworks>net462</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Composition" Version="15.3.38" />
<PackageReference Include="System.Composition" Version="1.1.0" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions VSMefSample/mefgraph.dgml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph Layout="Sugiyama" GraphDirection="RightToLeft" xmlns="http://schemas.microsoft.com/vs/2009/dgml">
<Nodes>
<Node Id="VSMefSample_Pear" Label="VSMefSample.Pear" />
<Node Id="VSMefSample_Tree" Label="VSMefSample.Tree" />
<Node Id="Microsoft_VisualStudio_Composition_MetadataViewClassProvider" Label="Microsoft.VisualStudio.Composition.MetadataViewClassProvider" Category="VsMEFBuiltIn" />
<Node Id="VSMefSample_Apple" Label="VSMefSample.Apple" />
<Node Id="Microsoft_VisualStudio_Composition_PassthroughMetadataViewProvider" Label="Microsoft.VisualStudio.Composition.PassthroughMetadataViewProvider" Category="VsMEFBuiltIn" />
<Node Id="Microsoft_VisualStudio_Composition_ExportProvider_ExportProviderAsExport" Label="Microsoft.VisualStudio.Composition.ExportProvider.ExportProviderAsExport" Category="VsMEFBuiltIn" />
</Nodes>
<Links>
<Link Source="VSMefSample_Tree" Target="VSMefSample_Pear" />
<Link Source="VSMefSample_Apple" Target="VSMefSample_Tree" Category="ExportFactory" />
<Link Source="VSMefSample_Pear" Target="VSMefSample_Tree" Category="ExportFactory" />
<Link Source="VSMefSample_Tree" Target="VSMefSample_Apple" />
</Links>
<Categories>
<Category Id="Contains" IsContainment="True" />
</Categories>
<Styles>
<Style TargetType="Link" GroupLabel="ExportFactory">
<Condition Expression="HasCategory('ExportFactory')" />
<Setter Property="StrokeDashArray" Value="2,2" />
</Style>
<Style TargetType="Node" GroupLabel="VsMEFBuiltIn">
<Condition Expression="HasCategory('VsMEFBuiltIn')" />
<Setter Property="Visibility" Value="Hidden" />
</Style>
</Styles>
</DirectedGraph>

0 comments on commit 5b29edb

Please sign in to comment.