Skip to content

Commit a0fc907

Browse files
committed
Working with the samples
1 parent 2bf20bf commit a0fc907

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1503
-448
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Weikio.PluginFramework.Abstractions\Weikio.PluginFramework.Abstractions.csproj" />
10+
<ProjectReference Include="..\..\src\Weikio.PluginFramework\Weikio.PluginFramework.csproj" />
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

samples/ConsoleApp/FirstPlugin.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Shared;
3+
4+
namespace ConsoleApp
5+
{
6+
public class FirstPlugin : IPlugin
7+
{
8+
public void Run()
9+
{
10+
Console.WriteLine("First plugin");
11+
}
12+
}
13+
}

samples/ConsoleApp/MyPlugin.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Shared;
3+
4+
namespace ConsoleApp
5+
{
6+
public class MyPlugin : IMyPlugin
7+
{
8+
public void Run()
9+
{
10+
Console.WriteLine("My plugin which implements IMyPlugin");
11+
}
12+
}
13+
}

samples/ConsoleApp/Program.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Shared;
4+
using Weikio.PluginFramework.Abstractions;
5+
using Weikio.PluginFramework.Catalogs;
6+
7+
namespace ConsoleApp
8+
{
9+
class Program
10+
{
11+
static async Task Main(string[] args)
12+
{
13+
await AssemblyCatalogSample();
14+
await TypeCatalogSample();
15+
await CompositeCatalogSample();
16+
}
17+
18+
private static async Task AssemblyCatalogSample()
19+
{
20+
Console.WriteLine("Assembly catalog sample");
21+
22+
// 1. Create a new plugin catalog from the current assembly
23+
var assemblyPluginCatalog = new AssemblyPluginCatalog(typeof(Program).Assembly, type => typeof(IPlugin).IsAssignableFrom(type));
24+
25+
// 2. Initialize the catalog
26+
await assemblyPluginCatalog.Initialize();
27+
28+
// 3. Get the plugins from the catalog
29+
var assemplyPlugins = assemblyPluginCatalog.GetPlugins();
30+
31+
foreach (var plugin in assemplyPlugins)
32+
{
33+
var inst = (IPlugin) Activator.CreateInstance(plugin);
34+
inst.Run();
35+
}
36+
}
37+
38+
private static async Task TypeCatalogSample()
39+
{
40+
Console.WriteLine("Type catalog sample");
41+
42+
var typePluginCatalog = new TypePluginCatalog(typeof(FirstPlugin));
43+
await typePluginCatalog.Initialize();
44+
45+
var typePlugin = typePluginCatalog.Get();
46+
47+
var pluginInstance = (IPlugin) Activator.CreateInstance(typePlugin);
48+
pluginInstance.Run();
49+
}
50+
51+
private static async Task CompositeCatalogSample()
52+
{
53+
Console.WriteLine("Composite catalog sample");
54+
55+
// 1. Create a new plugin catalog from the current assembly
56+
var assemblyPluginCatalog = new AssemblyPluginCatalog(typeof(Program).Assembly, type => typeof(IPlugin).IsAssignableFrom(type));
57+
58+
// 2. Also create a new plugin catalog from a type
59+
var typePluginCatalog = new TypePluginCatalog(typeof(MyPlugin));
60+
61+
// 3. Then combine the catalogs into a composite catalog
62+
var compositeCatalog = new CompositePluginCatalog(assemblyPluginCatalog, typePluginCatalog);
63+
64+
// 4. Initialize the composite catalog
65+
await compositeCatalog.Initialize();
66+
67+
// 5. Get the plugins from the catalog
68+
var assemplyPlugins = compositeCatalog.GetPlugins();
69+
70+
foreach (var plugin in assemplyPlugins)
71+
{
72+
if (plugin.Type.Name == "MyPlugin")
73+
{
74+
var inst = (IMyPlugin) Activator.CreateInstance(plugin);
75+
inst.Run();
76+
}
77+
else
78+
{
79+
var inst = (IPlugin) Activator.CreateInstance(plugin);
80+
inst.Run();
81+
}
82+
}
83+
}
84+
}
85+
}

samples/ConsoleApp/SecondPlugin.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Shared;
3+
4+
namespace ConsoleApp
5+
{
6+
public class SecondPlugin : IPlugin
7+
{
8+
public void Run()
9+
{
10+
Console.WriteLine("Second plugin");
11+
}
12+
}
13+
}

samples/Shared/IMyPlugin.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Shared
2+
{
3+
public interface IMyPlugin
4+
{
5+
void Run();
6+
}
7+
}

samples/Shared/IPlugin.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace Shared
4+
{
5+
public interface IPlugin
6+
{
7+
void Run();
8+
}
9+
10+
public interface IOutPlugin
11+
{
12+
string Get();
13+
}
14+
15+
public interface IOperator
16+
{
17+
int Calculate(int x, int y);
18+
}
19+
}

samples/Shared/Shared.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Shared;
2+
3+
namespace SharedPlugins
4+
{
5+
public class MinusOperator : IOperator
6+
{
7+
public int Calculate(int x, int y)
8+
{
9+
return x - y;
10+
}
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Shared;
2+
3+
namespace SharedPlugins
4+
{
5+
public class MultiplyOperator : IOperator
6+
{
7+
public int Calculate(int x, int y)
8+
{
9+
return x * y;
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)