commit 467e2c689fc64cef00b53c7d9a398edb4ccd9742 Author: Yik Teng Hie Date: Wed Feb 15 22:55:05 2023 +0800 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..123d20a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vs/ +bin/ +obj/ +packages/ \ No newline at end of file diff --git a/MEFDemo.sln b/MEFDemo.sln new file mode 100644 index 0000000..555b3c2 --- /dev/null +++ b/MEFDemo.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Util", "Util\Util.csproj", "{A847BA71-AFF1-48AF-AEA5-99F131882E09}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MEFDemo", "MEFDemo\MEFDemo.csproj", "{86A1EB30-75C4-4AC9-9955-0F6033013B73}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxy", "Proxy\Proxy.csproj", "{FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MEFDemo.unittest", "MEFDemo.unittest\MEFDemo.unittest.csproj", "{6FD344AD-8D18-4431-B095-273A130BE7BD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A847BA71-AFF1-48AF-AEA5-99F131882E09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A847BA71-AFF1-48AF-AEA5-99F131882E09}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A847BA71-AFF1-48AF-AEA5-99F131882E09}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A847BA71-AFF1-48AF-AEA5-99F131882E09}.Release|Any CPU.Build.0 = Release|Any CPU + {86A1EB30-75C4-4AC9-9955-0F6033013B73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86A1EB30-75C4-4AC9-9955-0F6033013B73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86A1EB30-75C4-4AC9-9955-0F6033013B73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86A1EB30-75C4-4AC9-9955-0F6033013B73}.Release|Any CPU.Build.0 = Release|Any CPU + {FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE}.Release|Any CPU.Build.0 = Release|Any CPU + {6FD344AD-8D18-4431-B095-273A130BE7BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6FD344AD-8D18-4431-B095-273A130BE7BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6FD344AD-8D18-4431-B095-273A130BE7BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6FD344AD-8D18-4431-B095-273A130BE7BD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/MEFDemo.unittest/MEFDemo.unittest.csproj b/MEFDemo.unittest/MEFDemo.unittest.csproj new file mode 100644 index 0000000..cd8902c --- /dev/null +++ b/MEFDemo.unittest/MEFDemo.unittest.csproj @@ -0,0 +1,108 @@ + + + + Debug + AnyCPU + {6FD344AD-8D18-4431-B095-273A130BE7BD} + Library + Properties + MEFDemo.unittest + MEFDemo.unittest + v4.0 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll + True + + + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + + {86a1eb30-75c4-4ac9-9955-0f6033013b73} + MEFDemo + + + {ff9b1521-799c-4e56-8d9c-25fbf9bf2abe} + Proxy + + + {a847ba71-aff1-48af-aea5-99f131882e09} + Util + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/MEFDemo.unittest/MEFDemoProxyIntegrationTest.cs b/MEFDemo.unittest/MEFDemoProxyIntegrationTest.cs new file mode 100644 index 0000000..f4ef429 --- /dev/null +++ b/MEFDemo.unittest/MEFDemoProxyIntegrationTest.cs @@ -0,0 +1,59 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Proxy; +using Rhino.Mocks; +using System.ComponentModel.Composition; +using System.ComponentModel.Composition.Hosting; +using Util; + +namespace MEFDemo.unittest +{ + [TestClass] + public class MEFDemoProxyIntegrationTest + { + private CompositionContainer CreateIsolatedContainer(INetwork fakeNetwork = null) + { + if (fakeNetwork == null) fakeNetwork = MockRepository.GenerateMock(); + //slower, but more robust: + //var catalog = new AggregateCatalog(); + //var assemblyDirectory = "."; + //catalog.Catalogs.Add(new DirectoryCatalog(assemblyDirectory)); + var catalog = new TypeCatalog(typeof(INetwork), typeof(MachineChecker)); + var container = new CompositionContainer(catalog); + container.ComposeExportedValue(fakeNetwork); + return container; + } + + [TestMethod] + public void Isolate_from_Network() + { + var fakeNetwork = MockRepository.GenerateMock(); + var container = CreateIsolatedContainer(fakeNetwork); + + Program.Run(container); + + fakeNetwork.AssertWasCalled(x => x.IsLocalMachine(Arg.Is.Anything)); + } + + [TestMethod] + public void OnLocalMachine_Run_returns_Local() + { + var fakseLocalMachineNetwork = MockRepository.GenerateMock(); + fakseLocalMachineNetwork.Stub(x => x.IsLocalMachine(Arg.Is.Anything)).Return(true); + var container = CreateIsolatedContainer(fakseLocalMachineNetwork); + + var result = Program.Run(container); + + Assert.IsTrue(result.ToLower().Contains("local")); + } + + [TestMethod] + public void OnUnknownMachine_Run_returns_Foreign() + { + var container = CreateIsolatedContainer(); + + var result = Program.Run(container); + + Assert.IsTrue(result.ToLower().Contains("foreign")); + } + } +} diff --git a/MEFDemo.unittest/Properties/AssemblyInfo.cs b/MEFDemo.unittest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b03c939 --- /dev/null +++ b/MEFDemo.unittest/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MEFDemo.unittest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MEFDemo.unittest")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c91a3836-e553-4fd8-af4a-5784110633f4")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MEFDemo.unittest/packages.config b/MEFDemo.unittest/packages.config new file mode 100644 index 0000000..219b8d8 --- /dev/null +++ b/MEFDemo.unittest/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/MEFDemo/ClassDiagram1.cd b/MEFDemo/ClassDiagram1.cd new file mode 100644 index 0000000..dcfaf5e --- /dev/null +++ b/MEFDemo/ClassDiagram1.cd @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAI= + Program.cs + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MEFDemo/MEFDemo.csproj b/MEFDemo/MEFDemo.csproj new file mode 100644 index 0000000..d74163e --- /dev/null +++ b/MEFDemo/MEFDemo.csproj @@ -0,0 +1,69 @@ + + + + + Debug + AnyCPU + {86A1EB30-75C4-4AC9-9955-0F6033013B73} + Exe + Properties + MEFDemo + MEFDemo + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + {ff9b1521-799c-4e56-8d9c-25fbf9bf2abe} + Proxy + + + {a847ba71-aff1-48af-aea5-99f131882e09} + Util + + + + + + + + \ No newline at end of file diff --git a/MEFDemo/Program.cs b/MEFDemo/Program.cs new file mode 100644 index 0000000..1d82c9d --- /dev/null +++ b/MEFDemo/Program.cs @@ -0,0 +1,39 @@ +using Proxy; +using System; +using System.ComponentModel.Composition.Hosting; +using System.Diagnostics; +using Util; + +namespace MEFDemo +{ + public class Program + { + public static void Main(string[] args) + { + var stopwatch = Stopwatch.StartNew(); + var catalog = new AggregateCatalog(); + var assemblyDirectory = "."; + catalog.Catalogs.Add(new DirectoryCatalog(assemblyDirectory)); + var container = new CompositionContainer(catalog); + Console.WriteLine("Setup : {0}", stopwatch.Elapsed); + + Run(container); + + Console.ReadLine(); + } + + public static string Run(CompositionContainer container) + { + var stopwatch = Stopwatch.StartNew(); + var machineChecker = container.GetExport(); + Console.WriteLine("Export: {0}", stopwatch.Elapsed); + + stopwatch.Restart(); + var check = machineChecker.Value.Check("localhost"); + Console.WriteLine("{0,8}{1}", check, stopwatch.Elapsed); + stopwatch.Stop(); + + return check; + } + } +} diff --git a/MEFDemo/Properties/AssemblyInfo.cs b/MEFDemo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f85dcf8 --- /dev/null +++ b/MEFDemo/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MEFDemo")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MEFDemo")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8695dd48-e914-4bca-aefd-fd15f7e08904")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Proxy/MachineChecker.cs b/Proxy/MachineChecker.cs new file mode 100644 index 0000000..ba316d5 --- /dev/null +++ b/Proxy/MachineChecker.cs @@ -0,0 +1,29 @@ +using System.ComponentModel.Composition; +using Util; + +namespace Proxy +{ + [Export] + public class MachineChecker + { + public INetwork Network { get; set; } + + [ImportingConstructor] + public MachineChecker(INetwork network) + { + Network = network; + } + + public string Check(string machine) + { + if (Network.IsLocalMachine(machine)) + { + return string.Format("Local : "); + } + else + { + return string.Format("Foreign:"); + } + } + } +} diff --git a/Proxy/Properties/AssemblyInfo.cs b/Proxy/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4311a51 --- /dev/null +++ b/Proxy/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Proxy")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Proxy")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("ead96a3a-6e77-4096-ba8b-e2c108ab67a2")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Proxy/Proxy.csproj b/Proxy/Proxy.csproj new file mode 100644 index 0000000..f17450b --- /dev/null +++ b/Proxy/Proxy.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {FF9B1521-799C-4E56-8D9C-25FBF9BF2ABE} + Library + Properties + Proxy + Proxy + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + {a847ba71-aff1-48af-aea5-99f131882e09} + Util + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c97080d --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# MEF Demo + +- Microsoft’s solution for dependency injection is called **Managed Extensibility Framework** (MEF) +- [Reference]https://erictummers.com/2015/06/15/first-look-at-managed-extensibility-framework-mef/ +- Simple Console to demo MEF pattern \ No newline at end of file diff --git a/Util/Network.cs b/Util/Network.cs new file mode 100644 index 0000000..497f50e --- /dev/null +++ b/Util/Network.cs @@ -0,0 +1,28 @@ +using System; +using System.ComponentModel.Composition; + +namespace Util +{ + public interface INetwork + { + bool IsLocalMachine(string machine); + } + + [Export(typeof(INetwork))] + public class Network : INetwork + { + public bool IsLocalMachine(string machine) + { + try + { + var localMachine = System.Net.Dns.GetHostName(); + var entry = System.Net.Dns.GetHostEntry(machine); + return entry.HostName.Equals(localMachine); + } + catch (Exception) + { + return false; + } + } + } +} diff --git a/Util/Properties/AssemblyInfo.cs b/Util/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a1a488c --- /dev/null +++ b/Util/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Util")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Util")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1050533b-51ef-40c1-8929-ebc4eedf84d1")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Util/Util.csproj b/Util/Util.csproj new file mode 100644 index 0000000..9c02092 --- /dev/null +++ b/Util/Util.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {A847BA71-AFF1-48AF-AEA5-99F131882E09} + Library + Properties + Util + Util + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file