From 2c4ac81f768eeba267b9e8034ce19f63cfb8a936 Mon Sep 17 00:00:00 2001 From: RandomEngy Date: Thu, 9 Aug 2018 23:44:14 -0700 Subject: [PATCH] Added DryIoc support. --- AnyContainer.sln | 8 +++ .../DryIocAnyContainer.cs | 57 +++++++++++++++++++ .../DryIocSingletonScopeRegistrar.cs | 36 ++++++++++++ .../DryIocTransientScopeRegistrar.cs | 36 ++++++++++++ .../Microsoft.AnyContainer.DryIoc.csproj | 23 ++++++++ UnitTests/ContainerTests.cs | 9 ++- UnitTests/DryIocContainerTests.cs | 30 ++++++++++ UnitTests/UnitTests.csproj | 8 +++ UnitTests/packages.config | 1 + 9 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 Microsoft.AnyContainer.DryIoc/DryIocAnyContainer.cs create mode 100644 Microsoft.AnyContainer.DryIoc/DryIocSingletonScopeRegistrar.cs create mode 100644 Microsoft.AnyContainer.DryIoc/DryIocTransientScopeRegistrar.cs create mode 100644 Microsoft.AnyContainer.DryIoc/Microsoft.AnyContainer.DryIoc.csproj create mode 100644 UnitTests/DryIocContainerTests.cs diff --git a/AnyContainer.sln b/AnyContainer.sln index d22a48a..779f936 100644 --- a/AnyContainer.sln +++ b/AnyContainer.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AnyContainer.Simp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AnyContainer.Unity", "Microsoft.AnyContainer.Unity\Microsoft.AnyContainer.Unity.csproj", "{787B31A0-82EF-4368-8315-CAC3CAD3ABBB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AnyContainer.DryIoc", "Microsoft.AnyContainer.DryIoc\Microsoft.AnyContainer.DryIoc.csproj", "{74545652-BCCC-4133-A8A1-D81D08A89FF8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -48,6 +50,12 @@ Global {787B31A0-82EF-4368-8315-CAC3CAD3ABBB}.Release|Any CPU.Build.0 = Release|Any CPU {787B31A0-82EF-4368-8315-CAC3CAD3ABBB}.Release-NuGet|Any CPU.ActiveCfg = Release|Any CPU {787B31A0-82EF-4368-8315-CAC3CAD3ABBB}.Release-NuGet|Any CPU.Build.0 = Release|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Release|Any CPU.Build.0 = Release|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Release-NuGet|Any CPU.ActiveCfg = Release|Any CPU + {74545652-BCCC-4133-A8A1-D81D08A89FF8}.Release-NuGet|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Microsoft.AnyContainer.DryIoc/DryIocAnyContainer.cs b/Microsoft.AnyContainer.DryIoc/DryIocAnyContainer.cs new file mode 100644 index 0000000..65de414 --- /dev/null +++ b/Microsoft.AnyContainer.DryIoc/DryIocAnyContainer.cs @@ -0,0 +1,57 @@ +// Copyright © Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DryIoc; + +namespace Microsoft.AnyContainer.DryIoc +{ + public class DryIocAnyContainer : AnyContainerBase + { + private readonly Container container; + + public DryIocAnyContainer() + { + this.container = new Container(); + this.AddCoreScopes(); + } + + public DryIocAnyContainer(Container container) + { + this.container = container; + this.AddCoreScopes(); + } + + /// + /// Adds the singleton and transient scope registrars. + /// + private void AddCoreScopes() + { + this.AddScope(Lifetime.Singleton, new DryIocSingletonScopeRegistrar(this.container)); + this.AddScope(Lifetime.Transient, new DryIocTransientScopeRegistrar(this.container)); + } + + public override T Resolve() + { + return this.container.Resolve(); + } + + public override object Resolve(Type componentType) + { + return this.container.Resolve(componentType); + } + + public override IList ResolveAll() + { + return this.container.ResolveMany().ToList(); + } + + public override IList ResolveAll(Type componentType) + { + return this.container.ResolveMany(componentType).ToList(); + } + } +} diff --git a/Microsoft.AnyContainer.DryIoc/DryIocSingletonScopeRegistrar.cs b/Microsoft.AnyContainer.DryIoc/DryIocSingletonScopeRegistrar.cs new file mode 100644 index 0000000..b7f8fce --- /dev/null +++ b/Microsoft.AnyContainer.DryIoc/DryIocSingletonScopeRegistrar.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DryIoc; + +namespace Microsoft.AnyContainer.DryIoc +{ + public class DryIocSingletonScopeRegistrar : ScopeRegistrar + { + private readonly Container container; + + /// + /// Creates a new instance of the class. + /// + /// The DryIoc container to use to register. + public DryIocSingletonScopeRegistrar(Container container) + { + this.container = container; + } + + public override void Register() + { + this.container.Register(Reuse.Singleton); + } + + public override void Register(Type registeredAs, Type resolvedTo) + { + this.container.Register(registeredAs, resolvedTo, Reuse.Singleton); + } + + public override void Register(Func factory) + { + this.container.RegisterDelegate(c => factory(), Reuse.Singleton); + } + } +} diff --git a/Microsoft.AnyContainer.DryIoc/DryIocTransientScopeRegistrar.cs b/Microsoft.AnyContainer.DryIoc/DryIocTransientScopeRegistrar.cs new file mode 100644 index 0000000..c0f13af --- /dev/null +++ b/Microsoft.AnyContainer.DryIoc/DryIocTransientScopeRegistrar.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DryIoc; + +namespace Microsoft.AnyContainer.DryIoc +{ + public class DryIocTransientScopeRegistrar : ScopeRegistrar + { + private readonly Container container; + + /// + /// Creates a new instance of the class. + /// + /// The DryIoc container to use to register. + public DryIocTransientScopeRegistrar(Container container) + { + this.container = container; + } + + public override void Register() + { + this.container.Register(Reuse.Transient); + } + + public override void Register(Type registeredAs, Type resolvedTo) + { + this.container.Register(registeredAs, resolvedTo, Reuse.Transient); + } + + public override void Register(Func factory) + { + this.container.RegisterDelegate(c => factory(), Reuse.Transient); + } + } +} diff --git a/Microsoft.AnyContainer.DryIoc/Microsoft.AnyContainer.DryIoc.csproj b/Microsoft.AnyContainer.DryIoc/Microsoft.AnyContainer.DryIoc.csproj new file mode 100644 index 0000000..1fddd44 --- /dev/null +++ b/Microsoft.AnyContainer.DryIoc/Microsoft.AnyContainer.DryIoc.csproj @@ -0,0 +1,23 @@ + + + + netstandard1.0 + Microsoft + Allows easily switching out IoC containers without rewriting code. This package supports DryIoc. + © Microsoft Corporation. All rights reserved. + https://github.com/Microsoft/AnyContainer/blob/master/LICENSE + https://github.com/Microsoft/AnyContainer + https://raw.githubusercontent.com/Microsoft/AnyContainer/master/AnyContainerIcon.png + DI IoC Container Dependency-Injection DryIoc + true + + + + + + + + + + + diff --git a/UnitTests/ContainerTests.cs b/UnitTests/ContainerTests.cs index b3f46e7..0bbd8eb 100644 --- a/UnitTests/ContainerTests.cs +++ b/UnitTests/ContainerTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using Microsoft.AnyContainer.DryIoc; using Microsoft.AnyContainer.Unity; using Microsoft.AnyContainer.SimpleInjector; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -22,5 +23,11 @@ namespace Microsoft.AnyContainer.UnitTests { CommonContainerTestRunner.RunTests(() => new SimpleInjectorAnyContainer()); } - } + + [TestMethod] + public void TestDryIoc() + { + CommonContainerTestRunner.RunTests(() => new DryIocAnyContainer()); + } + } } diff --git a/UnitTests/DryIocContainerTests.cs b/UnitTests/DryIocContainerTests.cs new file mode 100644 index 0000000..0f98140 --- /dev/null +++ b/UnitTests/DryIocContainerTests.cs @@ -0,0 +1,30 @@ +// Copyright © Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using DryIoc; +using Microsoft.AnyContainer.DryIoc; +using Microsoft.AnyContainer.UnitTests.RegisteredClasses; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.AnyContainer.UnitTests +{ + [TestClass] + public class DryIocContainerTests + { + [TestMethod] + public void ResolveAll() + { + Container dryContainer = new Container(); + DryIocAnyContainer anyContainer = new DryIocAnyContainer(dryContainer); + + dryContainer.Register(serviceKey: "a"); + dryContainer.Register(serviceKey: "b"); + + IList loggers = anyContainer.ResolveAll(); + Assert.AreEqual(2, loggers.Count); + Assert.AreNotEqual(loggers[0], loggers[1]); + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 7ddaa1b..929f0fd 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -41,6 +41,9 @@ ..\packages\Unity.5.8.6\lib\net46\CommonServiceLocator.dll + + ..\packages\DryIoc.dll.3.0.2\lib\net45\DryIoc.dll + ..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll @@ -76,6 +79,7 @@ + @@ -88,6 +92,10 @@ + + {74545652-bccc-4133-a8a1-d81d08a89ff8} + Microsoft.AnyContainer.DryIoc + {1e261dc5-e7b9-4914-94f1-b96ec7d2d374} Microsoft.AnyContainer.SimpleInjector diff --git a/UnitTests/packages.config b/UnitTests/packages.config index c622abe..feeb821 100644 --- a/UnitTests/packages.config +++ b/UnitTests/packages.config @@ -1,5 +1,6 @@  +