This commit is contained in:
RandomEngy 2018-08-09 23:44:14 -07:00
Родитель 245f2a6d04
Коммит 2c4ac81f76
9 изменённых файлов: 207 добавлений и 1 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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();
}
/// <summary>
/// Adds the singleton and transient scope registrars.
/// </summary>
private void AddCoreScopes()
{
this.AddScope(Lifetime.Singleton, new DryIocSingletonScopeRegistrar(this.container));
this.AddScope(Lifetime.Transient, new DryIocTransientScopeRegistrar(this.container));
}
public override T Resolve<T>()
{
return this.container.Resolve<T>();
}
public override object Resolve(Type componentType)
{
return this.container.Resolve(componentType);
}
public override IList<T> ResolveAll<T>()
{
return this.container.ResolveMany<T>().ToList();
}
public override IList<object> ResolveAll(Type componentType)
{
return this.container.ResolveMany(componentType).ToList();
}
}
}

Просмотреть файл

@ -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;
/// <summary>
/// Creates a new instance of the <see cref="DryIocSingletonScopeRegistrar"/> class.
/// </summary>
/// <param name="container">The DryIoc container to use to register.</param>
public DryIocSingletonScopeRegistrar(Container container)
{
this.container = container;
}
public override void Register<TRegisteredAs, TResolvedTo>()
{
this.container.Register<TRegisteredAs, TResolvedTo>(Reuse.Singleton);
}
public override void Register(Type registeredAs, Type resolvedTo)
{
this.container.Register(registeredAs, resolvedTo, Reuse.Singleton);
}
public override void Register<T>(Func<T> factory)
{
this.container.RegisterDelegate(c => factory(), Reuse.Singleton);
}
}
}

Просмотреть файл

@ -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;
/// <summary>
/// Creates a new instance of the <see cref="DryIocTransientScopeRegistrar"/> class.
/// </summary>
/// <param name="container">The DryIoc container to use to register.</param>
public DryIocTransientScopeRegistrar(Container container)
{
this.container = container;
}
public override void Register<TRegisteredAs, TResolvedTo>()
{
this.container.Register<TRegisteredAs, TResolvedTo>(Reuse.Transient);
}
public override void Register(Type registeredAs, Type resolvedTo)
{
this.container.Register(registeredAs, resolvedTo, Reuse.Transient);
}
public override void Register<T>(Func<T> factory)
{
this.container.RegisterDelegate(c => factory(), Reuse.Transient);
}
}
}

Просмотреть файл

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
<Authors>Microsoft</Authors>
<Description>Allows easily switching out IoC containers without rewriting code. This package supports DryIoc.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageLicenseUrl>https://github.com/Microsoft/AnyContainer/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Microsoft/AnyContainer</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/Microsoft/AnyContainer/master/AnyContainerIcon.png</PackageIconUrl>
<PackageTags>DI IoC Container Dependency-Injection DryIoc</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DryIoc.dll" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.AnyContainer\Microsoft.AnyContainer.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -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());
}
}
}

Просмотреть файл

@ -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<ILogger, Logger>(serviceKey: "a");
dryContainer.Register<ILogger, AlternateLogger>(serviceKey: "b");
IList<ILogger> loggers = anyContainer.ResolveAll<ILogger>();
Assert.AreEqual(2, loggers.Count);
Assert.AreNotEqual(loggers[0], loggers[1]);
}
}
}

Просмотреть файл

@ -41,6 +41,9 @@
<Reference Include="CommonServiceLocator, Version=2.0.3.0, Culture=neutral, PublicKeyToken=489b6accfaf20ef0, processorArchitecture=MSIL">
<HintPath>..\packages\Unity.5.8.6\lib\net46\CommonServiceLocator.dll</HintPath>
</Reference>
<Reference Include="DryIoc, Version=3.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DryIoc.dll.3.0.2\lib\net45\DryIoc.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
@ -76,6 +79,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CommonContainerTestRunner.cs" />
<Compile Include="DryIocContainerTests.cs" />
<Compile Include="RegisteredClasses\AlternateLogger.cs" />
<Compile Include="RegisteredClasses\ILogger.cs" />
<Compile Include="RegisteredClasses\Logger.cs" />
@ -88,6 +92,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.AnyContainer.DryIoc\Microsoft.AnyContainer.DryIoc.csproj">
<Project>{74545652-bccc-4133-a8a1-d81d08a89ff8}</Project>
<Name>Microsoft.AnyContainer.DryIoc</Name>
</ProjectReference>
<ProjectReference Include="..\Microsoft.AnyContainer.SimpleInjector\Microsoft.AnyContainer.SimpleInjector.csproj">
<Project>{1e261dc5-e7b9-4914-94f1-b96ec7d2d374}</Project>
<Name>Microsoft.AnyContainer.SimpleInjector</Name>

Просмотреть файл

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DryIoc.dll" version="3.0.2" targetFramework="net461" />
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net461" />
<package id="SimpleInjector" version="4.3.0" targetFramework="net461" />