├── .gitattributes ├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── regression.yml ├── .gitignore ├── LICENSE ├── README.md ├── appveyor.yml ├── codecov.yml ├── package.props ├── package.sln ├── src ├── Builder │ ├── Context │ │ ├── BuilderContext.cs │ │ └── BuilderContextExpression.cs │ └── Stages │ │ ├── BuilderStage.cs │ │ ├── SelectionStage.cs │ │ └── UnityBuildStage.cs ├── Events │ ├── ChildContainerCreatedEventArgs.cs │ ├── NamedEventArgs.cs │ ├── RegisterEventArgs.cs │ └── RegisterInstanceEventArgs.cs ├── Exceptions │ ├── DependencyMissingException.cs │ ├── IllegalInjectionMethodException.Desktop.cs │ ├── IllegalInjectionMethodException.cs │ ├── InvalidRegistrationException.cs │ └── MakeGenericTypeFailedException.cs ├── Extension │ ├── ExtensionContext.cs │ ├── IUnityContainerExtensionConfigurator.cs │ └── UnityContainerExtension.cs ├── Extensions │ ├── DefaultLifetime.cs │ ├── Diagnostic.cs │ ├── ExtensionExtensions.cs │ ├── Legacy.cs │ └── Strategies.cs ├── Factories │ ├── DeferredFuncResolverFactory.cs │ ├── EnumerableResolver.cs │ └── GenericLazyResolverFactory.cs ├── Injection │ └── Validating.cs ├── Legacy │ ├── DynamicMethod │ │ └── DynamicBuildPlanGenerationContext.cs │ ├── IBuildPlanCreatorPolicy.cs │ ├── IBuildPlanPolicy.cs │ ├── IConstructorSelectorPolicy.cs │ ├── IMethodSelectorPolicy.cs │ ├── IPropertySelectorPolicy.cs │ ├── InjectionParameterValue.cs │ ├── NamedTypeBuildKey.cs │ ├── TypeBasedOverride.cs │ └── TypedInjectionValue.cs ├── Lifetime │ ├── ContainerLifetimeManager.cs │ ├── InternalPerResolveLifetimeManager.cs │ └── LifetimeContainer.cs ├── Policy │ ├── Converter.cs │ ├── IResolveDelegateFactory.cs │ └── ISelect.cs ├── Processors │ ├── Abstracts │ │ ├── MemberExpression.cs │ │ ├── MemberProcessor.cs │ │ └── MemberResolution.cs │ ├── Constructor │ │ ├── ConstructorDiagnostic.cs │ │ ├── ConstructorExpression.cs │ │ ├── ConstructorProcessor.cs │ │ └── ConstructorResolution.cs │ ├── Fields │ │ ├── FieldDiagnostic.cs │ │ └── FieldProcessor.cs │ ├── Methods │ │ ├── MethodDiagnostic.cs │ │ └── MethodProcessor.cs │ ├── Parameters │ │ ├── ParametersDiagnostic.cs │ │ └── ParametersProcessor.cs │ └── Properties │ │ ├── PropertyDiagnostic.cs │ │ └── PropertyProcessor.cs ├── Properties │ └── AssemblyInfo.cs ├── Registration │ ├── ContainerRegistration.cs │ └── InternalRegistration.cs ├── Storage │ ├── HashRegistry.cs │ ├── IRegistry.cs │ ├── IStagedStrategyChain.cs │ ├── LinkedNode.cs │ ├── LinkedRegistry.cs │ ├── PolicyList.cs │ ├── QuickSet.cs │ ├── RegistrationSet.cs │ ├── Registrations.cs │ └── StagedStrategyChain.cs ├── Strategies │ ├── ArrayResolveStrategy.cs │ ├── BuildKeyMappingStrategy.cs │ ├── BuildPlanStrategy.cs │ ├── BuilderStrategy.cs │ └── LifetimeStrategy.cs ├── Unity.Container.csproj ├── Unity.Container.csproj.DotSettings ├── UnityContainer.ContainerContext.cs ├── UnityContainer.Diagnostic.cs ├── UnityContainer.IUnityContainer.cs ├── UnityContainer.Implementation.cs ├── UnityContainer.Public.cs ├── UnityContainer.Registration.cs ├── UnityContainer.Resolution.cs ├── Utility │ ├── ContainerConstants.cs │ └── HashHelpers.cs └── package.snk └── tests ├── Performance ├── Abstracts │ ├── BasicBase.cs │ └── RegistrationBase.cs ├── Configuration │ └── BenchmarkConfiguration.cs ├── Data │ ├── MultiType.cs │ └── TestData.cs ├── Performance.csproj ├── Program.cs └── Tests │ ├── Registration │ └── Registration.cs │ └── Resolution │ ├── Compiled.cs │ ├── PreCompiled.cs │ ├── PreResolved.cs │ └── Resolved.cs ├── Unity.Diagnostic ├── BuildUp.cs ├── Constructor.cs ├── Cyclic.cs ├── Field.cs ├── Hierarchical.cs ├── Issues.cs ├── Method.cs ├── Overrides.cs ├── Property.cs ├── Registration.cs └── Unity.Specification.Tests.Diagnostic.csproj ├── Unity.Specification ├── BuildUp.cs ├── Constructor.cs ├── Container │ ├── Hierachy.cs │ ├── IsRegistered.cs │ └── Registrations.cs ├── Factory │ ├── Registration.cs │ └── Resolution.cs ├── Field.cs ├── Issues.cs ├── Lifetime.cs ├── Method.cs ├── Parameter.cs ├── Property.cs ├── Registration.cs ├── Resolution │ ├── Array.cs │ ├── Basics.cs │ ├── Deferred.cs │ ├── Enumerable.cs │ ├── Generic.cs │ ├── Lazy.cs │ ├── Mapping.cs │ └── Overrides.cs └── Unity.Specification.Tests.csproj └── Unity.Tests ├── ChildContainer ├── ChildContainerInterfaceChangeFixture.cs ├── ITestContainer.cs ├── TestContainer.cs ├── TestContainer1.cs ├── TestContainer2.cs └── TestContainer3.cs ├── CollectionSupport ├── CollectionSupportFixture.cs ├── ConfigurationTestClass.cs ├── ConfigurationTestClassGeneric.cs ├── ITestInterface.cs ├── TestClass.cs ├── TestClassDerived.cs ├── TestClassWithArrayDependency.cs ├── TestClassWithDependencyArrayConstructor.cs ├── TestClassWithDependencyArrayMethod.cs ├── TestClassWithDependencyArrayProperty.cs ├── TestClassWithDependencyEnumerableConstructor.cs ├── TestClassWithDependencyTypeConstructor.cs ├── TestClassWithDependencyTypeMethod.cs └── TestClassWithEnumerableDependency.cs ├── Container ├── ContainerBuildUpFixture.cs ├── ContainerControlledLifetimeThreadingFixture.cs ├── ContainerDefaultContentFixture.cs ├── SeparateContainerFixture.cs ├── UnityExtensionFixture.cs └── UnityHierarchyFixture.cs ├── ContainerRegistration ├── AnotherTypeImplementation.cs ├── GivenContainerIntrospectionCorrectUsageFixture.cs ├── ITypeAnotherInterface.cs ├── ITypeInterface.cs └── TypeImplementation.cs ├── Extensions ├── DefaultLifetimeExtensionTests.cs ├── DiagnosticExtensionTests.cs └── LegacyExtensionTests.cs ├── Generics ├── ClassWithConstMethodandProperty.cs ├── Foo.cs ├── FooRepository.cs ├── GenMockLogger.cs ├── GenSpecialLogger.cs ├── GenericA.cs ├── GenericB.cs ├── GenericC.cs ├── GenericChainingFixture.cs ├── GenericConstraintsFixture.cs ├── GenericD.cs ├── GenericList.cs ├── GenericParameterFixture.cs ├── GenericResolvedArrayParameterFixture.cs ├── GenericsFixture.cs ├── GenericsReflectionExperimentsFixture.cs ├── HaveAGenericType.cs ├── HaveManyGenericTypes.cs ├── HaveManyGenericTypesClosed.cs ├── IFoo.cs ├── IGenLogger.cs ├── IHaveAGenericType.cs ├── IHaveManyGenericTypes.cs ├── IHaveManyGenericTypesClosed.cs ├── IRepository.cs ├── MockRespository.cs ├── Refer.cs └── Refer1.cs ├── Injection ├── InjectedMembersFixture.cs ├── InjectingArraysFixture.cs ├── OptionalDependencyAPIConfigurationFixture.cs ├── OptionalDependencyAttributeFixture.cs ├── OptionalDependencyFixture.cs └── OptionalGenericParameterFixture.cs ├── Issues ├── CodeGenBugFixture.cs ├── CodeplexIssuesFixture.cs └── GitHub.cs ├── Lifetime ├── A.cs ├── AA.cs ├── ATTest.cs ├── B.cs ├── BB.cs ├── HierarchicalLifetimeFixture.cs ├── I1.cs ├── I2.cs ├── ITTest.cs ├── LifetimeFixture.cs ├── PerResolveLifetimeFixture.cs └── UnityTestClass.cs ├── ObjectBuilder ├── BuildPlanAndChildContainerFixture.cs ├── LifetimeContainerTest.cs ├── StagedStrategyChainTest.cs └── Utility │ ├── ActivatorCreationStrategy.cs │ ├── AssertActualExpectedException.cs │ └── AssertHelper.cs ├── Override ├── IForToUndergoeInject.cs ├── IForTypeToInject.cs ├── IInterfaceForTypesToInject.cs ├── IInterfaceForTypesToInjectForPropertyOverride.cs ├── ISubjectTypeToInject.cs ├── ISubjectTypeToInjectForPropertyOverride.cs ├── MultiThreadedPropertyOverrideFixture.cs ├── MySimpleType.cs ├── MySimpleTypeForPropertyOverride.cs ├── SubjectType1ToInject.cs ├── SubjectType1ToInjectForPropertyOverride.cs ├── SubjectType2ToInject.cs ├── SubjectType2ToInjectForPropertyOverride.cs ├── SubjectType3ToInject.cs ├── SubjectType3ToInjectForPropertyOverride.cs ├── TestTypeInConfig.cs ├── TypeBasedOverrideFixture.cs ├── TypeToInject1.cs ├── TypeToInject1ForTypeOverride.cs ├── TypeToInject2.cs ├── TypeToInject2ForTypeOverride.cs ├── TypeToInject3.cs ├── TypeToInject3ForTypeOverride.cs ├── TypeToInjectForPropertyOverride1.cs ├── TypeToInjectForPropertyOverride2.cs ├── TypeToInjectForPropertyOverride3.cs ├── TypeToToUndergoeTypeBasedInject2.cs ├── TypeToToUndergoeTypeBasedInject3.cs └── TypeToUndergoeTypeBasedInject1.cs ├── Storage └── RegistrationSetTests.cs ├── TestDoubles ├── DependencyAttribute.cs ├── InjectionConstructorAttribute.cs ├── InjectionMethodAttribute.cs ├── MockContainerExtension.cs ├── MockContainerExtensionWithNonDefaultConstructor.cs ├── SpyExtension.cs ├── SpyPolicy.cs └── SpyStrategy.cs ├── TestObjects ├── DisposableObject.cs ├── EmailService.cs ├── FileLogger.cs ├── IBase.cs ├── IService.cs ├── NullLogger.cs ├── ObjectWithAmbiguousConstructors.cs ├── ObjectWithAmbiguousMarkedConstructor.cs ├── ObjectWithExplicitInterface.cs ├── ObjectWithInjectionConstructor.cs ├── ObjectWithLotsOfDependencies.cs ├── ObjectWithMarkedConstructor.cs ├── ObjectWithMultipleConstructors.cs ├── ObjectWithOneDependency.cs ├── ObjectWithStaticAndInstanceProperties.cs ├── ObjectWithTwoConstructorDependencies.cs └── OptionalLogger.cs ├── TestSupport ├── AssertExtensions.cs ├── AssertHelper.cs ├── CollectionAssertExtensions.cs ├── EnumerableAssertionExtensions.cs ├── EnumerableExtensions.cs ├── ExtensibilityTestExtension.cs ├── Guard.cs ├── IAdditionalInterface.cs ├── ILogger.cs ├── MockContainerExtension.cs ├── MockDatabase.cs ├── MockLogger.cs ├── NegativeTypeConverter.cs ├── ObjectUsingLogger.cs ├── ObjectWithOneConstructorDependency.cs ├── ObjectWithTwoConstructorParameters.cs ├── ObjectWithTwoProperties.cs ├── Pair.cs ├── Sequence.cs ├── SessionLifetimeManager.cs ├── SpecialLogger.cs └── TypeReflectionExtensions.cs └── Unity.Tests.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | 7 | ############################################################################### 8 | # Prevent merging of README.md 9 | ############################################################################### 10 | README.md merge=ours 11 | .gitmodules merge=ours 12 | *.yml merge=ours -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [ENikS] 2 | open_collective: unity-container 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | --- 8 | name: Bug report 9 | about: Create a report to help us improve 10 | 11 | --- 12 | 13 | ### Description 14 | 15 | A clear and concise description of what is wrong. 16 | 17 | ### To Reproduce 18 | 19 | Please provide UnitTest in the form of: 20 | 21 | ```C# 22 | [TestMethod] 23 | public void SomeDescriptiveName() 24 | { 25 | var container = new UnityContainer() 26 | .RegisterType(); 27 | ... 28 | 29 | var res = container.Resolve>(); 30 | 31 | Assert.SomeFailingVerification(...); 32 | } 33 | ``` 34 | 35 | **Additional context** 36 | 37 | Add any other context about the problem here. 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | ### Description 8 | Briefly explain the feature you are proposing to implement 9 | 10 | ### Problem 11 | Is your feature request related to a problem? Please describe. 12 | 13 | ### Solution 14 | A clear and concise description of what you want to happen. 15 | 16 | ### Impact 17 | Possible impact of the change on existing users. 18 | 19 | 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | [Bb]in/ 15 | [Oo]bj/ 16 | [Ll]ib/ 17 | 18 | # MSTest test Results 19 | [Tt]est[Rr]esult*/ 20 | [Bb]uild[Ll]og.* 21 | 22 | # Visual Studio 2015 cache/options directory 23 | .vs/ 24 | 25 | # DNX 26 | project.lock.json 27 | artifacts/ 28 | 29 | *_i.c 30 | *_p.c 31 | *_i.h 32 | *.ilk 33 | *.meta 34 | *.obj 35 | *.pch 36 | *.pdb 37 | *.pgc 38 | *.pgd 39 | *.rsp 40 | *.sbr 41 | *.tlb 42 | *.tli 43 | *.tlh 44 | *.tmp 45 | *.tmp_proj 46 | *.log 47 | *.vspscc 48 | *.vssscc 49 | .builds 50 | *.pidb 51 | *.log 52 | *.svclog 53 | *.scc 54 | 55 | # Visual C++ cache files 56 | ipch/ 57 | *.aps 58 | *.ncb 59 | *.opensdf 60 | *.sdf 61 | *.cachefile 62 | 63 | # Visual Studio profiler 64 | *.psess 65 | *.vsp 66 | *.vspx 67 | 68 | # Guidance Automation Toolkit 69 | *.gpState 70 | 71 | # ReSharper is a .NET coding add-in 72 | _ReSharper*/ 73 | *.[Rr]e[Ss]harper 74 | *.DotSettings.user 75 | 76 | # Click-Once directory 77 | publish/ 78 | 79 | # Publish Web Output 80 | *.Publish.xml 81 | *.pubxml 82 | *.azurePubxml 83 | 84 | # NuGet Packages Directory 85 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 86 | packages/ 87 | ## TODO: If the tool you use requires repositories.config, also uncomment the next line 88 | !packages/repositories.config 89 | 90 | # Windows Azure Build Output 91 | csx/ 92 | *.build.csdef 93 | 94 | # Windows Store app package directory 95 | AppPackages/ 96 | 97 | # Others 98 | sql/ 99 | *.Cache 100 | ClientBin/ 101 | [Ss]tyle[Cc]op.* 102 | ~$* 103 | *~ 104 | *.dbmdl 105 | *.[Pp]ublish.xml 106 | 107 | *.publishsettings 108 | 109 | # RIA/Silverlight projects 110 | Generated_Code/ 111 | 112 | # Backup & report files from converting an old project file to a newer 113 | # Visual Studio version. Backup files are not needed, because we have git ;-) 114 | _UpgradeReport_Files/ 115 | Backup*/ 116 | UpgradeLog*.XML 117 | UpgradeLog*.htm 118 | 119 | # SQL Server files 120 | App_Data/*.mdf 121 | App_Data/*.ldf 122 | 123 | # ========================= 124 | # Windows detritus 125 | # ========================= 126 | 127 | # Windows image file caches 128 | Thumbs.db 129 | ehthumbs.db 130 | 131 | # Folder config file 132 | Desktop.ini 133 | 134 | # Recycle Bin used on file shares 135 | $RECYCLE.BIN/ 136 | 137 | # Mac desktop service store files 138 | .DS_Store 139 | 140 | _NCrunch* 141 | project.lock.json 142 | testresults.xml 143 | testresults.xml 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build status](https://ci.appveyor.com/api/projects/status/s7s905q6xd6b2503/branch/master?svg=true)](https://ci.appveyor.com/project/unitycontainer/container/branch/master) 2 | ![Regression Tests](https://github.com/unitycontainer/container/workflows/Regression%20Tests/badge.svg?branch=master) 3 | [![License](https://img.shields.io/badge/license-apache%202.0-60C060.svg)](https://github.com/IoC-Unity/Unity/blob/master/LICENSE) 4 | [![NuGet](https://img.shields.io/nuget/dt/Unity.Container.svg)](https://www.nuget.org/packages/Unity.Container) 5 | [![NuGet](https://img.shields.io/nuget/v/Unity.Container.svg)](https://www.nuget.org/packages/Unity.Container) 6 | 7 | ## Overview 8 | 9 | The Unity Container (Unity) is a lightweight, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: 10 | 11 | * Simplified object creation, especially for hierarchical object structures and dependencies 12 | * Abstraction of requirements; this allows developers to specify dependencies at run time or in configuration and simplify management of crosscutting concerns 13 | * Increased flexibility by deferring component configuration to the container 14 | * Service location capability; this allows clients to store or cache the container 15 | * Instance and type interception 16 | * Registration by convention 17 | 18 | ## Code of Conduct 19 | 20 | This project has adopted the code of conduct defined by the [Contributor Covenant](https://www.contributor-covenant.org/) to clarify expected behavior in our community. For more information, see the [.NET Foundation Code of Conduct](https://www.dotnetfoundation.org/code-of-conduct) 21 | 22 | ## Contributing 23 | 24 | See the [Contributing guide](https://github.com/unitycontainer/unity/blob/master/CONTRIBUTING.md) for more information. 25 | 26 | ## .NET Foundation 27 | 28 | Unity Container is a [.NET Foundation](https://dotnetfoundation.org/projects/unitycontainer) project 29 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2019 2 | configuration: Release 3 | platform: Any CPU 4 | 5 | install: 6 | - ps: $env:build_version = (Select-Xml -Path ".\package.props" -XPath "/Project/PropertyGroup/VersionBase" | Select-Object -ExpandProperty Node).InnerText 7 | - ps: Update-AppveyorBuild -Version "$env:build_version.$env:APPVEYOR_BUILD_NUMBER" 8 | 9 | assembly_info: 10 | patch: false 11 | assembly_informational_version: "{version} $(GIT_HASH)" 12 | 13 | dotnet_csproj: 14 | patch: false 15 | 16 | before_build: 17 | - cmd: dotnet restore -v=n 18 | 19 | build: 20 | project: package.sln 21 | parallel: true 22 | verbosity: minimal 23 | 24 | after_build: 25 | - choco install opencover.portable 26 | - choco install codecov 27 | 28 | test_script: 29 | - OpenCover.Console.exe -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --framework net46 --verbosity q" 30 | 31 | after_test: 32 | - codecov -f "results.xml" 33 | 34 | artifacts: 35 | - path: '**\Unity.*.nupkg' 36 | name: 'Unity' 37 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | #ignore: 2 | #- "tests/*" # Ignore tests 3 | #- "src/Utility/*" # Ignore utilities 4 | #- "**/*.Designer.cs" # Ignore generated code 5 | -------------------------------------------------------------------------------- /package.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5.11.11 5 | This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7 6 | 7 | 8 | 9 | 5.11.* 10 | netstandard2.0;netstandard1.0;netcoreapp3.0;netcoreapp2.0;netcoreapp1.0;net48;net47;net46;net45;net40 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /package.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27004.2002 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Container", "src\Unity.Container.csproj", "{EE1F752C-1FAB-41AD-AD63-857D0E62AB6B}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{13CA431F-C840-429D-A83D-BFDEDCCA0F6F}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Tests", "tests\Unity.Tests\Unity.Tests.csproj", "{25E09D23-F407-4A61-8446-E5FBD6F689B8}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {EE1F752C-1FAB-41AD-AD63-857D0E62AB6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {EE1F752C-1FAB-41AD-AD63-857D0E62AB6B}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {EE1F752C-1FAB-41AD-AD63-857D0E62AB6B}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {EE1F752C-1FAB-41AD-AD63-857D0E62AB6B}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {25E09D23-F407-4A61-8446-E5FBD6F689B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {25E09D23-F407-4A61-8446-E5FBD6F689B8}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {25E09D23-F407-4A61-8446-E5FBD6F689B8}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {25E09D23-F407-4A61-8446-E5FBD6F689B8}.Release|Any CPU.Build.0 = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(SolutionProperties) = preSolution 28 | HideSolutionNode = FALSE 29 | EndGlobalSection 30 | GlobalSection(NestedProjects) = preSolution 31 | {25E09D23-F407-4A61-8446-E5FBD6F689B8} = {13CA431F-C840-429D-A83D-BFDEDCCA0F6F} 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {7D864CD1-AEA6-4EDF-B8F8-071CE7F88251} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /src/Builder/Context/BuilderContextExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | using Unity.Resolution; 5 | 6 | namespace Unity.Builder 7 | { 8 | public class BuilderContextExpression : IResolveContextExpression 9 | { 10 | #region Fields 11 | 12 | public static readonly MethodInfo ResolvePropertyMethod = 13 | typeof(BuilderContext).GetTypeInfo() 14 | .GetDeclaredMethods(nameof(BuilderContext.Resolve)) 15 | .First(m => 16 | { 17 | var parameters = m.GetParameters(); 18 | return 0 < parameters.Length && 19 | typeof(PropertyInfo) == parameters[0].ParameterType; 20 | }); 21 | 22 | public static readonly MethodInfo ResolveFieldMethod = 23 | typeof(BuilderContext).GetTypeInfo() 24 | .GetDeclaredMethods(nameof(BuilderContext.Resolve)) 25 | .First(m => 26 | { 27 | var parameters = m.GetParameters(); 28 | return 0 < parameters.Length && 29 | typeof(FieldInfo) == parameters[0].ParameterType; 30 | }); 31 | 32 | public static readonly MethodInfo ResolveParameterMethod = 33 | typeof(BuilderContext).GetTypeInfo() 34 | .GetDeclaredMethods(nameof(BuilderContext.Resolve)) 35 | .First(m => 36 | { 37 | var parameters = m.GetParameters(); 38 | return 0 < parameters.Length && 39 | typeof(ParameterInfo) == parameters[0].ParameterType; 40 | }); 41 | 42 | public static readonly MethodInfo SetMethod = 43 | typeof(BuilderContext).GetTypeInfo() 44 | .GetDeclaredMethods(nameof(BuilderContext.Set)) 45 | .First(m => 2 == m.GetParameters().Length); 46 | 47 | #endregion 48 | 49 | 50 | #region Constructor 51 | 52 | static BuilderContextExpression() 53 | { 54 | var typeInfo = typeof(BuilderContext).GetTypeInfo(); 55 | 56 | 57 | Existing = Expression.MakeMemberAccess(Context, typeInfo.GetDeclaredProperty(nameof(BuilderContext.Existing))); 58 | } 59 | 60 | #endregion 61 | 62 | 63 | #region Public Properties 64 | 65 | public static readonly MemberExpression Existing; 66 | 67 | #endregion 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Builder/Stages/BuilderStage.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Unity.Builder 4 | { 5 | /// 6 | /// Enumeration to represent the object builder stages. 7 | /// 8 | /// 9 | /// The order of the values in the enumeration is the order in which the stages are run. 10 | /// 11 | public enum BuilderStage 12 | { 13 | /// 14 | /// Strategies in this stage run before creation. Typical work done in this stage might 15 | /// include strategies that use reflection to set policies into the context that other 16 | /// strategies would later use. 17 | /// 18 | PreCreation, 19 | 20 | /// 21 | /// Strategies in this stage create objects. Typically you will only have a single policy-driven 22 | /// creation strategy in this stage. 23 | /// 24 | Creation, 25 | 26 | /// 27 | /// Strategies in this stage work on created objects. 28 | /// 29 | Initialization, 30 | 31 | /// 32 | /// Strategies in this stage initialize fields. 33 | /// 34 | Fields, 35 | 36 | /// 37 | /// Strategies in this stage work initialize properties. 38 | /// 39 | Properties, 40 | 41 | /// 42 | /// Strategies in this stage do method calls. 43 | /// 44 | Methods, 45 | 46 | /// 47 | /// Strategies in this stage work on objects that are already initialized. Typical work done in 48 | /// this stage might include looking to see if the object implements some notification interface 49 | /// to discover when its initialization stage has been completed. 50 | /// 51 | PostInitialization 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Builder/Stages/SelectionStage.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Builder 2 | { 3 | public enum SelectionStage 4 | { 5 | Injected = 0, 6 | 7 | Custom = 1, 8 | 9 | Default = 2 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Builder/Stages/UnityBuildStage.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Unity.Builder 4 | { 5 | /// 6 | /// The build stages we use in the Unity container 7 | /// strategy pipeline. 8 | /// 9 | public enum UnityBuildStage 10 | { 11 | /// 12 | /// First stage. By default, nothing happens here. 13 | /// 14 | Setup, 15 | 16 | /// 17 | /// Stage where Array or IEnumerable is resolved 18 | /// 19 | Enumerable, 20 | 21 | /// 22 | /// Third stage. lifetime managers are checked here, 23 | /// and if they're available the rest of the pipeline is skipped. 24 | /// 25 | Lifetime, 26 | 27 | /// 28 | /// Second stage. Type mapping occurs here. 29 | /// 30 | TypeMapping, 31 | 32 | /// 33 | /// Fourth stage. Reflection over constructors, properties, etc. is 34 | /// performed here. 35 | /// 36 | PreCreation, 37 | 38 | /// 39 | /// Fifth stage. Instance creation happens here. 40 | /// 41 | Creation, 42 | 43 | /// 44 | /// Sixth stage. Property sets and method injection happens here. 45 | /// 46 | Initialization, 47 | 48 | /// 49 | /// Seventh and final stage. By default, nothing happens here. 50 | /// 51 | PostInitialization 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Events/ChildContainerCreatedEventArgs.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | using Unity.Extension; 5 | 6 | namespace Unity.Events 7 | { 8 | /// 9 | /// Event argument class for the event. 10 | /// 11 | public class ChildContainerCreatedEventArgs : EventArgs 12 | { 13 | /// 14 | /// Construct a new object with the 15 | /// given child container object. 16 | /// 17 | /// An for the newly created child 18 | /// container. 19 | public ChildContainerCreatedEventArgs(ExtensionContext childContext) 20 | { 21 | ChildContext = childContext; 22 | } 23 | 24 | /// 25 | /// The newly created child container. 26 | /// 27 | public IUnityContainer ChildContainer => ChildContext.Container; 28 | 29 | /// 30 | /// An extension context for the created child container. 31 | /// 32 | public ExtensionContext ChildContext { get; } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Events/NamedEventArgs.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | 5 | namespace Unity.Events 6 | { 7 | /// 8 | /// An EventArgs class that holds a string Name. 9 | /// 10 | public abstract class NamedEventArgs : EventArgs 11 | { 12 | private string _name; 13 | 14 | /// 15 | /// Create a new with a null name. 16 | /// 17 | protected NamedEventArgs() 18 | { 19 | } 20 | 21 | /// 22 | /// Create a new with the given name. 23 | /// 24 | /// Name to store. 25 | protected NamedEventArgs(string name) 26 | { 27 | _name = name; 28 | } 29 | 30 | /// 31 | /// The name. 32 | /// 33 | /// Name used for this EventArg object. 34 | public virtual string Name 35 | { 36 | get => _name; 37 | set => _name = value; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Events/RegisterEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Extension; 3 | using Unity.Lifetime; 4 | 5 | namespace Unity.Events 6 | { 7 | /// 8 | /// Event argument class for the event. 9 | /// 10 | public class RegisterEventArgs : NamedEventArgs 11 | { 12 | /// 13 | /// Create a new instance of . 14 | /// 15 | /// Type to map from. 16 | /// Type to map to. 17 | /// Name for the registration. 18 | /// to manage instances. 19 | public RegisterEventArgs(Type typeFrom, Type typeTo, string name, LifetimeManager lifetimeManager) 20 | : base(name) 21 | { 22 | TypeFrom = typeFrom; 23 | TypeTo = typeTo; 24 | LifetimeManager = lifetimeManager; 25 | } 26 | 27 | /// 28 | /// Type to map from. 29 | /// 30 | public Type TypeFrom { get; } 31 | 32 | /// 33 | /// Type to map to. 34 | /// 35 | public Type TypeTo { get; } 36 | 37 | /// 38 | /// to manage instances. 39 | /// 40 | public LifetimeManager LifetimeManager { get; } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Events/RegisterInstanceEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Extension; 3 | using Unity.Lifetime; 4 | 5 | namespace Unity.Events 6 | { 7 | /// 8 | /// Event argument class for the event. 9 | /// 10 | public class RegisterInstanceEventArgs : NamedEventArgs 11 | { 12 | /// 13 | /// Create a default instance. 14 | /// 15 | public RegisterInstanceEventArgs() 16 | { 17 | } 18 | 19 | /// 20 | /// Create a instance initialized with the given arguments. 21 | /// 22 | /// Type of instance being registered. 23 | /// The instance object itself. 24 | /// Name to register under, null if default registration. 25 | /// object that handles how 26 | /// the instance will be owned. 27 | public RegisterInstanceEventArgs(Type registeredType, object instance, string name, LifetimeManager lifetimeManager) 28 | : base(name) 29 | { 30 | RegisteredType = registeredType; 31 | Instance = instance; 32 | LifetimeManager = lifetimeManager; 33 | } 34 | 35 | /// 36 | /// Type of instance being registered. 37 | /// 38 | /// 39 | /// Type of instance being registered. 40 | /// 41 | public Type RegisteredType { get; } 42 | 43 | /// 44 | /// Instance object being registered. 45 | /// 46 | /// Instance object being registered 47 | public object Instance { get; } 48 | 49 | /// 50 | /// that controls ownership of 51 | /// this instance. 52 | /// 53 | public LifetimeManager LifetimeManager { get; } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Exceptions/DependencyMissingException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | 4 | namespace Unity.Exceptions 5 | { 6 | /// 7 | /// Represents that a dependency could not be resolved. 8 | /// 9 | public class DependencyMissingException : Exception 10 | { 11 | /// 12 | /// Initializes a new instance of the class with no extra information. 13 | /// 14 | public DependencyMissingException() 15 | { 16 | } 17 | 18 | /// 19 | /// Initializes a new instance of the class with the given message. 20 | /// 21 | /// Some random message. 22 | public DependencyMissingException(string message) 23 | : base(message) 24 | { 25 | } 26 | 27 | /// 28 | /// Initialize a new instance of the class with the given 29 | /// message and inner exception. 30 | /// 31 | /// Some random message 32 | /// Inner exception. 33 | public DependencyMissingException(string message, Exception innerException) 34 | : base(message, innerException) 35 | { 36 | } 37 | 38 | /// 39 | /// Initializes a new instance of the class with the build key of the object begin built. 40 | /// 41 | /// The build key of the object begin built. 42 | public DependencyMissingException(object buildKey) 43 | : base(string.Format(CultureInfo.CurrentCulture, 44 | Error.MissingDependency, 45 | buildKey)) 46 | { 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Exceptions/IllegalInjectionMethodException.Desktop.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Exceptions 4 | { 5 | /// 6 | /// The exception thrown when injection is attempted on a method 7 | /// that is an open generic or has out or ref params. 8 | /// 9 | [Serializable] 10 | public partial class IllegalInjectionMethodException 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Exceptions/IllegalInjectionMethodException.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | 5 | namespace Unity.Exceptions 6 | { 7 | /// 8 | /// The exception thrown when injection is attempted on a method 9 | /// that is an open generic or has out or ref params. 10 | /// 11 | public partial class IllegalInjectionMethodException : Exception 12 | { 13 | /// 14 | /// Construct a new with no 15 | /// message. 16 | /// 17 | public IllegalInjectionMethodException() 18 | { 19 | } 20 | 21 | /// 22 | /// Construct a with the given message 23 | /// 24 | /// Message to return. 25 | public IllegalInjectionMethodException(string message) 26 | : base(message) 27 | { 28 | } 29 | 30 | /// 31 | /// Construct a with the given message 32 | /// and inner exception. 33 | /// 34 | /// Message to return. 35 | /// Inner exception 36 | public IllegalInjectionMethodException(string message, Exception innerException) 37 | : base(message, innerException) 38 | { 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidRegistrationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Exceptions 4 | { 5 | internal class InvalidRegistrationException : Exception 6 | { 7 | public InvalidRegistrationException() 8 | : base() 9 | { 10 | 11 | } 12 | 13 | public InvalidRegistrationException(string message, Exception exception) 14 | : base(message, exception) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Exceptions/MakeGenericTypeFailedException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Exceptions 4 | { 5 | internal class MakeGenericTypeFailedException : Exception 6 | { 7 | public MakeGenericTypeFailedException(ArgumentException innerException) 8 | : base("MakeGenericType failed", innerException) 9 | { 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Extension/IUnityContainerExtensionConfigurator.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | namespace Unity.Extension 5 | { 6 | /// 7 | /// Base interface for all extension configuration interfaces. 8 | /// 9 | public interface IUnityContainerExtensionConfigurator 10 | { 11 | /// 12 | /// Retrieve the container instance that we are currently configuring. 13 | /// 14 | IUnityContainer Container { get; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Extension/UnityContainerExtension.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | 5 | namespace Unity.Extension 6 | { 7 | /// 8 | /// Base class for all extension objects. 9 | /// 10 | public abstract class UnityContainerExtension : IUnityContainerExtensionConfigurator 11 | { 12 | private IUnityContainer _container; 13 | private ExtensionContext _context; 14 | 15 | /// 16 | /// The container calls this method when the extension is added. 17 | /// 18 | /// A instance that gives the 19 | /// extension access to the internals of the container. 20 | public void InitializeExtension(ExtensionContext context) 21 | { 22 | if (context == null) 23 | { 24 | throw new ArgumentNullException(nameof(context)); 25 | } 26 | 27 | _container = context.Container; 28 | _context = context; 29 | Initialize(); 30 | } 31 | 32 | /// 33 | /// The container this extension has been added to. 34 | /// 35 | /// The that this extension has been added to. 36 | public IUnityContainer Container => _container; 37 | 38 | /// 39 | /// The object used to manipulate 40 | /// the inner state of the container. 41 | /// 42 | protected ExtensionContext Context => _context; 43 | 44 | /// 45 | /// Initial the container with this extension's functionality. 46 | /// 47 | /// 48 | /// When overridden in a derived class, this method will modify the given 49 | /// by adding strategies, policies, etc. to 50 | /// install it's functions into the container. 51 | protected abstract void Initialize(); 52 | 53 | /// 54 | /// Removes the extension's functions from the container. 55 | /// 56 | /// 57 | /// 58 | /// This method is called when extensions are being removed from the container. It can be 59 | /// used to do things like disconnect event handlers or clean up member state. You do not 60 | /// need to remove strategies or policies here; the container will do that automatically. 61 | /// 62 | /// 63 | /// The default implementation of this method does nothing. 64 | /// 65 | public virtual void Remove() 66 | { 67 | // Do nothing by default, can be overridden to do whatever you want. 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Extensions/DefaultLifetime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Extension; 3 | using Unity.Lifetime; 4 | 5 | namespace Unity 6 | { 7 | public class DefaultLifetime : UnityContainerExtension 8 | { 9 | protected override void Initialize() 10 | { 11 | } 12 | 13 | #region Public Members 14 | 15 | public ITypeLifetimeManager TypeDefaultLifetime 16 | { 17 | get => (ITypeLifetimeManager)((UnityContainer)Container).TypeLifetimeManager; 18 | set => ((UnityContainer)Container).TypeLifetimeManager = (LifetimeManager)value ?? 19 | throw new ArgumentNullException("Type Lifetime Manager can not be null"); 20 | } 21 | 22 | public IInstanceLifetimeManager InstanceDefaultLifetime 23 | { 24 | get => (IInstanceLifetimeManager)((UnityContainer)Container).InstanceLifetimeManager; 25 | set => ((UnityContainer)Container).InstanceLifetimeManager = (LifetimeManager)value ?? 26 | throw new ArgumentNullException("Instance Lifetime Manager can not be null"); 27 | } 28 | 29 | public IFactoryLifetimeManager FactoryDefaultLifetime 30 | { 31 | get => (IFactoryLifetimeManager)((UnityContainer)Container).FactoryLifetimeManager; 32 | set => ((UnityContainer)Container).FactoryLifetimeManager = (LifetimeManager)value ?? 33 | throw new ArgumentNullException("Factory Lifetime Manager can not be null"); 34 | } 35 | 36 | #endregion 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Extensions/Legacy.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Unity.Builder; 3 | using Unity.Processors; 4 | using Unity.Storage; 5 | 6 | namespace Unity.Extension 7 | { 8 | public class Legacy : UnityContainerExtension 9 | { 10 | protected override void Initialize() 11 | { 12 | var strategies = (StagedStrategyChain)Context.BuildPlanStrategies; 13 | var processor = (ConstructorProcessor)strategies.First(s => s is ConstructorProcessor); 14 | 15 | processor.SelectMethod = processor.LegacySelector; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Extensions/Strategies.cs: -------------------------------------------------------------------------------- 1 | using Unity.Extension; 2 | using Unity.Policy; 3 | 4 | namespace Unity 5 | { 6 | /// 7 | /// This extension forces the container to only use activated strategies during resolution 8 | /// 9 | /// 10 | /// This extension forces compatibility with systems without support for runtime compilers. 11 | /// One of such systems is iOS. 12 | /// 13 | public class ForceActivation : UnityContainerExtension 14 | { 15 | protected override void Initialize() 16 | { 17 | var unity = (UnityContainer)Container; 18 | 19 | unity._buildStrategy = unity.ResolvingFactory; 20 | unity.Defaults.Set(typeof(ResolveDelegateFactory), unity._buildStrategy); 21 | } 22 | } 23 | 24 | /// 25 | /// This extension forces the container to only use compiled strategies during resolution 26 | /// 27 | public class ForceCompillation : UnityContainerExtension 28 | { 29 | protected override void Initialize() 30 | { 31 | var unity = (UnityContainer)Container; 32 | 33 | unity._buildStrategy = unity.CompilingFactory; 34 | unity.Defaults.Set(typeof(ResolveDelegateFactory), unity._buildStrategy); 35 | 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/Factories/DeferredFuncResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Unity.Builder; 4 | using Unity.Policy; 5 | using Unity.Resolution; 6 | 7 | namespace Unity.Factories 8 | { 9 | internal class DeferredFuncResolverFactory 10 | { 11 | private static readonly MethodInfo DeferredResolveMethodInfo 12 | = typeof(DeferredFuncResolverFactory).GetTypeInfo() 13 | .GetDeclaredMethod(nameof(DeferredResolve)); 14 | public static ResolveDelegate DeferredResolveDelegateFactory(ref BuilderContext context) 15 | { 16 | var typeToBuild = context.Type.GetTypeInfo().GenericTypeArguments[0]; 17 | var factoryMethod = DeferredResolveMethodInfo.MakeGenericMethod(typeToBuild); 18 | 19 | return (ResolveDelegate)factoryMethod.CreateDelegate(typeof(ResolveDelegate)); 20 | } 21 | 22 | private static Func DeferredResolve(ref BuilderContext context) 23 | { 24 | var nameToBuild = context.Name; 25 | var container = context.Container; 26 | 27 | return () => (T)container.Resolve(nameToBuild); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Factories/GenericLazyResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Unity.Builder; 4 | using Unity.Lifetime; 5 | using Unity.Resolution; 6 | using Unity.Strategies; 7 | 8 | namespace Unity.Factories 9 | { 10 | /// 11 | /// An Resolver Delegate Factory implementation 12 | /// that constructs a build plan for creating objects. 13 | /// 14 | internal class GenericLazyResolverFactory 15 | { 16 | #region Fields 17 | 18 | private static readonly MethodInfo BuildResolveLazyMethod = 19 | typeof(GenericLazyResolverFactory).GetTypeInfo() 20 | .GetDeclaredMethod(nameof(BuildResolveLazy)); 21 | 22 | #endregion 23 | 24 | 25 | #region ResolveDelegateFactory 26 | 27 | public static ResolveDelegate GetResolver(ref BuilderContext context) 28 | { 29 | var itemType = context.Type.GetTypeInfo().GenericTypeArguments[0]; 30 | var lazyMethod = BuildResolveLazyMethod.MakeGenericMethod(itemType); 31 | 32 | return (ResolveDelegate)lazyMethod.CreateDelegate(typeof(ResolveDelegate)); 33 | } 34 | 35 | #endregion 36 | 37 | 38 | #region Implementation 39 | 40 | private static object BuildResolveLazy(ref BuilderContext context) 41 | { 42 | var container = context.Container; 43 | var name = context.Name; 44 | context.Existing = new Lazy(() => container.Resolve(name)); 45 | 46 | var lifetime = BuilderStrategy.GetPolicy(ref context); 47 | 48 | if (lifetime is PerResolveLifetimeManager) 49 | { 50 | var perBuildLifetime = new InternalPerResolveLifetimeManager(context.Existing); 51 | context.Set(typeof(LifetimeManager), perBuildLifetime); 52 | } 53 | 54 | return context.Existing; 55 | } 56 | 57 | #endregion 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Legacy/DynamicMethod/DynamicBuildPlanGenerationContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | using Unity.Builder; 7 | using Unity.Policy; 8 | using Unity.Resolution; 9 | 10 | namespace Unity.ObjectBuilder.BuildPlan.DynamicMethod 11 | { 12 | /// 13 | /// 14 | /// 15 | public class DynamicBuildPlanGenerationContext 16 | { 17 | private readonly Queue _buildPlanExpressions; 18 | 19 | /// 20 | /// 21 | /// 22 | /// 23 | public DynamicBuildPlanGenerationContext(Type typeToBuild) 24 | { 25 | TypeToBuild = typeToBuild; 26 | _buildPlanExpressions = new Queue(); 27 | } 28 | 29 | /// 30 | /// The type that is to be built with the dynamic build plan. 31 | /// 32 | public Type TypeToBuild { get; } 33 | 34 | /// 35 | /// 36 | /// 37 | /// 38 | public void AddToBuildPlan(Expression expression) 39 | { 40 | _buildPlanExpressions.Enqueue(expression); 41 | } 42 | 43 | internal ResolveDelegate GetBuildMethod() 44 | { 45 | var block = Expression.Block( 46 | _buildPlanExpressions.Concat(new[] { BuilderContextExpression.Existing })); 47 | 48 | var lambda = Expression.Lambda>(block, 49 | BuilderContextExpression.Context); 50 | 51 | var planDelegate = lambda.Compile(); 52 | 53 | return (ref BuilderContext context) => 54 | { 55 | try 56 | { 57 | context.Existing = planDelegate(ref context); 58 | } 59 | catch (TargetInvocationException e) 60 | { 61 | if (e.InnerException != null) throw e.InnerException; 62 | throw; 63 | } 64 | 65 | return context.Existing; 66 | }; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Legacy/IBuildPlanCreatorPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Builder; 3 | 4 | namespace Unity.Policy 5 | { 6 | [Obsolete("This interface has been replaced with Unity.Policy.ResolveDelegateFactory delegate", true)] 7 | public interface IBuildPlanCreatorPolicy 8 | { 9 | /// 10 | /// Create a build plan using the given context and build key. 11 | /// 12 | /// Current build context. 13 | /// 14 | /// 15 | /// The build plan. 16 | IBuildPlanPolicy CreatePlan(ref BuilderContext context, Type type, string name); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Legacy/IBuildPlanPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Builder; 3 | 4 | namespace Unity.Policy 5 | { 6 | [Obsolete("IBuildPlanPolicy has been deprecated, please use ResolveDelegateFactory instead", true)] 7 | public interface IBuildPlanPolicy 8 | { 9 | void BuildUp(ref BuilderContext context); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Legacy/IConstructorSelectorPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Unity.Policy 5 | { 6 | [Obsolete("IConstructorSelectorPolicy has been deprecated, please use ISelect instead", true)] 7 | public interface IConstructorSelectorPolicy : ISelect 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Legacy/IMethodSelectorPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Unity.Builder; 5 | 6 | namespace Unity.Policy 7 | { 8 | [Obsolete("IMethodSelectorPolicy has been deprecated, please use ISelectMembers instead", true)] 9 | public interface IMethodSelectorPolicy 10 | { 11 | IEnumerable SelectMethods(ref BuilderContext context); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Legacy/IPropertySelectorPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Unity.Builder; 5 | 6 | namespace Unity.Policy 7 | { 8 | [Obsolete("IPropertySelectorPolicy has been deprecated, please use IPropertySelectorPolicy instead", true)] 9 | public interface IPropertySelectorPolicy : ISelect 10 | { 11 | IEnumerable SelectProperties(ref BuilderContext context); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Legacy/InjectionParameterValue.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Injection 2 | { 3 | /// 4 | /// Base type for objects that are used to configure parameters for 5 | /// constructor or method injection, or for getting the value to 6 | /// be injected into a property. 7 | /// 8 | public abstract class Injection_ParameterValue 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Legacy/TypedInjectionValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Unity.Injection 5 | { 6 | /// 7 | /// A base class for implementing classes 8 | /// that deal in explicit types. 9 | /// 10 | public abstract class Typed_InjectionValue : Injection_ParameterValue 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Lifetime/ContainerLifetimeManager.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Lifetime 2 | { 3 | /// 4 | /// Internal container lifetime manager. 5 | /// This manager distinguishes internal registration from user mode registration. 6 | /// 7 | /// 8 | /// Works like the ExternallyControlledLifetimeManager, but uses 9 | /// regular instead of weak references 10 | /// 11 | internal class ContainerLifetimeManager : LifetimeManager, IInstanceLifetimeManager 12 | { 13 | public override object GetValue(ILifetimeContainer container = null) 14 | { 15 | return container.Container; 16 | } 17 | 18 | protected override LifetimeManager OnCreateLifetimeManager() 19 | { 20 | return new ContainerLifetimeManager(); 21 | } 22 | 23 | public override bool InUse { get => false; set => base.InUse = false; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Lifetime/InternalPerResolveLifetimeManager.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Unity.Lifetime 4 | { 5 | /// 6 | /// This is a custom lifetime manager that acts like , 7 | /// but also provides a signal to the default build plan, marking the type so that 8 | /// instances are reused across the build up object graph. 9 | /// 10 | internal class InternalPerResolveLifetimeManager : PerResolveLifetimeManager 11 | { 12 | /// 13 | /// Construct a new object that stores the 14 | /// give value. This value will be returned by 15 | /// but is not stored in the lifetime manager, nor is the value disposed. 16 | /// This WithLifetime manager is intended only for internal use, which is why the 17 | /// normal method is not used here. 18 | /// 19 | /// InjectionParameterValue to store. 20 | public InternalPerResolveLifetimeManager(object value) 21 | { 22 | base.value = value; 23 | InUse = true; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Policy/Converter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Unity.Policy 6 | { 7 | public delegate TOutput Converter(TInput input); 8 | } 9 | -------------------------------------------------------------------------------- /src/Policy/IResolveDelegateFactory.cs: -------------------------------------------------------------------------------- 1 | using Unity.Builder; 2 | using Unity.Resolution; 3 | 4 | namespace Unity.Policy 5 | { 6 | public delegate ResolveDelegate ResolveDelegateFactory(ref BuilderContext context); 7 | 8 | public interface IResolveDelegateFactory 9 | { 10 | ResolveDelegate GetResolver(ref BuilderContext context); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Policy/ISelect.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Unity.Policy 6 | { 7 | public interface ISelect where TMemberInfo : MemberInfo 8 | { 9 | IEnumerable Select(Type type, IPolicySet registration); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Processors/Abstracts/MemberExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using System.Reflection; 5 | using Unity.Injection; 6 | 7 | namespace Unity.Processors 8 | { 9 | public abstract partial class MemberProcessor where TMemberInfo : MemberInfo 10 | { 11 | #region Selection Processing 12 | 13 | protected virtual IEnumerable ExpressionsFromSelection(Type type, IEnumerable members) 14 | { 15 | HashSet memberSet = new HashSet(); 16 | 17 | foreach (var member in members) 18 | { 19 | switch (member) 20 | { 21 | // TMemberInfo 22 | case TMemberInfo info: 23 | if (!memberSet.Add(info)) continue; 24 | object value = DependencyAttribute.Instance; 25 | foreach (var node in AttributeFactories) 26 | { 27 | var attribute = GetCustomAttribute(info, node.Type); 28 | if (null == attribute) continue; 29 | 30 | value = null == node.Factory ? (object)attribute : node.Factory(attribute, info, null); 31 | break; 32 | } 33 | 34 | yield return GetResolverExpression(info, value); 35 | break; 36 | 37 | // Injection Member 38 | case InjectionMember injectionMember: 39 | var selection = injectionMember.MemberInfo(type); 40 | if (!memberSet.Add(selection)) continue; 41 | yield return GetResolverExpression(selection, injectionMember.Data); 42 | break; 43 | 44 | // Unknown 45 | default: 46 | throw new InvalidOperationException($"Unknown MemberInfo<{typeof(TMemberInfo)}> type"); 47 | } 48 | } 49 | } 50 | 51 | #endregion 52 | 53 | 54 | #region Implementation 55 | 56 | protected abstract Expression GetResolverExpression(TMemberInfo info, object resolver); 57 | 58 | #endregion 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Processors/Abstracts/MemberResolution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Unity.Builder; 5 | using Unity.Injection; 6 | using Unity.Resolution; 7 | 8 | namespace Unity.Processors 9 | { 10 | public abstract partial class MemberProcessor where TMemberInfo : MemberInfo 11 | { 12 | #region Selection Processing 13 | 14 | protected virtual IEnumerable> ResolversFromSelection(Type type, IEnumerable members) 15 | { 16 | HashSet memberSet = new HashSet(); 17 | 18 | foreach (var member in members) 19 | { 20 | switch (member) 21 | { 22 | // MemberInfo 23 | case TMemberInfo info: 24 | if (!memberSet.Add(info)) continue; 25 | object value = DependencyAttribute.Instance; 26 | foreach (var node in AttributeFactories) 27 | { 28 | var attribute = GetCustomAttribute(info, node.Type); 29 | if (null == attribute) continue; 30 | 31 | value = null == node.Factory ? (object)attribute : node.Factory(attribute, info, null); 32 | break; 33 | } 34 | yield return GetResolverDelegate(info, value); 35 | break; 36 | 37 | // Injection Member 38 | case InjectionMember injectionMember: 39 | var selection = injectionMember.MemberInfo(type); 40 | if (!memberSet.Add(selection)) continue; 41 | yield return GetResolverDelegate(selection, injectionMember.Data); 42 | break; 43 | 44 | // Unknown 45 | default: 46 | throw new InvalidOperationException($"Unknown MemberInfo<{typeof(TMemberInfo)}> type"); 47 | } 48 | } 49 | } 50 | 51 | #endregion 52 | 53 | 54 | #region Implementation 55 | 56 | protected abstract ResolveDelegate GetResolverDelegate(TMemberInfo info, object resolver); 57 | 58 | #endregion 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Processors/Fields/FieldProcessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | using Unity.Builder; 7 | using Unity.Policy; 8 | using Unity.Resolution; 9 | 10 | namespace Unity.Processors 11 | { 12 | public class FieldProcessor : MemberProcessor 13 | { 14 | #region Constructors 15 | 16 | public FieldProcessor(IPolicySet policySet) 17 | : base(policySet) 18 | { 19 | } 20 | 21 | #endregion 22 | 23 | 24 | #region Overrides 25 | 26 | protected override IEnumerable DeclaredMembers(Type type) 27 | { 28 | return type.GetDeclaredFields() 29 | .Where(member => !member.IsFamily && !member.IsPrivate && 30 | !member.IsInitOnly && !member.IsStatic); 31 | } 32 | 33 | protected override Type MemberType(FieldInfo info) => info.FieldType; 34 | 35 | #endregion 36 | 37 | 38 | #region Expression 39 | 40 | protected override Expression GetResolverExpression(FieldInfo info, object resolver) 41 | { 42 | return Expression.Assign( 43 | Expression.Field(Expression.Convert(BuilderContextExpression.Existing, info.DeclaringType), info), 44 | Expression.Convert( 45 | Expression.Call(BuilderContextExpression.Context, 46 | BuilderContextExpression.ResolveFieldMethod, 47 | Expression.Constant(info, typeof(FieldInfo)), 48 | Expression.Constant(PreProcessResolver(info, resolver), typeof(object))), 49 | info.FieldType)); 50 | } 51 | 52 | #endregion 53 | 54 | 55 | #region Resolution 56 | 57 | protected override ResolveDelegate GetResolverDelegate(FieldInfo info, object resolver) 58 | { 59 | var value = PreProcessResolver(info, resolver); 60 | return (ref BuilderContext context) => 61 | { 62 | info.SetValue(context.Existing, context.Resolve(info, value)); 63 | return context.Existing; 64 | }; 65 | } 66 | 67 | #endregion 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Processors/Methods/MethodProcessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | using Unity.Builder; 7 | using Unity.Exceptions; 8 | using Unity.Policy; 9 | using Unity.Resolution; 10 | 11 | namespace Unity.Processors 12 | { 13 | public class MethodProcessor : ParametersProcessor 14 | { 15 | #region Constructors 16 | 17 | public MethodProcessor(IPolicySet policySet, UnityContainer container) 18 | : base(policySet, typeof(InjectionMethodAttribute), container) 19 | { 20 | } 21 | 22 | #endregion 23 | 24 | 25 | #region Selection 26 | 27 | protected override IEnumerable DeclaredMembers(Type type) 28 | { 29 | return type.GetDeclaredMethods() 30 | .Where(member => !member.IsFamily && 31 | !member.IsPrivate && 32 | !member.IsStatic); 33 | } 34 | 35 | #endregion 36 | 37 | 38 | #region Expression 39 | 40 | protected override Expression GetResolverExpression(MethodInfo info, object resolvers) 41 | { 42 | try 43 | { 44 | return Expression.Call( 45 | Expression.Convert(BuilderContextExpression.Existing, info.DeclaringType), 46 | info, CreateParameterExpressions(info.GetParameters(), resolvers)); 47 | } 48 | catch (ArgumentException ex) 49 | { 50 | throw new InvalidRegistrationException("Invalid Argument", ex); 51 | } 52 | } 53 | 54 | #endregion 55 | 56 | 57 | #region Resolution 58 | 59 | protected override ResolveDelegate GetResolverDelegate(MethodInfo info, object resolvers) 60 | { 61 | var parameterResolvers = CreateParameterResolvers(info.GetParameters(), resolvers).ToArray(); 62 | return (ref BuilderContext c) => 63 | { 64 | if (null == c.Existing) return c.Existing; 65 | 66 | var parameters = new object[parameterResolvers.Length]; 67 | for (var i = 0; i < parameters.Length; i++) 68 | parameters[i] = parameterResolvers[i](ref c); 69 | 70 | info.Invoke(c.Existing, parameters); 71 | 72 | return c.Existing; 73 | }; 74 | } 75 | 76 | #endregion 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Processors/Properties/PropertyProcessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using System.Reflection; 5 | using Unity.Builder; 6 | using Unity.Policy; 7 | using Unity.Resolution; 8 | 9 | namespace Unity.Processors 10 | { 11 | public class PropertyProcessor : MemberProcessor 12 | { 13 | #region Constructors 14 | 15 | public PropertyProcessor(IPolicySet policySet) 16 | : base(policySet) 17 | { 18 | } 19 | 20 | #endregion 21 | 22 | 23 | #region Overrides 24 | 25 | protected override Type MemberType(PropertyInfo info) => info.PropertyType; 26 | 27 | protected override IEnumerable DeclaredMembers(Type type) 28 | { 29 | foreach (var member in type.GetDeclaredProperties()) 30 | { 31 | if (!member.CanWrite || 0 != member.GetIndexParameters().Length) 32 | continue; 33 | 34 | var setter = member.GetSetMethod(true); 35 | if (setter.IsPrivate || setter.IsFamily) 36 | continue; 37 | 38 | yield return member; 39 | } 40 | } 41 | 42 | #endregion 43 | 44 | 45 | #region Expression 46 | 47 | protected override Expression GetResolverExpression(PropertyInfo info, object resolver) 48 | { 49 | return Expression.Assign( 50 | Expression.Property(Expression.Convert(BuilderContextExpression.Existing, info.DeclaringType), info), 51 | Expression.Convert( 52 | Expression.Call(BuilderContextExpression.Context, 53 | BuilderContextExpression.ResolvePropertyMethod, 54 | Expression.Constant(info, typeof(PropertyInfo)), 55 | Expression.Constant(PreProcessResolver(info, resolver), typeof(object))), 56 | info.PropertyType)); 57 | } 58 | 59 | #endregion 60 | 61 | 62 | #region Resolution 63 | 64 | protected override ResolveDelegate GetResolverDelegate(PropertyInfo info, object resolver) 65 | { 66 | var value = PreProcessResolver(info, resolver); 67 | return (ref BuilderContext context) => 68 | { 69 | #if NET40 70 | info.SetValue(context.Existing, context.Resolve(info, value), null); 71 | #else 72 | info.SetValue(context.Existing, context.Resolve(info, value)); 73 | #endif 74 | return context.Existing; 75 | }; 76 | } 77 | 78 | #endregion 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | using System.Security; 4 | 5 | [assembly: CLSCompliant(true)] 6 | [assembly: AllowPartiallyTrustedCallers] 7 | 8 | 9 | [assembly: InternalsVisibleTo("Unity.Microsoft.DependencyInjection, PublicKey=" + 10 | "002400000480000094000000060200000024000052534131000400000100010037b16015885a7a" + 11 | "c3c63f3c10b23972ec0dfd6db643eaef45ea2297bdfdc53b1945017fc76fd038dc6e7bf9190024" + 12 | "d5435fa49630fdfd143e3149a1506b895fbcce017df1d4f0eac6f05f6d257be45c7be9a8aa8d3d" + 13 | "4164892dc75e7c379a22da0d986db393fbd09e4ba42398c80a305361553ef90eb3484d9cf12df9" + 14 | "0fc0e6e3")] 15 | -------------------------------------------------------------------------------- /src/Registration/ContainerRegistration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using Unity.Injection; 4 | using Unity.Lifetime; 5 | using Unity.Storage; 6 | 7 | namespace Unity.Registration 8 | { 9 | [DebuggerDisplay("ContainerRegistration: MappedTo={Type?.Name ?? string.Empty}, {LifetimeManager?.GetType()?.Name}")] 10 | public class ContainerRegistration : InternalRegistration 11 | { 12 | #region Constructors 13 | 14 | public ContainerRegistration(LinkedNode validators, Type mappedTo, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers = null) 15 | { 16 | Type = mappedTo; 17 | Key = typeof(LifetimeManager); 18 | Value = lifetimeManager; 19 | LifetimeManager.InUse = true; 20 | InjectionMembers = injectionMembers; 21 | Next = validators; 22 | } 23 | 24 | #endregion 25 | 26 | 27 | #region IContainerRegistration 28 | 29 | /// 30 | /// The type that this registration is mapped to. If no type mapping was done, the 31 | /// property and this one will have the same value. 32 | /// 33 | public virtual Type Type { get; } 34 | 35 | /// 36 | /// The lifetime manager for this registration. 37 | /// 38 | /// 39 | /// This property will be null if this registration is for an open generic. 40 | public virtual LifetimeManager LifetimeManager => (LifetimeManager)Value; 41 | 42 | #endregion 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Storage/IRegistry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Unity.Storage 5 | { 6 | 7 | public interface IRegistry 8 | { 9 | TValue this[TKey index] { get; set; } 10 | 11 | bool RequireToGrow { get; } 12 | 13 | IEnumerable Keys { get; } 14 | 15 | IEnumerable Values { get; } 16 | 17 | TValue GetOrAdd(TKey key, Func factory); 18 | 19 | TValue SetOrReplace(TKey key, TValue value); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Storage/IStagedStrategyChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Storage 4 | { 5 | /// 6 | /// This interface defines a standard method to create multi staged strategy chain. 7 | /// 8 | /// The of strategy 9 | /// The stage enum 10 | public interface IStagedStrategyChain 11 | { 12 | 13 | /// 14 | /// Adds a strategy to the chain at a particular stage. 15 | /// 16 | /// The strategy to add to the chain. 17 | /// The stage to add the strategy. 18 | void Add(TStrategyType strategy, TStageEnum stage); 19 | 20 | /// 21 | /// Signals that chain has been changed 22 | /// 23 | event EventHandler Invalidated; 24 | } 25 | 26 | 27 | public static class StagedStrategyChainExtensions 28 | { 29 | /// 30 | /// Add a new strategy for the . 31 | /// 32 | /// The of strategy 33 | /// The stage enum 34 | /// The chain this strategy is added to. 35 | /// The stage to add the strategy to. 36 | public static void AddNew(this IStagedStrategyChain chain, TStageEnum stage) 37 | where TStrategy : new() 38 | { 39 | chain.Add(new TStrategy(), stage); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Storage/LinkedNode.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace Unity.Storage 4 | { 5 | [DebuggerDisplay("Node: Key={Key}, Value={Value}")] 6 | public class LinkedNode 7 | { 8 | public TKey Key; 9 | public TValue Value; 10 | public LinkedNode Next; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Strategies/ArrayResolveStrategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using Unity.Builder; 5 | using Unity.Injection; 6 | using Unity.Policy; 7 | using Unity.Registration; 8 | using Unity.Resolution; 9 | 10 | namespace Unity.Strategies 11 | { 12 | /// 13 | /// This strategy is responsible for building Array 14 | /// 15 | public class ArrayResolveStrategy : BuilderStrategy 16 | { 17 | #region Fields 18 | 19 | private readonly MethodInfo _resolveMethod; 20 | private readonly MethodInfo _resolveGenericMethod; 21 | 22 | #endregion 23 | 24 | 25 | #region Constructors 26 | 27 | public ArrayResolveStrategy(MethodInfo method, MethodInfo generic) 28 | { 29 | _resolveMethod = method; 30 | _resolveGenericMethod = generic; 31 | } 32 | 33 | #endregion 34 | 35 | 36 | #region Registration and Analysis 37 | 38 | public override bool RequiredToBuildType(IUnityContainer container, Type type, InternalRegistration registration, params InjectionMember[] injectionMembers) 39 | { 40 | if (registration is ContainerRegistration containerRegistration) 41 | { 42 | if (type != containerRegistration.Type || 43 | #pragma warning disable CS0618 // TODO: InjectionFactory 44 | null != injectionMembers && injectionMembers.Any(i => i is InjectionFactory)) 45 | #pragma warning restore CS0618 46 | return false; 47 | } 48 | 49 | return null != type && type.IsArray && type.GetArrayRank() == 1; 50 | } 51 | 52 | #endregion 53 | 54 | 55 | #region Build 56 | 57 | public override void PreBuildUp(ref BuilderContext context) 58 | { 59 | var plan = context.Registration.Get>(); 60 | if (plan == null) 61 | { 62 | var typeArgument = context.RegistrationType.GetElementType(); 63 | var type = ((UnityContainer)context.Container).GetFinalType(typeArgument); 64 | 65 | if (type != typeArgument) 66 | { 67 | var method = (ResolveArrayDelegate)_resolveGenericMethod 68 | .MakeGenericMethod(typeArgument) 69 | .CreateDelegate(typeof(ResolveArrayDelegate)); 70 | plan = (ref BuilderContext c) => method(ref c, type); 71 | } 72 | else 73 | { 74 | plan = (ResolveDelegate)_resolveMethod 75 | .MakeGenericMethod(typeArgument) 76 | .CreateDelegate(typeof(ResolveDelegate)); 77 | } 78 | 79 | context.Registration.Set(typeof(ResolveDelegate), plan); 80 | } 81 | 82 | context.Existing = plan(ref context); 83 | context.BuildComplete = true; 84 | } 85 | 86 | #endregion 87 | 88 | 89 | #region Nested Types 90 | 91 | private delegate object ResolveArrayDelegate(ref BuilderContext context, Type type); 92 | 93 | #endregion 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/Unity.Container.csproj.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | True 10 | True 11 | True 12 | True 13 | True 14 | True 15 | True -------------------------------------------------------------------------------- /src/Utility/ContainerConstants.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity 3 | { 4 | internal static class Error 5 | { 6 | public const string MissingDependency = "Could not resolve dependency for build key {0}."; 7 | public const string NoOperationExceptionReason = "while resolving"; 8 | public const string NotAGenericType = "The type {0} is not a generic type, and you are attempting to inject a generic parameter named '{1}'."; 9 | public const string ResolutionFailed = "Resolution of the dependency failed, type = '{0}', name = '{1}'.\nException occurred while: {2}.\nException is: {3} - {4}\n-----------------------------------------------\nAt the time of the exception, the container was: "; 10 | public const string SelectedConstructorHasRefParameters = "The constructor {1} selected for type {0} has ref or out parameters. Such parameters are not supported for constructor injection."; 11 | public const string SelectedConstructorHasRefItself = "The constructor {1} selected for type {0} has reference to itself. Such references create infinite loop during resolving."; 12 | public const string TypesAreNotAssignable = "The type {1} cannot be assigned to variables of type {0}."; 13 | public const string UnknownType = ""; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/Utility/HashHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Utility 4 | { 5 | internal static class HashHelpers 6 | { 7 | // Table of prime numbers to use as hash table sizes. 8 | public static readonly int[] Primes = { 9 | 1, 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919, 10 | 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 11 | 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 12 | 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 13 | 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369}; 14 | 15 | internal const Int32 HashPrime = 101; 16 | 17 | 18 | public static bool IsPrime(int candidate) 19 | { 20 | if ((candidate & 1) != 0) 21 | { 22 | int limit = (int)Math.Sqrt(candidate); 23 | for (int divisor = 3; divisor <= limit; divisor += 2) 24 | { 25 | if ((candidate % divisor) == 0) 26 | return false; 27 | } 28 | return true; 29 | } 30 | return (candidate == 2); 31 | } 32 | 33 | public static int GetPrime(int min) 34 | { 35 | if (min < 0) 36 | throw new ArgumentException("Capacity Overflow"); 37 | 38 | for (int i = 0; i < Primes.Length; i++) 39 | { 40 | int prime = Primes[i]; 41 | if (prime >= min) return prime; 42 | } 43 | 44 | //outside of our predefined table. 45 | //compute the hard way. 46 | for (int i = (min | 1); i < Int32.MaxValue; i += 2) 47 | { 48 | if (IsPrime(i) && ((i - 1) % HashPrime != 0)) 49 | return i; 50 | } 51 | return min; 52 | } 53 | 54 | public static int GetMinPrime() 55 | { 56 | return Primes[0]; 57 | } 58 | 59 | // Returns size of hashtable to grow to. 60 | public static int ExpandPrime(int oldSize) 61 | { 62 | int newSize = 2 * oldSize; 63 | 64 | // Allow the hashtables to grow to maximum possible size (~2G elements) before encoutering capacity overflow. 65 | // Note that this check works even when _items.Length overflowed thanks to the (uint) cast 66 | if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize) 67 | { 68 | return MaxPrimeArrayLength; 69 | } 70 | 71 | return GetPrime(newSize); 72 | } 73 | 74 | 75 | // This is the maximum prime smaller than Array.MaxArrayLength 76 | public const int MaxPrimeArrayLength = 0x7FEFFFFD; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/package.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/unitycontainer/container/7c89f7cb33c91f2dffaeee1af8a2f7b4e8ac2714/src/package.snk -------------------------------------------------------------------------------- /tests/Performance/Configuration/BenchmarkConfiguration.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Configs; 2 | using BenchmarkDotNet.Jobs; 3 | using BenchmarkDotNet.Validators; 4 | 5 | namespace Runner.Setup 6 | { 7 | public class BenchmarkConfiguration : ManualConfig 8 | { 9 | public BenchmarkConfiguration() 10 | { 11 | 12 | Add(Job.Default 13 | .WithUnrollFactor(1) 14 | .WithInvocationCount(1)); 15 | 16 | Add(JitOptimizationsValidator.DontFailOnError); // ALLOW NON-OPTIMIZED DLLS 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Performance/Performance.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net461 6 | true 7 | ..\..\src\package.snk 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/Performance/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Running; 2 | using System.Reflection; 3 | using Unity; 4 | 5 | namespace Performance 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | //var container = new UnityContainer(); 12 | 13 | //var res = container.Resolve(typeof(IUnityContainer), null, null); 14 | 15 | //if (0 == args.Length) 16 | // BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).RunAllJoined(); 17 | //else 18 | BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Performance/Tests/Registration/Registration.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | 3 | namespace Performance.Tests 4 | { 5 | public class Registration : RegistrationBase 6 | { 7 | [Benchmark(Description = "Register (No Mapping)", OperationsPerInvoke = 100)] 8 | public override object Register() => base.Register(); 9 | 10 | [Benchmark(Description = "Register Mapping", OperationsPerInvoke = 100)] 11 | public override object RegisterMapping() => base.RegisterMapping(); 12 | 13 | [Benchmark(Description = "Register Instance", OperationsPerInvoke = 100)] 14 | public override object RegisterInstance() => base.RegisterInstance(); 15 | 16 | [Benchmark(Description = "Registrations.ToArray(100)", OperationsPerInvoke = 100)] 17 | public override object Registrations() => base.Registrations(); 18 | 19 | [Benchmark(Description = "IsRegistered (True)", OperationsPerInvoke = 100)] 20 | public override object IsRegistered() => base.IsRegistered(); 21 | 22 | [Benchmark(Description = "IsRegistered (False)", OperationsPerInvoke = 100)] 23 | public override object IsRegisteredFalse() => base.IsRegisteredFalse(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Performance/Tests/Resolution/Compiled.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using Unity; 3 | 4 | namespace Performance.Tests 5 | { 6 | public class Compiled : BasicBase 7 | { 8 | const string name = nameof(Compiled); 9 | 10 | [IterationSetup] 11 | public override void SetupContainer() 12 | { 13 | _container = new UnityContainer().AddExtension(new ForceCompillation()); 14 | 15 | base.SetupContainer(); 16 | } 17 | 18 | [Benchmark(Description = "(" + name + ") IUnityContainer", OperationsPerInvoke = 20)] 19 | public override object UnityContainer() => base.UnityContainer(); 20 | 21 | [Benchmark(Description = " IUnityContainerAsync", OperationsPerInvoke = 20)] 22 | public override object UnityContainerAsync() => base.UnityContainerAsync(); 23 | 24 | [Benchmark(Description = "Factory (c,t,n)=>new Foo()", OperationsPerInvoke = 20)] 25 | public override object Factory() => base.Factory(); 26 | 27 | [Benchmark(Description = "Factory (with name)", OperationsPerInvoke = 20)] 28 | public override object LegacyFactory() => base.LegacyFactory(); 29 | 30 | [Benchmark(Description = "Instance", OperationsPerInvoke = 20)] 31 | public override object Instance() => base.Instance(); 32 | 33 | [Benchmark(Description = "Unregistered type", OperationsPerInvoke = 20)] 34 | public override object Unregistered() => base.Unregistered(); 35 | 36 | [Benchmark(Description = "Registered type with dependencies", OperationsPerInvoke = 20)] 37 | public override object Transient() => base.Transient(); 38 | 39 | [Benchmark(Description = "Registered interface to type mapping", OperationsPerInvoke = 20)] 40 | public override object Mapping() => base.Mapping(); 41 | 42 | [Benchmark(Description = "Mapping to Singleton", OperationsPerInvoke = 20)] 43 | public override object MappingToSingleton() => base.MappingToSingleton(); 44 | 45 | [Benchmark(Description = "Registered generic type mapping", OperationsPerInvoke = 20)] 46 | public override object GenericInterface() => base.GenericInterface(); 47 | 48 | [Benchmark(Description = "Array", OperationsPerInvoke = 20)] 49 | public override object Array() => base.Array(); 50 | 51 | [Benchmark(Description = "Enumerable", OperationsPerInvoke = 20)] 52 | public override object Enumerable() => base.Enumerable(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/Performance/Tests/Resolution/PreCompiled.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using Unity; 3 | 4 | namespace Performance.Tests 5 | { 6 | public class PreCompiled : BasicBase 7 | { 8 | const string name = nameof(PreCompiled); 9 | 10 | [IterationSetup] 11 | public override void SetupContainer() 12 | { 13 | _container = new UnityContainer().AddExtension(new ForceCompillation()); 14 | 15 | base.SetupContainer(); 16 | 17 | for (var i = 0; i < 3; i++) 18 | { 19 | base.UnityContainer(); 20 | base.UnityContainerAsync(); 21 | base.LegacyFactory(); 22 | base.Factory(); 23 | base.Instance(); 24 | base.Unregistered(); 25 | base.Transient(); 26 | base.Mapping(); 27 | base.MappingToSingleton(); 28 | base.GenericInterface(); 29 | base.Array(); 30 | base.Enumerable(); 31 | } 32 | } 33 | 34 | [Benchmark(Description = "(" + name + ") IUnityContainer", OperationsPerInvoke = 20)] 35 | public override object UnityContainer() => base.UnityContainer(); 36 | 37 | [Benchmark(Description = " IUnityContainerAsync", OperationsPerInvoke = 20)] 38 | public override object UnityContainerAsync() => base.UnityContainerAsync(); 39 | 40 | [Benchmark(Description = "Factory (c,t,n)=>new Foo()", OperationsPerInvoke = 20)] 41 | public override object Factory() => base.Factory(); 42 | 43 | [Benchmark(Description = "Factory (with name)", OperationsPerInvoke = 20)] 44 | public override object LegacyFactory() => base.LegacyFactory(); 45 | 46 | [Benchmark(Description = "Instance", OperationsPerInvoke = 20)] 47 | public override object Instance() => base.Instance(); 48 | 49 | [Benchmark(Description = "Unregistered type", OperationsPerInvoke = 20)] 50 | public override object Unregistered() => base.Unregistered(); 51 | 52 | [Benchmark(Description = "Registered type with dependencies", OperationsPerInvoke = 20)] 53 | public override object Transient() => base.Transient(); 54 | 55 | [Benchmark(Description = "Registered interface to type mapping", OperationsPerInvoke = 20)] 56 | public override object Mapping() => base.Mapping(); 57 | 58 | [Benchmark(Description = "Mapping to Singleton", OperationsPerInvoke = 20)] 59 | public override object MappingToSingleton() => base.MappingToSingleton(); 60 | 61 | [Benchmark(Description = "Registered generic type mapping", OperationsPerInvoke = 20)] 62 | public override object GenericInterface() => base.GenericInterface(); 63 | 64 | [Benchmark(Description = "Array", OperationsPerInvoke = 20)] 65 | public override object Array() => base.Array(); 66 | 67 | [Benchmark(Description = "Enumerable", OperationsPerInvoke = 20)] 68 | public override object Enumerable() => base.Enumerable(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /tests/Performance/Tests/Resolution/PreResolved.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using Unity; 3 | 4 | namespace Performance.Tests 5 | { 6 | public class PreResolved : BasicBase 7 | { 8 | const string name = nameof(PreResolved); 9 | 10 | [IterationSetup] 11 | public override void SetupContainer() 12 | { 13 | _container = new UnityContainer().AddExtension(new ForceActivation()); 14 | 15 | base.SetupContainer(); 16 | 17 | for (var i = 0; i < 3; i++) 18 | { 19 | base.UnityContainer(); 20 | base.LegacyFactory(); 21 | base.Factory(); 22 | base.Instance(); 23 | base.Unregistered(); 24 | base.Transient(); 25 | base.Mapping(); 26 | base.MappingToSingleton(); 27 | base.GenericInterface(); 28 | base.Array(); 29 | base.Enumerable(); 30 | } 31 | } 32 | 33 | [Benchmark(Description = "(" + name + ") IUnityContainer", OperationsPerInvoke = 20)] 34 | public override object UnityContainer() => base.UnityContainer(); 35 | 36 | [Benchmark(Description = " IUnityContainerAsync", OperationsPerInvoke = 20)] 37 | public override object UnityContainerAsync() => base.UnityContainerAsync(); 38 | 39 | [Benchmark(Description = "Factory (c,t,n)=>new Foo()", OperationsPerInvoke = 20)] 40 | public override object Factory() => base.Factory(); 41 | 42 | [Benchmark(Description = "Factory (with name)", OperationsPerInvoke = 20)] 43 | public override object LegacyFactory() => base.LegacyFactory(); 44 | 45 | [Benchmark(Description = "Instance", OperationsPerInvoke = 20)] 46 | public override object Instance() => base.Instance(); 47 | 48 | [Benchmark(Description = "Unregistered type", OperationsPerInvoke = 20)] 49 | public override object Unregistered() => base.Unregistered(); 50 | 51 | [Benchmark(Description = "Registered type with dependencies", OperationsPerInvoke = 20)] 52 | public override object Transient() => base.Transient(); 53 | 54 | [Benchmark(Description = "Registered interface to type mapping", OperationsPerInvoke = 20)] 55 | public override object Mapping() => base.Mapping(); 56 | 57 | [Benchmark(Description = "Registered generic type mapping", OperationsPerInvoke = 20)] 58 | public override object GenericInterface() => base.GenericInterface(); 59 | 60 | [Benchmark(Description = "Mapping to Singleton", OperationsPerInvoke = 20)] 61 | public override object MappingToSingleton() => base.MappingToSingleton(); 62 | 63 | [Benchmark(Description = "Array", OperationsPerInvoke = 20)] 64 | public override object Array() => base.Array(); 65 | 66 | [Benchmark(Description = "Enumerable", OperationsPerInvoke = 20)] 67 | public override object Enumerable() => base.Enumerable(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/Performance/Tests/Resolution/Resolved.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using Unity; 3 | 4 | namespace Performance.Tests 5 | { 6 | public class Resolved : BasicBase 7 | { 8 | const string name = nameof(Resolved); 9 | 10 | [IterationSetup] 11 | public override void SetupContainer() 12 | { 13 | _container = new UnityContainer().AddExtension(new ForceActivation()); 14 | 15 | base.SetupContainer(); 16 | } 17 | 18 | [Benchmark(Description = "(" + name + ") IUnityContainer", OperationsPerInvoke = 20)] 19 | public override object UnityContainer() => base.UnityContainer(); 20 | 21 | [Benchmark(Description = " IUnityContainerAsync", OperationsPerInvoke = 20)] 22 | public override object UnityContainerAsync() => base.UnityContainerAsync(); 23 | 24 | [Benchmark(Description = "Factory (c,t,n)=>new Foo()", OperationsPerInvoke = 20)] 25 | public override object Factory() => base.Factory(); 26 | 27 | [Benchmark(Description = "Factory (with name)", OperationsPerInvoke = 20)] 28 | public override object LegacyFactory() => base.LegacyFactory(); 29 | 30 | [Benchmark(Description = "Instance", OperationsPerInvoke = 20)] 31 | public override object Instance() => base.Instance(); 32 | 33 | [Benchmark(Description = "Unregistered type", OperationsPerInvoke = 20)] 34 | public override object Unregistered() => base.Unregistered(); 35 | 36 | [Benchmark(Description = "Registered type with dependencies", OperationsPerInvoke = 20)] 37 | public override object Transient() => base.Transient(); 38 | 39 | [Benchmark(Description = "Registered interface to type mapping", OperationsPerInvoke = 20)] 40 | public override object Mapping() => base.Mapping(); 41 | 42 | [Benchmark(Description = "Registered generic type mapping", OperationsPerInvoke = 20)] 43 | public override object GenericInterface() => base.GenericInterface(); 44 | 45 | [Benchmark(Description = "Mapping to Singleton", OperationsPerInvoke = 20)] 46 | public override object MappingToSingleton() => base.MappingToSingleton(); 47 | 48 | [Benchmark(Description = "Array", OperationsPerInvoke = 20)] 49 | public override object Array() => base.Array(); 50 | 51 | [Benchmark(Description = "Enumerable", OperationsPerInvoke = 20)] 52 | public override object Enumerable() => base.Enumerable(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/BuildUp.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | [TestClass] 7 | public class BuildUp : Unity.Specification.Diagnostic.BuildUp.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()) 12 | .AddExtension(new Diagnostic()); 13 | } 14 | } 15 | } 16 | 17 | namespace Resolved 18 | { 19 | [TestClass] 20 | public class BuildUp : Unity.Specification.Diagnostic.BuildUp.SpecificationTests 21 | { 22 | public override IUnityContainer GetContainer() 23 | { 24 | return new UnityContainer().AddExtension(new ForceActivation()) 25 | .AddExtension(new Diagnostic()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Cyclic.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | 7 | [TestClass] 8 | public class Cyclic : Unity.Specification.Diagnostic.Cyclic.SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer().AddExtension(new ForceCompillation()) 13 | .AddExtension(new Diagnostic()); 14 | } 15 | } 16 | } 17 | 18 | namespace Resolved 19 | { 20 | 21 | [TestClass] 22 | public class Cyclic : Unity.Specification.Diagnostic.Cyclic.SpecificationTests 23 | { 24 | public override IUnityContainer GetContainer() 25 | { 26 | return new UnityContainer().AddExtension(new ForceActivation()) 27 | .AddExtension(new Diagnostic()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Field.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | 7 | [TestClass] 8 | public class Field : Unity.Specification.Diagnostic.Field.Validation.SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer().AddExtension(new ForceCompillation()) 13 | .AddExtension(new Diagnostic()); 14 | } 15 | } 16 | } 17 | 18 | namespace Resolved 19 | { 20 | 21 | [TestClass] 22 | public class Field : Unity.Specification.Diagnostic.Field.Validation.SpecificationTests 23 | { 24 | public override IUnityContainer GetContainer() 25 | { 26 | return new UnityContainer().AddExtension(new ForceActivation()) 27 | .AddExtension(new Diagnostic()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Hierarchical.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | [TestClass] 7 | public class Hierarchical : Unity.Specification.Diagnostic.Hierarchical.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()) 12 | .AddExtension(new Diagnostic()); 13 | } 14 | } 15 | } 16 | 17 | namespace Resolved 18 | { 19 | [TestClass] 20 | public class Hierarchical : Unity.Specification.Diagnostic.Hierarchical.SpecificationTests 21 | { 22 | public override IUnityContainer GetContainer() 23 | { 24 | return new UnityContainer().AddExtension(new ForceActivation()) 25 | .AddExtension(new Diagnostic()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Issues.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Issues 5 | { 6 | [TestClass] 7 | public class GitHub : Unity.Specification.Diagnostic.Issues.GitHub.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddNewExtension(); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class CodePlex : Unity.Specification.Issues.Codeplex.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddNewExtension(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Method.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Extension; 4 | 5 | namespace Compiled.Method 6 | { 7 | [TestClass] 8 | public class Parameters : Unity.Specification.Diagnostic.Method.Parameters.SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer().AddExtension(new ForceCompillation()) 13 | .AddExtension(new Diagnostic()); 14 | } 15 | } 16 | 17 | [TestClass] 18 | public class Validation : Unity.Specification.Diagnostic.Method.Validation.SpecificationTests 19 | { 20 | public override IUnityContainer GetContainer() 21 | { 22 | return new UnityContainer().AddExtension(new ForceCompillation()) 23 | .AddExtension(new Diagnostic()); 24 | } 25 | } 26 | } 27 | 28 | namespace Resolved.Method 29 | { 30 | [TestClass] 31 | public class Parameters : Unity.Specification.Diagnostic.Method.Parameters.SpecificationTests 32 | { 33 | public override IUnityContainer GetContainer() 34 | { 35 | return new UnityContainer().AddExtension(new ForceActivation()) 36 | .AddExtension(new Diagnostic()); 37 | } 38 | } 39 | 40 | [TestClass] 41 | public class Validation : Unity.Specification.Diagnostic.Method.Validation.SpecificationTests 42 | { 43 | public override IUnityContainer GetContainer() 44 | { 45 | return new UnityContainer().AddExtension(new ForceActivation()) 46 | .AddExtension(new Diagnostic()); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Overrides.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | 7 | [TestClass] 8 | public class Override : Unity.Specification.Diagnostic.Overrides.SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer().AddExtension(new ForceCompillation()) 13 | .AddExtension(new Diagnostic()); 14 | } 15 | } 16 | } 17 | 18 | namespace Resolved 19 | { 20 | 21 | [TestClass] 22 | public class Override : Unity.Specification.Diagnostic.Overrides.SpecificationTests 23 | { 24 | public override IUnityContainer GetContainer() 25 | { 26 | return new UnityContainer().AddExtension(new ForceActivation()) 27 | .AddExtension(new Diagnostic()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Property.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | 7 | [TestClass] 8 | public class Property : Unity.Specification.Diagnostic.Property.Validation.SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer().AddExtension(new ForceCompillation()) 13 | .AddExtension(new Diagnostic()); 14 | } 15 | } 16 | } 17 | 18 | namespace Resolved 19 | { 20 | 21 | [TestClass] 22 | public class Property : Unity.Specification.Diagnostic.Property.Validation.SpecificationTests 23 | { 24 | public override IUnityContainer GetContainer() 25 | { 26 | return new UnityContainer().AddExtension(new ForceActivation()) 27 | .AddExtension(new Diagnostic()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Registration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Registration 5 | { 6 | [TestClass] 7 | public class Types : Unity.Specification.Diagnostic.Registration.Types.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new Diagnostic()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Instance : Unity.Specification.Diagnostic.Registration.Instance.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new Diagnostic()); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Factory : Unity.Specification.Diagnostic.Registration.Factory.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer().AddExtension(new Diagnostic()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unity.Diagnostic/Unity.Specification.Tests.Diagnostic.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net48 5 | false 6 | true 7 | ..\..\src\package.snk 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /tests/Unity.Specification/BuildUp.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | [TestClass] 7 | public class BuildUp : Unity.Specification.BuildUp.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | } 15 | 16 | 17 | namespace Resolved 18 | { 19 | [TestClass] 20 | public class BuildUp : Unity.Specification.BuildUp.SpecificationTests 21 | { 22 | public override IUnityContainer GetContainer() 23 | { 24 | return new UnityContainer().AddExtension(new ForceActivation()); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Constructor.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled.Constructor 5 | { 6 | [TestClass] 7 | public class Injection : Unity.Specification.Constructor.Injection.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Attribute : Unity.Specification.Constructor.Attribute.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new ForceCompillation()); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Parameters : Unity.Specification.Constructor.Parameters.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer().AddExtension(new ForceCompillation()); 30 | } 31 | } 32 | 33 | [TestClass] 34 | public class Overrides : Unity.Specification.Constructor.Overrides.SpecificationTests 35 | { 36 | public override IUnityContainer GetContainer() 37 | { 38 | return new UnityContainer().AddExtension(new ForceCompillation()); 39 | } 40 | } 41 | } 42 | 43 | 44 | namespace Resolved.Constructor 45 | { 46 | [TestClass] 47 | public class Injection : Unity.Specification.Constructor.Injection.SpecificationTests 48 | { 49 | public override IUnityContainer GetContainer() 50 | { 51 | return new UnityContainer().AddExtension(new ForceActivation()); 52 | } 53 | } 54 | 55 | [TestClass] 56 | public class Attribute : Unity.Specification.Constructor.Attribute.SpecificationTests 57 | { 58 | public override IUnityContainer GetContainer() 59 | { 60 | return new UnityContainer().AddExtension(new ForceActivation()); 61 | } 62 | } 63 | 64 | [TestClass] 65 | public class Parameters : Unity.Specification.Constructor.Parameters.SpecificationTests 66 | { 67 | public override IUnityContainer GetContainer() 68 | { 69 | return new UnityContainer().AddExtension(new ForceActivation()); 70 | } 71 | } 72 | 73 | [TestClass] 74 | public class Overrides : Unity.Specification.Constructor.Overrides.SpecificationTests 75 | { 76 | public override IUnityContainer GetContainer() 77 | { 78 | return new UnityContainer().AddExtension(new ForceActivation()); 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Container/Hierachy.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Container.Hierarchy; 4 | 5 | namespace Container 6 | { 7 | [TestClass] 8 | public class Hierarchy : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Container/IsRegistered.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Container.IsRegistered; 4 | 5 | namespace Container 6 | { 7 | [TestClass] 8 | public class IsRegistered : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Container/Registrations.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Container.Registrations; 4 | 5 | namespace Container 6 | { 7 | [TestClass] 8 | public class Registrations : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Factory/Registration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Factory.Registration; 4 | 5 | namespace Factory 6 | { 7 | [TestClass] 8 | public class Registration : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Factory/Resolution.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Factory.Resolution; 4 | 5 | namespace Factory 6 | { 7 | [TestClass] 8 | public class Resolution : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Field.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled.Field 5 | { 6 | [TestClass] 7 | public class Attribute : Unity.Specification.Field.Attribute.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Injection : Unity.Specification.Field.Injection.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new ForceCompillation()); 21 | } 22 | } 23 | 24 | 25 | [TestClass] 26 | public class Overrides : Unity.Specification.Field.Overrides.SpecificationTests 27 | { 28 | public override IUnityContainer GetContainer() 29 | { 30 | return new UnityContainer().AddExtension(new ForceCompillation()); 31 | } 32 | } 33 | 34 | } 35 | 36 | namespace Resolved.Field 37 | { 38 | [TestClass] 39 | public class Attribute : Unity.Specification.Field.Attribute.SpecificationTests 40 | { 41 | public override IUnityContainer GetContainer() 42 | { 43 | return new UnityContainer().AddExtension(new ForceActivation()); 44 | } 45 | } 46 | 47 | [TestClass] 48 | public class Injection : Unity.Specification.Field.Injection.SpecificationTests 49 | { 50 | public override IUnityContainer GetContainer() 51 | { 52 | return new UnityContainer().AddExtension(new ForceActivation()); 53 | } 54 | } 55 | 56 | [TestClass] 57 | public class Overrides : Unity.Specification.Field.Overrides.SpecificationTests 58 | { 59 | public override IUnityContainer GetContainer() 60 | { 61 | return new UnityContainer().AddExtension(new ForceActivation()); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Issues.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Issues 5 | { 6 | [TestClass] 7 | public class GitHub : Unity.Specification.Issues.GitHub.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer(); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class CodePlex : Unity.Specification.Issues.Codeplex.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Lifetime.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled 5 | { 6 | [TestClass] 7 | public class Lifetime : Unity.Specification.Lifetime.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | } 15 | 16 | namespace Resolved 17 | { 18 | [TestClass] 19 | public class Lifetime : Unity.Specification.Lifetime.SpecificationTests 20 | { 21 | public override IUnityContainer GetContainer() 22 | { 23 | return new UnityContainer().AddExtension(new ForceActivation()); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Method.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled.Method 5 | { 6 | [TestClass] 7 | public class Attribute : Unity.Specification.Method.Attribute.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Injection : Unity.Specification.Method.Injection.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new ForceCompillation()); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Selection : Unity.Specification.Method.Selection.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer().AddExtension(new ForceCompillation()); 30 | } 31 | } 32 | 33 | [TestClass] 34 | public class Parameters : Unity.Specification.Method.Parameters.SpecificationTests 35 | { 36 | public override IUnityContainer GetContainer() 37 | { 38 | return new UnityContainer().AddExtension(new ForceCompillation()); 39 | } 40 | } 41 | 42 | [TestClass] 43 | public class Overrides : Unity.Specification.Method.Overrides.SpecificationTests 44 | { 45 | public override IUnityContainer GetContainer() 46 | { 47 | return new UnityContainer().AddExtension(new ForceCompillation()); 48 | } 49 | } 50 | } 51 | 52 | 53 | 54 | namespace Resolved.Method 55 | { 56 | [TestClass] 57 | public class Attribute : Unity.Specification.Method.Attribute.SpecificationTests 58 | { 59 | public override IUnityContainer GetContainer() 60 | { 61 | return new UnityContainer().AddExtension(new ForceActivation()); 62 | } 63 | } 64 | 65 | [TestClass] 66 | public class Injection : Unity.Specification.Method.Injection.SpecificationTests 67 | { 68 | public override IUnityContainer GetContainer() 69 | { 70 | return new UnityContainer().AddExtension(new ForceActivation()); 71 | } 72 | } 73 | 74 | [TestClass] 75 | public class Selection : Unity.Specification.Method.Selection.SpecificationTests 76 | { 77 | public override IUnityContainer GetContainer() 78 | { 79 | return new UnityContainer().AddExtension(new ForceActivation()); 80 | } 81 | } 82 | 83 | [TestClass] 84 | public class Parameters : Unity.Specification.Method.Parameters.SpecificationTests 85 | { 86 | public override IUnityContainer GetContainer() 87 | { 88 | return new UnityContainer().AddExtension(new ForceActivation()); 89 | } 90 | } 91 | 92 | [TestClass] 93 | public class Overrides : Unity.Specification.Method.Overrides.SpecificationTests 94 | { 95 | public override IUnityContainer GetContainer() 96 | { 97 | return new UnityContainer().AddExtension(new ForceActivation()); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Parameter.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled.Parameter 5 | { 6 | [TestClass] 7 | public class Attribute : Unity.Specification.Parameter.Attribute.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new ForceCompillation()); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Resolved : Unity.Specification.Parameter.Resolved.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer().AddExtension(new ForceCompillation()); 30 | } 31 | } 32 | 33 | [TestClass] 34 | public class Optional : Unity.Specification.Parameter.Optional.SpecificationTests 35 | { 36 | public override IUnityContainer GetContainer() 37 | { 38 | return new UnityContainer().AddExtension(new ForceCompillation()); 39 | } 40 | } 41 | 42 | [TestClass] 43 | public class Overrides : Unity.Specification.Parameter.Overrides.SpecificationTests 44 | { 45 | public override IUnityContainer GetContainer() 46 | { 47 | return new UnityContainer().AddExtension(new ForceCompillation()); 48 | } 49 | } 50 | } 51 | 52 | 53 | 54 | namespace Resolved.Parameter 55 | { 56 | [TestClass] 57 | public class Attribute : Unity.Specification.Parameter.Attribute.SpecificationTests 58 | { 59 | public override IUnityContainer GetContainer() 60 | { 61 | return new UnityContainer().AddExtension(new ForceActivation()); 62 | } 63 | } 64 | 65 | [TestClass] 66 | public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests 67 | { 68 | public override IUnityContainer GetContainer() 69 | { 70 | return new UnityContainer().AddExtension(new ForceActivation()); 71 | } 72 | } 73 | 74 | [TestClass] 75 | public class Resolved : Unity.Specification.Parameter.Resolved.SpecificationTests 76 | { 77 | public override IUnityContainer GetContainer() 78 | { 79 | return new UnityContainer().AddExtension(new ForceActivation()); 80 | } 81 | } 82 | 83 | [TestClass] 84 | public class Optional : Unity.Specification.Parameter.Optional.SpecificationTests 85 | { 86 | public override IUnityContainer GetContainer() 87 | { 88 | return new UnityContainer().AddExtension(new ForceActivation()); 89 | } 90 | } 91 | 92 | [TestClass] 93 | public class Overrides : Unity.Specification.Parameter.Overrides.SpecificationTests 94 | { 95 | public override IUnityContainer GetContainer() 96 | { 97 | return new UnityContainer().AddExtension(new ForceActivation()); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Property.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Compiled.Property 5 | { 6 | [TestClass] 7 | public class Attribute : Unity.Specification.Property.Attribute.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer().AddExtension(new ForceCompillation()); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Injection : Unity.Specification.Property.Injection.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer().AddExtension(new ForceCompillation()); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Override : Unity.Specification.Property.Overrides.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer().AddExtension(new ForceCompillation()); 30 | } 31 | } 32 | } 33 | 34 | 35 | namespace Resolved.Property 36 | { 37 | [TestClass] 38 | public class Attribute : Unity.Specification.Property.Attribute.SpecificationTests 39 | { 40 | public override IUnityContainer GetContainer() 41 | { 42 | return new UnityContainer().AddExtension(new ForceActivation()); 43 | } 44 | } 45 | 46 | [TestClass] 47 | public class Injection : Unity.Specification.Property.Injection.SpecificationTests 48 | { 49 | public override IUnityContainer GetContainer() 50 | { 51 | return new UnityContainer().AddExtension(new ForceActivation()); 52 | } 53 | } 54 | 55 | [TestClass] 56 | public class Override : Unity.Specification.Property.Overrides.SpecificationTests 57 | { 58 | public override IUnityContainer GetContainer() 59 | { 60 | return new UnityContainer().AddExtension(new ForceActivation()); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Registration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Registration 5 | { 6 | [TestClass] 7 | public class Native : Unity.Specification.Registration.Native.SpecificationTests 8 | { 9 | public override IUnityContainer GetContainer() 10 | { 11 | return new UnityContainer(); 12 | } 13 | } 14 | 15 | [TestClass] 16 | public class Extended : Unity.Specification.Registration.Extended.SpecificationTests 17 | { 18 | public override IUnityContainer GetContainer() 19 | { 20 | return new UnityContainer(); 21 | } 22 | } 23 | 24 | [TestClass] 25 | public class Syntax : Unity.Specification.Registration.Syntax.SpecificationTests 26 | { 27 | public override IUnityContainer GetContainer() 28 | { 29 | return new UnityContainer(); 30 | } 31 | } 32 | 33 | [TestClass] 34 | public class Factory : Unity.Specification.Registration.Factory.SpecificationTests 35 | { 36 | public override IUnityContainer GetContainer() 37 | { 38 | return new UnityContainer(); 39 | } 40 | } 41 | 42 | [TestClass] 43 | public class Instance : Unity.Specification.Registration.Instance.SpecificationTests 44 | { 45 | public override IUnityContainer GetContainer() 46 | { 47 | return new UnityContainer(); 48 | } 49 | } 50 | 51 | [TestClass] 52 | public class Types : Unity.Specification.Registration.Types.SpecificationTests 53 | { 54 | public override IUnityContainer GetContainer() 55 | { 56 | return new UnityContainer(); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Array.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Array; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Array : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Basics.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Basics; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Basics : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Deferred.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Deferred; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Deferred : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Enumerable.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Enumerable; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Enumerable : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Generic.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Generic; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Generic : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Lazy.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Lazy; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Lazy : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Mapping.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Mapping; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Mapping : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Resolution/Overrides.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | using Unity.Specification.Resolution.Overrides; 4 | 5 | namespace Resolution 6 | { 7 | [TestClass] 8 | public class Overrides : SpecificationTests 9 | { 10 | public override IUnityContainer GetContainer() 11 | { 12 | return new UnityContainer(); 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /tests/Unity.Specification/Unity.Specification.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net48 5 | false 6 | true 7 | ..\..\src\package.snk 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ChildContainer/ITestContainer.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.ChildContainer 2 | { 3 | public interface ITestContainer 4 | { } 5 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/ChildContainer/TestContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.ChildContainer 4 | { 5 | public class TestContainer : ITestContainer, IDisposable 6 | { 7 | private bool wasDisposed = false; 8 | 9 | public bool WasDisposed 10 | { 11 | get { return wasDisposed; } 12 | set { wasDisposed = value; } 13 | } 14 | 15 | public void Dispose() 16 | { 17 | wasDisposed = true; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/ChildContainer/TestContainer1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.ChildContainer 2 | { 3 | public class TestContainer1 : ITestContainer 4 | { } 5 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/ChildContainer/TestContainer2.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.ChildContainer 2 | { 3 | public class TestContainer2 : ITestContainer 4 | { } 5 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/ChildContainer/TestContainer3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.ChildContainer 4 | { 5 | public class TestContainer3 : ITestContainer, IDisposable 6 | { 7 | private bool wasDisposed = false; 8 | 9 | public bool WasDisposed 10 | { 11 | get { return wasDisposed; } 12 | set { wasDisposed = value; } 13 | } 14 | 15 | public void Dispose() 16 | { 17 | wasDisposed = true; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/ConfigurationTestClass.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class ConfigurationTestClass 4 | { 5 | private TestClass[] arrayProperty; 6 | public TestClass[] ArrayProperty 7 | { 8 | get { return arrayProperty; } 9 | set { arrayProperty = value; } 10 | } 11 | 12 | private TestClass[] arrayMethod; 13 | public TestClass[] ArrayMethod 14 | { 15 | get { return arrayMethod; } 16 | set { arrayMethod = value; } 17 | } 18 | 19 | private TestClass[] arrayCtor; 20 | public TestClass[] ArrayCtor 21 | { 22 | get { return arrayCtor; } 23 | set { arrayCtor = value; } 24 | } 25 | 26 | public void InjectionMethod(TestClass[] arrayMethod) 27 | { 28 | ArrayMethod = arrayMethod; 29 | } 30 | 31 | [InjectionConstructor] 32 | public ConfigurationTestClass() 33 | { } 34 | 35 | public ConfigurationTestClass(TestClass[] arrayCtor) 36 | { 37 | ArrayCtor = arrayCtor; 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/ConfigurationTestClassGeneric.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class ConfigurationTestClassGeneric 4 | { 5 | private T[] arrayProperty; 6 | public T[] ArrayProperty 7 | { 8 | get { return arrayProperty; } 9 | set { arrayProperty = value; } 10 | } 11 | 12 | private T[] arrayMethod; 13 | public T[] ArrayMethod 14 | { 15 | get { return arrayMethod; } 16 | set { arrayMethod = value; } 17 | } 18 | 19 | private T[] arrayCtor; 20 | public T[] ArrayCtor 21 | { 22 | get { return arrayCtor; } 23 | set { arrayCtor = value; } 24 | } 25 | 26 | public void InjectionMethod(T[] arrayMethod) 27 | { 28 | ArrayMethod = arrayMethod; 29 | } 30 | 31 | [InjectionConstructor] 32 | public ConfigurationTestClassGeneric() 33 | { } 34 | 35 | public ConfigurationTestClassGeneric(T[] arrayCtor) 36 | { 37 | ArrayCtor = arrayCtor; 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/ITestInterface.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public interface ITestInterface 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.CollectionSupport 4 | { 5 | public class TestClass : ITestInterface 6 | { 7 | public string ID { get; } = Guid.NewGuid().ToString(); 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassDerived.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassDerived : TestClass { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithArrayDependency.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithArrayDependency 4 | { 5 | [Dependency] 6 | public TestClass[] Dependency { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyArrayConstructor.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithDependencyArrayConstructor 4 | { 5 | public TestClass[] Dependency { get; set; } 6 | 7 | public TestClassWithDependencyArrayConstructor(TestClass[] dependency) 8 | { 9 | Dependency = dependency; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyArrayMethod.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithDependencyArrayMethod 4 | { 5 | public TestClass[] Dependency { get; set; } 6 | 7 | [InjectionMethod] 8 | public void Injector(TestClass[] dependency) 9 | { 10 | Dependency = dependency; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyArrayProperty.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithDependencyArrayProperty 4 | { 5 | [Dependency] 6 | public TestClass[] Dependency { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyEnumerableConstructor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Unity.Tests.v5.CollectionSupport 4 | { 5 | public class TestClassWithDependencyEnumerableConstructor 6 | { 7 | public IEnumerable Dependency { get; set; } 8 | 9 | public TestClassWithDependencyEnumerableConstructor(IEnumerable dependency) 10 | { 11 | Dependency = dependency; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyTypeConstructor.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithDependencyTypeConstructor 4 | { 5 | public TestClass[] Dependency { get; set; } 6 | 7 | public TestClassWithDependencyTypeConstructor(TestClass[] dependency) 8 | { 9 | Dependency = dependency; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithDependencyTypeMethod.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.CollectionSupport 2 | { 3 | public class TestClassWithDependencyTypeMethod 4 | { 5 | public TestClass[] Dependency { get; set; } 6 | 7 | [InjectionMethod] 8 | public void Injector(TestClass[] dependency) 9 | { 10 | Dependency = dependency; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/Unity.Tests/CollectionSupport/TestClassWithEnumerableDependency.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Unity.Tests.v5.CollectionSupport 4 | { 5 | public class TestClassWithEnumerableDependency 6 | { 7 | [Dependency] 8 | public IEnumerable Dependency { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Container/ContainerBuildUpFixture.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/unitycontainer/container/7c89f7cb33c91f2dffaeee1af8a2f7b4e8ac2714/tests/Unity.Tests/Container/ContainerBuildUpFixture.cs -------------------------------------------------------------------------------- /tests/Unity.Tests/Container/ContainerDefaultContentFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace Unity.Tests.v5.Container 4 | { 5 | [TestClass] 6 | public class ContainerDefaultContentFixture 7 | { 8 | [TestMethod] 9 | public void WhenResolvingAnIUnityContainerItResolvesItself() 10 | { 11 | IUnityContainer container = new UnityContainer(); 12 | 13 | IUnityContainer resolvedContainer = container.Resolve(); 14 | 15 | Assert.AreSame(container, resolvedContainer); 16 | } 17 | 18 | [TestMethod] 19 | public void WhenResolveingAnIUnityContainerForAChildContainerItResolvesTheChildContainer() 20 | { 21 | IUnityContainer container = new UnityContainer(); 22 | IUnityContainer childContainer = container.CreateChildContainer(); 23 | 24 | IUnityContainer resolvedContainer = childContainer.Resolve(); 25 | 26 | Assert.AreSame(childContainer, resolvedContainer); 27 | } 28 | 29 | [TestMethod] 30 | public void AClassThatHasADependencyOnTheContainerGetsItInjected() 31 | { 32 | IUnityContainer container = new UnityContainer(); 33 | 34 | IUnityContainerInjectionClass obj; 35 | obj = container.Resolve(); 36 | 37 | Assert.AreSame(container, obj.Container); 38 | } 39 | 40 | public class IUnityContainerInjectionClass 41 | { 42 | [Dependency] 43 | public IUnityContainer Container { get; set; } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ContainerRegistration/AnotherTypeImplementation.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity.Tests.v5.ContainerRegistration 3 | { 4 | internal class AnotherTypeImplementation : ITypeAnotherInterface 5 | { 6 | private readonly string name; 7 | 8 | public AnotherTypeImplementation() 9 | { 10 | } 11 | 12 | public AnotherTypeImplementation(string name) 13 | { 14 | this.name = name; 15 | } 16 | 17 | #region ITypeAnotherInterface Members 18 | 19 | public string GetName() 20 | { 21 | return name; 22 | } 23 | 24 | #endregion 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ContainerRegistration/ITypeAnotherInterface.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity.Tests.v5.ContainerRegistration 3 | { 4 | internal interface ITypeAnotherInterface 5 | { 6 | string GetName(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ContainerRegistration/ITypeInterface.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.ContainerRegistration 2 | { 3 | internal interface ITypeInterface 4 | { 5 | string GetName(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ContainerRegistration/TypeImplementation.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.ContainerRegistration 2 | { 3 | internal class TypeImplementation : ITypeInterface 4 | { 5 | private string name; 6 | 7 | public TypeImplementation() 8 | { 9 | } 10 | 11 | public TypeImplementation(string name) 12 | { 13 | this.name = name; 14 | } 15 | 16 | #region ITypeInterface Members 17 | 18 | public string GetName() 19 | { 20 | return name; 21 | } 22 | 23 | #endregion 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/ClassWithConstMethodandProperty.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class ClassWithConstMethodandProperty 4 | { 5 | private T value; 6 | public ClassWithConstMethodandProperty() 7 | { } 8 | public ClassWithConstMethodandProperty(T value) 9 | { 10 | this.value = value; 11 | } 12 | 13 | public T Value 14 | { 15 | get { return this.value; } 16 | set { this.value = value; } 17 | } 18 | 19 | public void SetValue(T value) 20 | { 21 | this.value = value; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/Foo.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Generics 3 | { 4 | public class Foo : IFoo 5 | { 6 | public Foo() 7 | { 8 | } 9 | 10 | public Foo(TEntity value) 11 | { 12 | Value = value; 13 | } 14 | 15 | public TEntity Value { get; } 16 | } 17 | 18 | public class Foo : IFoo 19 | { 20 | } 21 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/FooRepository.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Generics 3 | { 4 | public class FooRepository : IRepository 5 | { 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenMockLogger.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenMockLogger : IGenLogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenSpecialLogger.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenSpecialLogger : IGenLogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericA.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenericA { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericB.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenericB { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericC.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenericC { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericConstraintsFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity.Exceptions; 3 | 4 | namespace Unity.Tests.v5.Generics 5 | { 6 | [TestClass] 7 | public class GenericConstraintsFixture 8 | { 9 | interface IFoo 10 | { 11 | } 12 | 13 | class Foo : IFoo where T : struct 14 | { 15 | } 16 | 17 | [TestMethod] 18 | public void ThrowsAppropriateExceptionWhenGenericArgumentFailsToMeetConstraintsOfMappedToType() 19 | { 20 | var ioc = new UnityContainer(); 21 | 22 | ioc.RegisterType(typeof(IFoo<>), typeof(Foo<>)); 23 | 24 | Assert.ThrowsException(() => ioc.Resolve>()); 25 | } 26 | 27 | [TestMethod] 28 | public void CanResolveOpenGenericInterfaceWithConstraintsInMappedToTypeWhenConstraintsAreMet() 29 | { 30 | var ioc = new UnityContainer(); 31 | 32 | ioc.RegisterType(typeof(IFoo<>), typeof(Foo<>)); 33 | 34 | Assert.IsNotNull(ioc.Resolve>()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericD.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenericD { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/GenericList.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class GenericList 4 | { 5 | private void Add(T input) { } 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/HaveAGenericType.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Generics 3 | { 4 | public class HaveAGenericType : IHaveAGenericType 5 | { 6 | public HaveAGenericType() 7 | { } 8 | 9 | public HaveAGenericType(T1 t1Value) 10 | { 11 | PropT1 = t1Value; 12 | } 13 | 14 | private T1 propT1; 15 | 16 | public T1 PropT1 17 | { 18 | get { return propT1; } 19 | set { propT1 = value; } 20 | } 21 | 22 | public void Set(T1 value) 23 | { 24 | PropT1 = value; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/HaveManyGenericTypes.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Generics 3 | { 4 | public class HaveManyGenericTypes : IHaveManyGenericTypes 5 | { 6 | public HaveManyGenericTypes() 7 | { } 8 | 9 | public HaveManyGenericTypes(T1 t1Value) 10 | { 11 | PropT1 = t1Value; 12 | } 13 | 14 | public HaveManyGenericTypes(T2 t2Value) 15 | { 16 | PropT2 = t2Value; 17 | } 18 | 19 | public HaveManyGenericTypes(T2 t2Value, T1 t1Value) 20 | { 21 | PropT2 = t2Value; 22 | PropT1 = t1Value; 23 | } 24 | 25 | private T1 propT1; 26 | 27 | public T1 PropT1 28 | { 29 | get { return propT1; } 30 | set { propT1 = value; } 31 | } 32 | 33 | private T2 propT2; 34 | 35 | public T2 PropT2 36 | { 37 | get { return propT2; } 38 | set { propT2 = value; } 39 | } 40 | 41 | private T3 propT3; 42 | 43 | public T3 PropT3 44 | { 45 | get { return propT3; } 46 | set { propT3 = value; } 47 | } 48 | 49 | private T4 propT4; 50 | 51 | public T4 PropT4 52 | { 53 | get { return propT4; } 54 | set { propT4 = value; } 55 | } 56 | 57 | public void Set(T1 t1Value) 58 | { 59 | PropT1 = t1Value; 60 | } 61 | 62 | public void Set(T2 t2Value) 63 | { 64 | PropT2 = t2Value; 65 | } 66 | 67 | public void Set(T3 t3Value) 68 | { 69 | PropT3 = t3Value; 70 | } 71 | 72 | public void Set(T4 t4Value) 73 | { 74 | PropT4 = t4Value; 75 | } 76 | 77 | public void SetMultiple(T4 t4Value, T3 t3Value) 78 | { 79 | PropT4 = t4Value; 80 | PropT3 = t3Value; 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/HaveManyGenericTypesClosed.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class HaveManyGenericTypesClosed : IHaveManyGenericTypesClosed 4 | { 5 | public HaveManyGenericTypesClosed() 6 | { } 7 | 8 | public HaveManyGenericTypesClosed(GenericA t1Value) 9 | { 10 | PropT1 = t1Value; 11 | } 12 | 13 | public HaveManyGenericTypesClosed(GenericB t2Value) 14 | { 15 | PropT2 = t2Value; 16 | } 17 | 18 | public HaveManyGenericTypesClosed(GenericB t2Value, GenericA t1Value) 19 | { 20 | PropT2 = t2Value; 21 | PropT1 = t1Value; 22 | } 23 | 24 | private GenericA propT1; 25 | 26 | public GenericA PropT1 27 | { 28 | get { return propT1; } 29 | set { propT1 = value; } 30 | } 31 | 32 | private GenericB propT2; 33 | 34 | public GenericB PropT2 35 | { 36 | get { return propT2; } 37 | set { propT2 = value; } 38 | } 39 | 40 | private GenericC propT3; 41 | 42 | public GenericC PropT3 43 | { 44 | get { return propT3; } 45 | set { propT3 = value; } 46 | } 47 | 48 | private GenericD propT4; 49 | 50 | public GenericD PropT4 51 | { 52 | get { return propT4; } 53 | set { propT4 = value; } 54 | } 55 | 56 | public void Set(GenericA t1Value) 57 | { 58 | PropT1 = t1Value; 59 | } 60 | 61 | public void Set(GenericB t2Value) 62 | { 63 | PropT2 = t2Value; 64 | } 65 | 66 | public void Set(GenericC t3Value) 67 | { 68 | PropT3 = t3Value; 69 | } 70 | 71 | public void Set(GenericD t4Value) 72 | { 73 | PropT4 = t4Value; 74 | } 75 | 76 | public void SetMultiple(GenericD t4Value, GenericC t3Value) 77 | { 78 | PropT4 = t4Value; 79 | PropT3 = t3Value; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IFoo.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IFoo 4 | { 5 | TEntity Value { get; } 6 | } 7 | 8 | public interface IFoo 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IGenLogger.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IGenLogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IHaveAGenericType.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IHaveAGenericType 4 | { 5 | T1 PropT1 { get; set; } 6 | 7 | void Set(T1 value); 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IHaveManyGenericTypes.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IHaveManyGenericTypes 4 | { 5 | T1 PropT1 { get; set; } 6 | T2 PropT2 { get; set; } 7 | T3 PropT3 { get; set; } 8 | T4 PropT4 { get; set; } 9 | 10 | void Set(T1 value); 11 | void Set(T2 value); 12 | void Set(T3 value); 13 | void Set(T4 value); 14 | 15 | void SetMultiple(T4 t4Value, T3 t3Value); 16 | } 17 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IHaveManyGenericTypesClosed.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IHaveManyGenericTypesClosed 4 | { 5 | GenericA PropT1 { get; set; } 6 | GenericB PropT2 { get; set; } 7 | GenericC PropT3 { get; set; } 8 | GenericD PropT4 { get; set; } 9 | 10 | void Set(GenericA value); 11 | void Set(GenericB value); 12 | void Set(GenericC value); 13 | void Set(GenericD value); 14 | 15 | void SetMultiple(GenericD t4Value, GenericC t3Value); 16 | } 17 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/IRepository.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public interface IRepository { } 4 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/MockRespository.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class MockRespository : IRepository 4 | { 5 | private Refer obj; 6 | 7 | [Dependency] 8 | public Refer Add 9 | { 10 | get { return obj; } 11 | set { obj = value; } 12 | } 13 | 14 | [InjectionConstructor] 15 | public MockRespository(Refer obj) 16 | { } 17 | } 18 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/Refer.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class Refer 4 | { 5 | private string str; 6 | 7 | public string Str 8 | { 9 | get { return str; } 10 | set { str = value; } 11 | } 12 | 13 | public Refer() 14 | { 15 | str = "Hello"; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Generics/Refer1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Generics 2 | { 3 | public class Refer1 4 | { 5 | public Refer1() 6 | { 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Injection/OptionalGenericParameterFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using Unity.Injection; 4 | using Unity.Tests.v5.Generics; 5 | 6 | namespace Unity.Tests.v5.Injection 7 | { 8 | /// 9 | /// Tests that use the GenericParameter class to ensure that 10 | /// generic object injection works. 11 | /// 12 | [TestClass] 13 | public class OptionalGenericParameterFixture 14 | { 15 | [TestMethod] 16 | public void CanCallConstructorTakingGenericParameterWithResolvableOptional() 17 | { 18 | IUnityContainer container = new UnityContainer() 19 | .RegisterType(typeof(ClassWithOneGenericParameter<>), 20 | new InjectionConstructor(new OptionalGenericParameter("T"))); 21 | 22 | Account a = new Account(); 23 | container.RegisterInstance(a); 24 | 25 | ClassWithOneGenericParameter result = container.Resolve>(); 26 | 27 | Assert.AreSame(a, result.InjectedValue); 28 | } 29 | 30 | [TestMethod] 31 | public void CanCallConstructorTakingGenericParameterWithNonResolvableOptional() 32 | { 33 | IUnityContainer container = new UnityContainer() 34 | .RegisterType(typeof(ClassWithOneGenericParameter<>), 35 | new InjectionConstructor(new OptionalGenericParameter("T"))); 36 | 37 | var result = container.Resolve>(); 38 | 39 | Assert.IsNull(result.InjectedValue); 40 | } 41 | 42 | [TestMethod] 43 | public void CanConfiguredNamedResolutionOfOptionalGenericParameter() 44 | { 45 | IUnityContainer container = new UnityContainer() 46 | .RegisterType(typeof(ClassWithOneGenericParameter<>), 47 | new InjectionConstructor(new OptionalGenericParameter("T", "named"))); 48 | 49 | Account a = new Account(); 50 | container.RegisterInstance(a); 51 | Account named = new Account(); 52 | container.RegisterInstance("named", named); 53 | 54 | ClassWithOneGenericParameter result = container.Resolve>(); 55 | Assert.AreSame(named, result.InjectedValue); 56 | } 57 | 58 | // Our various test objects 59 | public class ClassWithOneGenericParameter 60 | { 61 | public T InjectedValue; 62 | 63 | public ClassWithOneGenericParameter(string s, object o) 64 | { 65 | } 66 | 67 | public ClassWithOneGenericParameter(T injectedValue) 68 | { 69 | InjectedValue = injectedValue; 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Issues/CodeGenBugFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | 4 | namespace Unity.Tests.v5.Issues 5 | { 6 | /// 7 | /// Test for dynamic method creation and the CLR bug. This test will only 8 | /// fail if run in a release build! 9 | /// 10 | [TestClass] 11 | public class CodeGenBugFixture 12 | { 13 | [TestMethod] 14 | public void ResolvedTypeHasStaticConstructorCalled() 15 | { 16 | IUnityContainer container = new UnityContainer(); 17 | 18 | CodeGenBug result = container.Resolve(); 19 | } 20 | } 21 | 22 | public class CodeGenBug 23 | { 24 | public static readonly object TheStaticObject; 25 | 26 | static CodeGenBug() 27 | { 28 | TheStaticObject = new object(); 29 | } 30 | 31 | [InjectionConstructor] 32 | public CodeGenBug() 33 | : this(-12, TheStaticObject) 34 | { 35 | } 36 | 37 | public CodeGenBug(int i, object parameter) 38 | { 39 | if (parameter == null) 40 | { 41 | throw new ArgumentNullException("Static constructor was not called"); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/A.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public class A 4 | { } 5 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/AA.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Lifetime 3 | { 4 | public class AA : I1, I2 5 | { 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/ATTest.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Lifetime 3 | { 4 | public class ATTest : ITTest 5 | { 6 | public string Strtest = "Hello"; 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/B.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public class B 4 | { } 5 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/BB.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Lifetime 3 | { 4 | public class BB : I1, I2 5 | { 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/HierarchicalLifetimeFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | using Unity.Lifetime; 4 | 5 | namespace Unity.Tests.v5.Lifetime 6 | { 7 | [TestClass] 8 | public class WhenUsingHierarchicalLifetimeWithChildContainers 9 | { 10 | private IUnityContainer child1; 11 | private IUnityContainer child2; 12 | private IUnityContainer parentContainer; 13 | 14 | [TestInitialize] 15 | public void Setup() 16 | { 17 | parentContainer = new UnityContainer(); 18 | child1 = parentContainer.CreateChildContainer(); 19 | child2 = parentContainer.CreateChildContainer(); 20 | parentContainer.RegisterType(new HierarchicalLifetimeManager()); 21 | } 22 | 23 | [TestMethod] 24 | public void ThenResolvingInParentActsLikeContainerControlledLifetime() 25 | { 26 | var o1 = parentContainer.Resolve(); 27 | var o2 = parentContainer.Resolve(); 28 | Assert.AreSame(o1, o2); 29 | } 30 | 31 | [TestMethod] 32 | public void ThenParentAndChildResolveDifferentInstances() 33 | { 34 | var o1 = parentContainer.Resolve(); 35 | var o2 = child1.Resolve(); 36 | Assert.AreNotSame(o1, o2); 37 | } 38 | 39 | [TestMethod] 40 | public void ThenChildResolvesTheSameInstance() 41 | { 42 | var o1 = child1.Resolve(); 43 | var o2 = child1.Resolve(); 44 | Assert.AreSame(o1, o2); 45 | } 46 | 47 | [TestMethod] 48 | public void ThenSiblingContainersResolveDifferentInstances() 49 | { 50 | var o1 = child1.Resolve(); 51 | var o2 = child2.Resolve(); 52 | Assert.AreNotSame(o1, o2); 53 | } 54 | 55 | [TestMethod] 56 | public void ThenDisposingOfChildContainerDisposesOnlyChildObject() 57 | { 58 | var o1 = parentContainer.Resolve(); 59 | var o2 = child1.Resolve(); 60 | 61 | child1.Dispose(); 62 | Assert.IsFalse(o1.Disposed); 63 | Assert.IsTrue(o2.Disposed); 64 | } 65 | 66 | public class TestClass : IDisposable 67 | { 68 | public bool Disposed { get; private set; } 69 | 70 | public void Dispose() 71 | { 72 | Disposed = true; 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/I1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public interface I1 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/I2.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public interface I2 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/ITTest.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public interface ITTest 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Lifetime/UnityTestClass.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Lifetime 2 | { 3 | public class UnityTestClass 4 | { 5 | private string name = "Hello"; 6 | 7 | public string Name 8 | { 9 | get { return name; } 10 | set { name = value; } 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/ObjectBuilder/BuildPlanAndChildContainerFixture.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Practices.Unity.Tests.TestObjects; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using Unity.Injection; 4 | using Unity.Tests.v5.TestObjects; 5 | using Unity.Tests.v5.TestSupport; 6 | 7 | namespace Unity.Tests.v5.ObjectBuilder 8 | { 9 | /// 10 | /// Summary description for BuildPlanAndChildContainerFixture 11 | /// 12 | [TestClass] 13 | public class BuildPlanAndChildContainerFixture 14 | { 15 | private IUnityContainer parentContainer; 16 | private IUnityContainer childContainer; 17 | 18 | private const int ValueInjectedFromParent = 3; 19 | private const int ValueInjectedFromChild = 5; 20 | 21 | [TestInitialize] 22 | public void Setup() 23 | { 24 | parentContainer = new UnityContainer() 25 | .RegisterType(new InjectionConstructor(ValueInjectedFromParent)) 26 | .RegisterType(); 27 | 28 | childContainer = parentContainer.CreateChildContainer() 29 | .RegisterType(new InjectionConstructor(ValueInjectedFromChild)); 30 | } 31 | 32 | public class TestObject 33 | { 34 | public int Value { get; private set; } 35 | 36 | public TestObject(int value) 37 | { 38 | Value = value; 39 | } 40 | } 41 | 42 | [TestMethod] 43 | public void ValuesInjectedAreCorrectWhenResolvingFromParentFirst() 44 | { 45 | // Be aware ordering is important here - resolve through parent first 46 | // to elicit the buggy behavior 47 | var fromParent = parentContainer.Resolve(); 48 | var fromChild = childContainer.Resolve(); 49 | 50 | Assert.AreEqual(ValueInjectedFromParent, fromParent.Value); 51 | Assert.AreEqual(ValueInjectedFromChild, fromChild.Value); 52 | } 53 | 54 | [TestMethod] 55 | public void ChildContainersForUnconfiguredTypesPutConstructorParamResolversInParent() 56 | { 57 | childContainer.Resolve(); 58 | parentContainer.CreateChildContainer().Resolve(); 59 | 60 | // No exception means we're good. 61 | } 62 | 63 | [TestMethod] 64 | public void ChildContainersForUnconfiguredTypesPutPropertyResolversInParent() 65 | { 66 | childContainer.Resolve(); 67 | parentContainer.CreateChildContainer().Resolve(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /tests/Unity.Tests/ObjectBuilder/Utility/ActivatorCreationStrategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Builder; 3 | using Unity.Strategies; 4 | 5 | namespace Unity.Tests.v5.ObjectBuilder.Utility 6 | { 7 | internal class ActivatorCreationStrategy : BuilderStrategy 8 | { 9 | /// 10 | /// Called during the chain of responsibility for a build operation. The 11 | /// PreBuildUp method is called when the chain is being executed in the 12 | /// forward direction. 13 | /// 14 | /// Context of the build operation. 15 | public override void PreBuildUp(ref BuilderContext context) 16 | { 17 | if (context.Existing == null) 18 | { 19 | context.Existing = Activator.CreateInstance(context.Type); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/IForToUndergoeInject.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace Unity.Tests.v5.Override 3 | { 4 | public interface IForToUndergoeInject 5 | { 6 | IForTypeToInject IForTypeToInject { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/IForTypeToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public interface IForTypeToInject 4 | { 5 | int Value { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/IInterfaceForTypesToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public interface IInterfaceForTypesToInject 4 | { 5 | int Value { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/IInterfaceForTypesToInjectForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public interface IInterfaceForTypesToInjectForPropertyOverride 4 | { 5 | int Value { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/ISubjectTypeToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public interface ISubjectTypeToInject 4 | { 5 | int X { get; set; } 6 | string Y { get; set; } 7 | IInterfaceForTypesToInject InjectedObject { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/ISubjectTypeToInjectForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public interface ISubjectTypeToInjectForPropertyOverride 4 | { 5 | int X { get; set; } 6 | string Y { get; set; } 7 | [Dependency] 8 | IInterfaceForTypesToInjectForPropertyOverride InjectedObject { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/MySimpleType.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class MySimpleType 4 | { 5 | [Dependency] 6 | public int X { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/MySimpleTypeForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class MySimpleTypeForPropertyOverride 4 | { 5 | [Dependency] 6 | public int X { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType1ToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType1ToInject : ISubjectTypeToInject 4 | { 5 | [InjectionConstructor] 6 | public SubjectType1ToInject(IInterfaceForTypesToInject injectedObject) 7 | { 8 | InjectedObject = injectedObject; 9 | } 10 | 11 | public SubjectType1ToInject(int x, string y) 12 | { 13 | X = x; 14 | Y = y; 15 | } 16 | 17 | public int X { get; set; } 18 | public string Y { get; set; } 19 | public IInterfaceForTypesToInject InjectedObject { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType1ToInjectForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType1ToInjectForPropertyOverride : ISubjectTypeToInjectForPropertyOverride 4 | { 5 | public int X { get; set; } 6 | public string Y { get; set; } 7 | 8 | [Dependency] 9 | public IInterfaceForTypesToInjectForPropertyOverride InjectedObject { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType2ToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType2ToInject : ISubjectTypeToInject 4 | { 5 | [InjectionConstructor] 6 | public SubjectType2ToInject(IInterfaceForTypesToInject injectedObject) 7 | { 8 | InjectedObject = injectedObject; 9 | } 10 | 11 | public SubjectType2ToInject(int x, string y) 12 | { 13 | X = x; 14 | Y = y; 15 | } 16 | 17 | public int X { get; set; } 18 | public string Y { get; set; } 19 | public IInterfaceForTypesToInject InjectedObject { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType2ToInjectForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType2ToInjectForPropertyOverride : ISubjectTypeToInjectForPropertyOverride 4 | { 5 | public int X { get; set; } 6 | public string Y { get; set; } 7 | public IInterfaceForTypesToInjectForPropertyOverride InjectedObject { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType3ToInject.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType3ToInject : ISubjectTypeToInject 4 | { 5 | public SubjectType3ToInject(int x, string y) 6 | { 7 | X = x; 8 | Y = y; 9 | } 10 | 11 | public int X { get; set; } 12 | public string Y { get; set; } 13 | public IInterfaceForTypesToInject InjectedObject { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/SubjectType3ToInjectForPropertyOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class SubjectType3ToInjectForPropertyOverride : ISubjectTypeToInjectForPropertyOverride 4 | { 5 | public int X { get; set; } 6 | public string Y { get; set; } 7 | public IInterfaceForTypesToInjectForPropertyOverride InjectedObject { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TestTypeInConfig.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TestTypeInConfig 4 | { 5 | public TestTypeInConfig(int value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public TestTypeInConfig() 11 | { 12 | Value = 1; 13 | } 14 | 15 | public int Value { get; set; } 16 | public int X { get; set; } 17 | public string Y { get; set; } 18 | } 19 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject1 : IInterfaceForTypesToInject 4 | { 5 | public TypeToInject1(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject1ForTypeOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject1ForTypeOverride : IForTypeToInject 4 | { 5 | public TypeToInject1ForTypeOverride(int value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public int Value { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject2.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject2 : IInterfaceForTypesToInject 4 | { 5 | public TypeToInject2(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject2ForTypeOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject2ForTypeOverride : IForTypeToInject 4 | { 5 | public TypeToInject2ForTypeOverride(int value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public int Value { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject3.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject3 : IInterfaceForTypesToInject 4 | { 5 | public TypeToInject3(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInject3ForTypeOverride.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInject3ForTypeOverride : IForTypeToInject 4 | { 5 | public TypeToInject3ForTypeOverride(int value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public int Value { get; set; } 11 | public string PropertyToInject { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInjectForPropertyOverride1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInjectForPropertyOverride1 : IInterfaceForTypesToInjectForPropertyOverride 4 | { 5 | public TypeToInjectForPropertyOverride1(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInjectForPropertyOverride2.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInjectForPropertyOverride2 : IInterfaceForTypesToInjectForPropertyOverride 4 | { 5 | public TypeToInjectForPropertyOverride2(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToInjectForPropertyOverride3.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToInjectForPropertyOverride3 : IInterfaceForTypesToInjectForPropertyOverride 4 | { 5 | public TypeToInjectForPropertyOverride3(int value) 6 | { 7 | Value = value; 8 | } 9 | public int Value { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToToUndergoeTypeBasedInject2.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToToUndergoeTypeBasedInject2 : IForToUndergoeInject 4 | { 5 | public TypeToToUndergoeTypeBasedInject2(TypeToInject2ForTypeOverride injectedObject) 6 | { 7 | IForTypeToInject = injectedObject; 8 | } 9 | 10 | public IForTypeToInject IForTypeToInject { get; set; } 11 | public TypeToInject2ForTypeOverride TypeToInject2ForTypeOverride { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToToUndergoeTypeBasedInject3.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToToUndergoeTypeBasedInject3 : IForToUndergoeInject 4 | { 5 | public IForTypeToInject IForTypeToInject { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Override/TypeToUndergoeTypeBasedInject1.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.Override 2 | { 3 | public class TypeToUndergoeTypeBasedInject1 : IForToUndergoeInject 4 | { 5 | public TypeToUndergoeTypeBasedInject1(IForTypeToInject injectedObject) 6 | { 7 | IForTypeToInject = injectedObject; 8 | } 9 | 10 | public IForTypeToInject IForTypeToInject { get; set; } 11 | public TypeToInject1ForTypeOverride TypeToInject1ForTypeOverride { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/Storage/RegistrationSetTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | using Unity.Registration; 6 | using Unity.Storage; 7 | using Unity.Tests.TestObjects; 8 | 9 | namespace Unity.Tests.v5.Storage 10 | { 11 | [TestClass] 12 | public class RegistrationSetTests 13 | { 14 | [TestMethod] 15 | public void ShouldHandleCollisions() 16 | { 17 | Tuple s = MakeCollision(); 18 | 19 | var registrationSet = new RegistrationSet(); 20 | var registration1 = new InternalRegistration(); 21 | var registration2 = new InternalRegistration(); 22 | var registration3 = new InternalRegistration(); 23 | 24 | registrationSet.Add(typeof(IService), s.Item1, registration1); 25 | Assert.AreEqual(1, registrationSet.Count); 26 | registrationSet.Add(typeof(IService), s.Item2, registration2); 27 | Assert.AreEqual(2, registrationSet.Count); 28 | registrationSet.Add(typeof(IService), s.Item1, registration3); 29 | Assert.AreEqual(2, registrationSet.Count); 30 | } 31 | 32 | private static Tuple MakeCollision() 33 | { 34 | var strings = new Dictionary(); 35 | var random = new Random(); 36 | var size = 10; 37 | 38 | var builder = new StringBuilder(size); 39 | while (true) 40 | { 41 | for (var j = 0; j < size; j++) 42 | builder.Append((char) random.Next('a', 'z' + 1)); 43 | 44 | var str = builder.ToString(); 45 | var hash = str.GetHashCode(); 46 | if (strings.TryGetValue(hash, out var other)) 47 | return new Tuple (str, other); 48 | 49 | strings[hash] = str; 50 | builder.Clear(); 51 | } 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/DependencyAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter)] 6 | public class DependencyAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/InjectionConstructorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | [AttributeUsage(AttributeTargets.Constructor)] 6 | public class InjectionConstructorAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/InjectionMethodAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | [AttributeUsage(AttributeTargets.Method)] 6 | public class InjectionMethodAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/MockContainerExtension.cs: -------------------------------------------------------------------------------- 1 | using Unity.Extension; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | internal class MockContainerExtension : UnityContainerExtension, IMockConfiguration 6 | { 7 | private bool initializeWasCalled = false; 8 | 9 | public bool InitializeWasCalled 10 | { 11 | get { return this.initializeWasCalled; } 12 | } 13 | 14 | public new ExtensionContext Context 15 | { 16 | get { return base.Context; } 17 | } 18 | 19 | protected override void Initialize() 20 | { 21 | this.initializeWasCalled = true; 22 | } 23 | } 24 | 25 | internal interface IMockConfiguration : IUnityContainerExtensionConfigurator 26 | { 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/MockContainerExtensionWithNonDefaultConstructor.cs: -------------------------------------------------------------------------------- 1 | using Unity.Extension; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | public class ContainerExtensionWithNonDefaultConstructor : UnityContainerExtension 6 | { 7 | public ContainerExtensionWithNonDefaultConstructor(IUnityContainer container) 8 | { 9 | } 10 | 11 | protected override void Initialize() 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/SpyExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity.Builder; 3 | using Unity.Extension; 4 | using Unity.Strategies; 5 | 6 | namespace Unity.Tests.v5.TestDoubles 7 | { 8 | /// 9 | /// A simple extension that puts the supplied strategy into the 10 | /// chain at the indicated stage. 11 | /// 12 | internal class SpyExtension : UnityContainerExtension 13 | { 14 | private BuilderStrategy strategy; 15 | private UnityBuildStage stage; 16 | private object policy; 17 | private Type policyType; 18 | 19 | public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage) 20 | { 21 | this.strategy = strategy; 22 | this.stage = stage; 23 | } 24 | 25 | public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage, object policy, Type policyType) 26 | { 27 | this.strategy = strategy; 28 | this.stage = stage; 29 | this.policy = policy; 30 | this.policyType = policyType; 31 | } 32 | 33 | protected override void Initialize() 34 | { 35 | Context.Strategies.Add(this.strategy, this.stage); 36 | 37 | if (this.policy != null) 38 | { 39 | Context.Policies.Set(null, null, this.policyType, this.policy); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/SpyPolicy.cs: -------------------------------------------------------------------------------- 1 | using Unity.Policy; 2 | 3 | namespace Unity.Tests.v5.TestDoubles 4 | { 5 | /// 6 | /// A sample policy that gets used by the SpyStrategy 7 | /// if present to mark execution. 8 | /// 9 | internal class SpyPolicy 10 | { 11 | private bool wasSpiedOn; 12 | 13 | public bool WasSpiedOn 14 | { 15 | get { return wasSpiedOn; } 16 | set { wasSpiedOn = value; } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestDoubles/SpyStrategy.cs: -------------------------------------------------------------------------------- 1 | using Unity.Builder; 2 | using Unity.Strategies; 3 | 4 | namespace Unity.Tests.v5.TestDoubles 5 | { 6 | /// 7 | /// A small snoop strategy that lets us check afterwards to 8 | /// see if it ran in the strategy chain. 9 | /// 10 | internal class SpyStrategy : BuilderStrategy 11 | { 12 | private object existing = null; 13 | private bool buildUpWasCalled = false; 14 | 15 | public override void PreBuildUp(ref BuilderContext context) 16 | { 17 | this.buildUpWasCalled = true; 18 | this.existing = context.Existing; 19 | 20 | this.UpdateSpyPolicy(ref context); 21 | } 22 | 23 | public override void PostBuildUp(ref BuilderContext context) 24 | { 25 | this.existing = context.Existing; 26 | } 27 | 28 | public object Existing 29 | { 30 | get { return this.existing; } 31 | } 32 | 33 | public bool BuildUpWasCalled 34 | { 35 | get { return this.buildUpWasCalled; } 36 | } 37 | 38 | private void UpdateSpyPolicy(ref BuilderContext context) 39 | { 40 | SpyPolicy policy = (SpyPolicy)context.Get(null, null, typeof(SpyPolicy)); 41 | 42 | if (policy != null) 43 | { 44 | policy.WasSpiedOn = true; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/DisposableObject.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | 5 | namespace Microsoft.Practices.Unity.Tests.TestObjects 6 | { 7 | public class DisposableObject : IDisposable 8 | { 9 | private bool wasDisposed = false; 10 | 11 | public bool WasDisposed 12 | { 13 | get { return wasDisposed; } 14 | set { wasDisposed = value; } 15 | } 16 | 17 | public void Dispose() 18 | { 19 | wasDisposed = true; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/EmailService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.TestObjects 4 | { 5 | // A dummy class to support testing type mapping 6 | public class EmailService : IService, IDisposable 7 | { 8 | public string Id { get; } = Guid.NewGuid().ToString(); 9 | 10 | public bool Disposed = false; 11 | public void Dispose() 12 | { 13 | Disposed = true; 14 | } 15 | } 16 | 17 | // A dummy class to support testing type mapping 18 | public class OtherEmailService : IService, IOtherService, IDisposable 19 | { 20 | public string Id = Guid.NewGuid().ToString(); 21 | 22 | [InjectionConstructor] 23 | public OtherEmailService() 24 | { 25 | 26 | } 27 | 28 | public OtherEmailService(IUnityContainer container) 29 | { 30 | 31 | } 32 | 33 | public bool Disposed = false; 34 | public void Dispose() 35 | { 36 | Disposed = true; 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/FileLogger.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Microsoft.Practices.ObjectBuilder2.Tests.TestObjects 4 | { 5 | public class FileLogger 6 | { 7 | private string logFile; 8 | 9 | public FileLogger(string logFile) 10 | { 11 | this.logFile = logFile; 12 | } 13 | 14 | public string LogFile 15 | { 16 | get { return logFile; } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/IBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.TestObjects 4 | { 5 | public interface IBase 6 | { 7 | IService Service { get; set; } 8 | } 9 | 10 | public interface ILazyDependency 11 | { 12 | Lazy Service { get; set; } 13 | } 14 | 15 | public class Base : IBase 16 | { 17 | [Dependency] 18 | public IService Service { get; set; } 19 | } 20 | 21 | public class LazyDependency : ILazyDependency 22 | { 23 | [Dependency] 24 | public Lazy Service { get; set; } 25 | } 26 | 27 | public class LazyDependencyConstructor 28 | { 29 | private Lazy service = null; 30 | 31 | public LazyDependencyConstructor(Lazy s) 32 | { 33 | service = s; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/IService.cs: -------------------------------------------------------------------------------- 1 | 2 | 3 | namespace Unity.Tests.TestObjects 4 | { 5 | // A dummy interface to support testing type mapping 6 | public interface IService 7 | { 8 | } 9 | 10 | public interface IOtherService 11 | { 12 | } 13 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/NullLogger.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Microsoft.Practices.ObjectBuilder2.Tests.TestObjects 4 | { 5 | /// 6 | /// A simple class with only default constructor. A test 7 | /// target for the dynamic method build plan. 8 | /// 9 | public class NullLogger 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithAmbiguousConstructors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Unity.Tests.TestObjects 4 | { 5 | public class ObjectWithAmbiguousConstructors 6 | { 7 | public const string One = "1"; 8 | public const string Two = "2"; 9 | public const string Three = "3"; 10 | public const string Four = "4"; 11 | public const string Five = "5"; 12 | 13 | public string Signature { get; } 14 | 15 | public ObjectWithAmbiguousConstructors() 16 | { 17 | Signature = One; 18 | } 19 | 20 | public ObjectWithAmbiguousConstructors(int first, string second, float third) 21 | { 22 | Signature = Two; 23 | } 24 | 25 | public ObjectWithAmbiguousConstructors(Type first, Type second, Type third) 26 | { 27 | Signature = Three; 28 | } 29 | 30 | public ObjectWithAmbiguousConstructors(string first, string second, string third) 31 | { 32 | Signature = first; 33 | } 34 | 35 | public ObjectWithAmbiguousConstructors(string first, [Dependency(Five)]string second, IUnityContainer third) 36 | { 37 | Signature = second; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithAmbiguousMarkedConstructor.cs: -------------------------------------------------------------------------------- 1 |  2 | using Unity.Tests.v5.TestDoubles; 3 | 4 | namespace Unity.Tests.v5.TestObjects 5 | { 6 | internal class ObjectWithAmbiguousMarkedConstructor 7 | { 8 | public ObjectWithAmbiguousMarkedConstructor() 9 | { 10 | } 11 | 12 | public ObjectWithAmbiguousMarkedConstructor(int first, string second, float third) 13 | { 14 | } 15 | 16 | [InjectionConstructor] 17 | public ObjectWithAmbiguousMarkedConstructor(string first, string second, int third) 18 | { 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithExplicitInterface.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity.Tests.v5.TestSupport; 3 | 4 | namespace Unity.Tests.v5.TestObjects 5 | { 6 | public interface ISomeCommonProperties 7 | { 8 | [Dependency] 9 | ILogger Logger { get; set; } 10 | 11 | [Dependency] 12 | object SyncObject { get; set; } 13 | } 14 | 15 | public class ObjectWithExplicitInterface : ISomeCommonProperties 16 | { 17 | private ILogger logger; 18 | private object syncObject; 19 | 20 | private object somethingElse; 21 | 22 | [Dependency] 23 | public object SomethingElse 24 | { 25 | get { return somethingElse; } 26 | set { somethingElse = value; } 27 | } 28 | 29 | [Dependency] 30 | ILogger ISomeCommonProperties.Logger 31 | { 32 | get { return logger; } 33 | set { logger = value; } 34 | } 35 | 36 | [Dependency] 37 | object ISomeCommonProperties.SyncObject 38 | { 39 | get { return syncObject; } 40 | set { syncObject = value; } 41 | } 42 | 43 | public void ValidateInterface() 44 | { 45 | Assert.IsNotNull(logger); 46 | Assert.IsNotNull(syncObject); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithInjectionConstructor.cs: -------------------------------------------------------------------------------- 1 | using Unity; 2 | 3 | namespace Microsoft.Practices.Unity.Tests.TestObjects 4 | { 5 | public class ObjectWithInjectionConstructor 6 | { 7 | private object constructorDependency; 8 | 9 | public ObjectWithInjectionConstructor(object constructorDependency) 10 | { 11 | this.constructorDependency = constructorDependency; 12 | } 13 | 14 | [InjectionConstructor] 15 | public ObjectWithInjectionConstructor(string s) 16 | { 17 | constructorDependency = s; 18 | } 19 | 20 | public object ConstructorDependency 21 | { 22 | get { return constructorDependency; } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithLotsOfDependencies.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Practices.Unity.Tests.TestObjects; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using Unity.Tests.v5.TestSupport; 4 | 5 | namespace Unity.Tests.v5.TestObjects 6 | { 7 | // An object that has constructor, property, and method injection dependencies. 8 | public class ObjectWithLotsOfDependencies 9 | { 10 | private ILogger ctorLogger; 11 | private ObjectWithOneDependency dep1; 12 | private ObjectWithTwoConstructorDependencies dep2; 13 | private ObjectWithTwoProperties dep3; 14 | 15 | public ObjectWithLotsOfDependencies(ILogger logger, ObjectWithOneDependency dep1) 16 | { 17 | this.ctorLogger = logger; 18 | this.dep1 = dep1; 19 | } 20 | 21 | [Dependency] 22 | public ObjectWithTwoConstructorDependencies Dep2 23 | { 24 | get { return dep2; } 25 | set { dep2 = value; } 26 | } 27 | 28 | [InjectionMethod] 29 | public void InjectMe(ObjectWithTwoProperties dep3) 30 | { 31 | this.dep3 = dep3; 32 | } 33 | 34 | public void Validate() 35 | { 36 | Assert.IsNotNull(ctorLogger); 37 | Assert.IsNotNull(dep1); 38 | Assert.IsNotNull(dep2); 39 | Assert.IsNotNull(dep3); 40 | 41 | dep1.Validate(); 42 | dep2.Validate(); 43 | dep3.Validate(); 44 | } 45 | 46 | public ILogger CtorLogger 47 | { 48 | get { return ctorLogger; } 49 | } 50 | 51 | public ObjectWithOneDependency Dep1 52 | { 53 | get { return dep1; } 54 | } 55 | 56 | public ObjectWithTwoProperties Dep3 57 | { 58 | get { return dep3; } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithMarkedConstructor.cs: -------------------------------------------------------------------------------- 1 |  2 | using Unity.Tests.v5.TestDoubles; 3 | 4 | namespace Unity.Tests.v5.TestObjects 5 | { 6 | internal class ObjectWithMarkedConstructor 7 | { 8 | public ObjectWithMarkedConstructor(int notTheInjectionConstructor) 9 | { 10 | } 11 | 12 | [InjectionConstructor] 13 | public ObjectWithMarkedConstructor(string theInjectionConstructor) 14 | { 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithMultipleConstructors.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Unity.Tests.TestObjects 4 | { 5 | public class ObjectWithMultipleConstructors 6 | { 7 | public ObjectWithMultipleConstructors() 8 | { 9 | } 10 | 11 | public ObjectWithMultipleConstructors(int first, string second) 12 | { 13 | } 14 | 15 | public ObjectWithMultipleConstructors(int first) 16 | { 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithOneDependency.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace Microsoft.Practices.Unity.Tests.TestObjects 6 | { 7 | public class ObjectWithOneDependency 8 | { 9 | private object inner; 10 | 11 | public ObjectWithOneDependency(object inner) 12 | { 13 | this.inner = inner; 14 | } 15 | 16 | public object InnerObject 17 | { 18 | get { return inner; } 19 | } 20 | 21 | public void Validate() 22 | { 23 | Assert.IsNotNull(inner); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithStaticAndInstanceProperties.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using Unity; 3 | 4 | namespace Microsoft.Practices.Unity.Tests.TestObjects 5 | { 6 | public class ObjectWithStaticAndInstanceProperties 7 | { 8 | [Dependency] 9 | public static object StaticProperty { get; set; } 10 | 11 | [Dependency] 12 | public object InstanceProperty { get; set; } 13 | 14 | public void Validate() 15 | { 16 | Assert.IsNull(StaticProperty); 17 | Assert.IsNotNull(this.InstanceProperty); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/ObjectWithTwoConstructorDependencies.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace Microsoft.Practices.Unity.Tests.TestObjects 6 | { 7 | // A class that contains another one which has another 8 | // constructor dependency. Used to validate recursive 9 | // buildup of constructor dependencies. 10 | public class ObjectWithTwoConstructorDependencies 11 | { 12 | private ObjectWithOneDependency oneDep; 13 | 14 | public ObjectWithTwoConstructorDependencies(ObjectWithOneDependency oneDep) 15 | { 16 | this.oneDep = oneDep; 17 | } 18 | 19 | public ObjectWithOneDependency OneDep 20 | { 21 | get { return oneDep; } 22 | } 23 | 24 | public void Validate() 25 | { 26 | Assert.IsNotNull(oneDep); 27 | oneDep.Validate(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestObjects/OptionalLogger.cs: -------------------------------------------------------------------------------- 1 |  2 | using Unity.Tests.v5.TestDoubles; 3 | 4 | namespace Unity.Tests.v5.TestObjects 5 | { 6 | internal class OptionalLogger 7 | { 8 | private string logFile; 9 | 10 | public OptionalLogger([Dependency] string logFile) 11 | { 12 | this.logFile = logFile; 13 | } 14 | 15 | public string LogFile 16 | { 17 | get { return logFile; } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/AssertExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace Unity.Tests.v5.TestSupport 6 | { 7 | public static class AssertExtensions 8 | { 9 | public static void AssertException(Action action) 10 | where TException : Exception 11 | { 12 | AssertException(action, (e) => { }); 13 | } 14 | 15 | public static void AssertException(Action action, Action callback) 16 | where TException : Exception 17 | { 18 | try 19 | { 20 | action(); 21 | Assert.Fail("Expected exception of type {0}", typeof(TException).GetTypeInfo().Name); 22 | } 23 | catch (TException e) 24 | { 25 | callback(e); 26 | } 27 | } 28 | 29 | public static void IsInstanceOfType(object value, Type expectedType) 30 | { 31 | Assert.IsNotNull(value, "value should not be null"); 32 | Assert.IsNotNull(value, "expectedType should not be null"); 33 | Assert.IsTrue(expectedType.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo())); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/EnumerableAssertionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | 6 | namespace Unity.Tests.v5.TestSupport 7 | { 8 | public static class EnumerableAssertionExtensions 9 | { 10 | public static void AssertContainsExactly(this IEnumerable items, params TItem[] expected) 11 | { 12 | CollectionAssertExtensions.AreEqual(expected, items.ToArray()); 13 | } 14 | 15 | public static void AssertContainsInAnyOrder(this IEnumerable items, params TItem[] expected) 16 | { 17 | CollectionAssertExtensions.AreEquivalent(expected, items.ToArray()); 18 | } 19 | 20 | public static void AssertTrueForAll(this IEnumerable items, Func predicate) 21 | { 22 | Assert.IsTrue(items.All(predicate)); 23 | } 24 | 25 | public static void AssertTrueForAny(this IEnumerable items, Func predicate) 26 | { 27 | Assert.IsTrue(items.Any(predicate)); 28 | } 29 | 30 | public static void AssertFalseForAll(this IEnumerable items, Func predicate) 31 | { 32 | Assert.IsFalse(items.All(predicate)); 33 | } 34 | 35 | public static void AssertFalseForAny(this IEnumerable items, Func predicate) 36 | { 37 | Assert.IsFalse(items.Any(predicate)); 38 | } 39 | 40 | public static void AssertHasItems(this IEnumerable items) 41 | { 42 | Assert.IsTrue(items.Any()); 43 | } 44 | 45 | public static void AssertHasNoItems(this IEnumerable items) 46 | { 47 | Assert.IsFalse(items.Any()); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ExtensibilityTestExtension.cs: -------------------------------------------------------------------------------- 1 |  2 | using Unity.Extension; 3 | 4 | namespace Unity.Tests.v5.TestSupport 5 | { 6 | public interface IConfigOne : IUnityContainerExtensionConfigurator 7 | { 8 | IConfigOne SetText(string text); 9 | } 10 | 11 | public interface IConfigTwo : IUnityContainerExtensionConfigurator 12 | { 13 | IConfigTwo SetMessage(string text); 14 | } 15 | 16 | public class ExtensibilityTestExtension : UnityContainerExtension, IConfigOne, IConfigTwo 17 | { 18 | public string ConfigOneText { get; private set; } 19 | public string ConfigTwoText { get; private set; } 20 | 21 | protected override void Initialize() 22 | { 23 | } 24 | 25 | public IConfigOne SetText(string text) 26 | { 27 | this.ConfigOneText = text; 28 | return this; 29 | } 30 | 31 | public IConfigTwo SetMessage(string text) 32 | { 33 | this.ConfigTwoText = text; 34 | return this; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/IAdditionalInterface.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.TestSupport 2 | { 3 | public interface IAdditionalInterface 4 | { 5 | int DoNothing(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ILogger.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity.Tests.v5.TestSupport 3 | { 4 | public interface ILogger 5 | { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/MockContainerExtension.cs: -------------------------------------------------------------------------------- 1 |  2 | using Unity.Extension; 3 | 4 | namespace Unity.Tests.v5.TestSupport 5 | { 6 | public class MockContainerExtension : UnityContainerExtension, IMockConfiguration 7 | { 8 | private bool initializeWasCalled = false; 9 | 10 | public bool InitializeWasCalled 11 | { 12 | get { return this.initializeWasCalled; } 13 | } 14 | 15 | public new ExtensionContext Context 16 | { 17 | get { return base.Context; } 18 | } 19 | 20 | protected override void Initialize() 21 | { 22 | this.initializeWasCalled = true; 23 | } 24 | } 25 | 26 | public interface IMockConfiguration : IUnityContainerExtensionConfigurator 27 | { 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/MockDatabase.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity.Tests.v5.TestSupport 3 | { 4 | public class MockDatabase 5 | { 6 | private string connectionString; 7 | private bool defaultConstructorCalled; 8 | 9 | public MockDatabase() 10 | { 11 | defaultConstructorCalled = true; 12 | } 13 | 14 | public MockDatabase(string connectionString) 15 | { 16 | this.connectionString = connectionString; 17 | } 18 | 19 | public static MockDatabase Create(string connectionString) 20 | { 21 | return new MockDatabase(connectionString); 22 | } 23 | 24 | public string ConnectionString 25 | { 26 | get { return connectionString; } 27 | } 28 | 29 | public bool DefaultConstructorCalled 30 | { 31 | get { return defaultConstructorCalled; } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/MockLogger.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | namespace Unity.Tests.v5.TestSupport 4 | { 5 | public class MockLogger : ILogger 6 | { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ObjectUsingLogger.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.TestSupport 2 | { 3 | public class ObjectUsingLogger 4 | { 5 | private ILogger logger; 6 | 7 | [Dependency] 8 | public ILogger Logger 9 | { 10 | get { return logger; } 11 | set { logger = value; } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ObjectWithOneConstructorDependency.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.TestSupport 2 | { 3 | public class ObjectWithOneConstructorDependency 4 | { 5 | private ILogger logger; 6 | 7 | public ObjectWithOneConstructorDependency(ILogger logger) 8 | { 9 | this.logger = logger; 10 | } 11 | 12 | public ILogger Logger 13 | { 14 | get { return logger; } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ObjectWithTwoConstructorParameters.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.TestSupport 2 | { 3 | public class ObjectWithTwoConstructorParameters 4 | { 5 | private string connectionString; 6 | private ILogger logger; 7 | 8 | public ObjectWithTwoConstructorParameters(string connectionString, ILogger logger) 9 | { 10 | this.connectionString = connectionString; 11 | this.logger = logger; 12 | } 13 | 14 | public string ConnectionString 15 | { 16 | get { return connectionString; } 17 | } 18 | 19 | public ILogger Logger 20 | { 21 | get { return logger; } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/ObjectWithTwoProperties.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace Unity.Tests.v5.TestSupport 4 | { 5 | public class ObjectWithTwoProperties 6 | { 7 | private object obj1; 8 | private object obj2; 9 | 10 | [Dependency] 11 | public object Obj1 12 | { 13 | get { return obj1; } 14 | set { obj1 = value; } 15 | } 16 | 17 | [Dependency] 18 | public object Obj2 19 | { 20 | get { return obj2; } 21 | set { obj2 = value; } 22 | } 23 | 24 | public void Validate() 25 | { 26 | Assert.IsNotNull(obj1); 27 | Assert.IsNotNull(obj2); 28 | Assert.AreNotSame(obj1, obj2); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/Pair.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Unity.Tests.v5.TestSupport 3 | { 4 | /// 5 | /// A helper class that encapsulates two different 6 | /// data items together into a a single item. 7 | /// 8 | public class Pair 9 | { 10 | private TFirst first; 11 | private TSecond second; 12 | 13 | /// 14 | /// Create a new containing 15 | /// the two values give. 16 | /// 17 | /// First value 18 | /// Second value 19 | public Pair(TFirst first, TSecond second) 20 | { 21 | this.first = first; 22 | this.second = second; 23 | } 24 | 25 | /// 26 | /// The first value of the pair. 27 | /// 28 | public TFirst First 29 | { 30 | get { return first; } 31 | } 32 | 33 | /// 34 | /// The second value of the pair. 35 | /// 36 | public TSecond Second 37 | { 38 | get { return second; } 39 | } 40 | } 41 | 42 | /// 43 | /// Container for a Pair helper method. 44 | /// 45 | public static class Pair 46 | { 47 | /// 48 | /// A helper factory method that lets users take advantage of type inference. 49 | /// 50 | /// Type of first value. 51 | /// Type of second value. 52 | /// First value. 53 | /// Second value. 54 | /// A new instance. 55 | public static Pair Make(TFirstParameter first, TSecondParameter second) 56 | { 57 | return new Pair(first, second); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/Sequence.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Unity.Tests.v5.TestSupport 4 | { 5 | /// 6 | /// A series of helper methods to deal with sequences - 7 | /// objects that implement . 8 | /// 9 | public static class Sequence 10 | { 11 | /// 12 | /// A function that turns an arbitrary parameter list into an 13 | /// . 14 | /// 15 | /// Type of arguments. 16 | /// The items to put into the collection. 17 | /// An array that contains the values of the . 18 | public static T[] Collect(params T[] arguments) 19 | { 20 | return arguments; 21 | } 22 | 23 | /// 24 | /// Given two sequences, return a new sequence containing the corresponding values 25 | /// from each one. 26 | /// 27 | /// Type of first sequence. 28 | /// Type of second sequence. 29 | /// First sequence of items. 30 | /// Second sequence of items. 31 | /// New sequence of pairs. This sequence ends when the shorter of sequence1 and sequence2 does. 32 | public static IEnumerable> Zip(IEnumerable sequence1, IEnumerable sequence2) 33 | { 34 | var enum1 = sequence1.GetEnumerator(); 35 | var enum2 = sequence2.GetEnumerator(); 36 | 37 | while (enum1.MoveNext()) 38 | { 39 | if (enum2.MoveNext()) 40 | { 41 | yield return new Pair(enum1.Current, enum2.Current); 42 | } 43 | else 44 | { 45 | yield break; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/SpecialLogger.cs: -------------------------------------------------------------------------------- 1 | namespace Unity.Tests.v5.TestSupport 2 | { 3 | public class SpecialLogger : ILogger 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/Unity.Tests/TestSupport/TypeReflectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | 5 | namespace Unity.Tests.v5.TestSupport 6 | { 7 | public static class TypeReflectionExtensions 8 | { 9 | public static ConstructorInfo GetMatchingConstructor(this Type type, Type[] constructorParamTypes) 10 | { 11 | return type.GetTypeInfo().DeclaredConstructors 12 | .Where(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(constructorParamTypes)) 13 | .FirstOrDefault(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Unity.Tests/Unity.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net48 5 | false 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | --------------------------------------------------------------------------------