├── .gitignore ├── License.txt ├── README.md ├── how_to_build.txt ├── rakefile.rb └── src ├── .nuget ├── NuGet.Config ├── NuGet.exe └── NuGet.targets ├── UnityConfiguration.Tests ├── AddAllConventionTests.cs ├── ConstructorConfigurationTests.cs ├── CustomConventionTests.cs ├── Diagnostics │ └── DiagnosticsTests.cs ├── ExploratoryTests.cs ├── Extensions.cs ├── FilterTests.cs ├── FirstInterfaceConventionTests.cs ├── LifetimeManagementTests.cs ├── NamingConventionTests.cs ├── OpenGenericTests.cs ├── PostBuildUpActionTests.cs ├── Properties │ └── AssemblyInfo.cs ├── PropertyInjectionTests.cs ├── RegistryTests.cs ├── ScanForRegistriesConventionTests.cs ├── ScanningTests.cs ├── Services │ ├── AnotherMessage.cs │ ├── AnotherMessageHandler.cs │ ├── BarRegistry.cs │ ├── BarService.cs │ ├── FooDecorator.cs │ ├── FooRegistry.cs │ ├── FooService.cs │ ├── IBarService.cs │ ├── IFooDecorator.cs │ ├── IFooService.cs │ ├── IHandler.cs │ ├── IHaveManyImplementations.cs │ ├── IInternalService.cs │ ├── ILogger.cs │ ├── IMapper.cs │ ├── IServiceWithCtorArgs.cs │ ├── IStartable.cs │ ├── IStoppable.cs │ ├── Implementation1.cs │ ├── Implementation2.cs │ ├── InternalService.cs │ ├── Message.cs │ ├── MessageHandler.cs │ ├── MessageToAnotherMessageMapper.cs │ ├── NullLogger.cs │ ├── OtherNamespace │ │ ├── IServiceInOtherNamespace.cs │ │ └── ServiceInOtherNamespace.cs │ ├── ServiceWithCtorArgs.cs │ ├── StartableService1.cs │ ├── StartableService2.cs │ └── StoppableService.cs ├── TypeRegistrationTests.cs ├── UnityConfiguration.Tests.csproj ├── UnityExtensionsConfigurationTests.cs ├── UserInterface │ ├── IMyView.cs │ ├── MyView.Designer.cs │ └── MyView.cs ├── app.config └── packages.config ├── UnityConfiguration.sln └── UnityConfiguration ├── AddAllConvention.cs ├── AddNewExtensionExpression.cs ├── AssemblyScanner.cs ├── CompositeFilter.cs ├── CompositePredicate.cs ├── ConfigureExtensionExpression.cs ├── ConventionExtensions.cs ├── DecoratorExtension.cs ├── Diagnostics └── WhatDoIHaveExtensionMethod.cs ├── EnumerableExtensions.cs ├── Expression.cs ├── FactoryRegistrationExpression.cs ├── FirstInterfaceConvention.cs ├── IAssemblyScanner.cs ├── IAssemblyScannerConvention.cs ├── IHideObjectMembers.cs ├── ILifetimePolicyExpression.cs ├── IRegistrationExpression.cs ├── IUnityRegistry.cs ├── LifetimeExtensions.cs ├── NamingConvention.cs ├── PostBuildUpActionExtension.cs ├── PostBuildUpExpression.cs ├── Properties └── AssemblyInfo.cs ├── RegistrationExpression.cs ├── ScanForRegistriesConvention.cs ├── SetAllPropertiesConvention.cs ├── TypeExtensions.cs ├── UnityConfiguration.csproj ├── UnityConfiguration.snk ├── UnityExtension.cs ├── UnityRegistry.cs ├── app.config └── packages.config /.gitignore: -------------------------------------------------------------------------------- 1 | *.[Cc]ache 2 | *.csproj.user 3 | *.[Rr]e[Ss]harper* 4 | *.sln.cache 5 | *.suo 6 | *.user 7 | *.orig 8 | *.pidb 9 | *.userprefs 10 | .DS_Store 11 | deploy/ 12 | build/ 13 | [Bb]in/ 14 | [Dd]ebug/ 15 | [Oo]bj/ 16 | [Rr]elease/ 17 | _[Rr]e[Ss]harper.*/ 18 | TestResult.xml 19 | *.nuspec 20 | [Pp]ackages -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2011 Thomas Pedersen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityConfiguration v1.4.1 2 | 3 | Convention based configuration for the Microsoft Unity IoC container. With just a few lines of code, you can now registere all your classes in the entire solution. If the built-in conventions doesn't fit your needs, it is very easy to extend with your own. 4 | 5 | ## Download and install 6 | 7 | UnityConfiguration is available for download on [NuGet](http://nuget.org/packages/UnityConfiguration). 8 | 9 | To install it run the following command in the [NuGet Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console). 10 | 11 | PM> Install-Package UnityConfiguration 12 | 13 | This will download all the binaries, and add necessary references to your project. 14 | 15 | ## Configuring your container 16 | 17 | You get access to the configuration api by using the extension method Configure on the IUnityContainer interface. 18 | 19 | var container = new UnityContainer(); 20 | container.Configure(x => 21 | { 22 | x.AddRegistry(); 23 | x.AddRegistry(new BarRegistry()); 24 | }); 25 | 26 | ## Configure using registries 27 | 28 | Configuration is done in one or several registries that inherit the UnityRegistry base class. 29 | 30 | public class FooRegistry : UnityRegistry 31 | { 32 | public FooRegistry() 33 | { 34 | // Scan one or several assemblies and auto register types based on a 35 | // convention. You can also include or exclude certain types and/or 36 | // namespaces using a filter. 37 | Scan(scan => 38 | { 39 | scan.AssembliesInBaseDirectory(); 40 | scan.ForRegistries(); 41 | scan.With(); 42 | scan.With() 43 | .TypesImplementing(); 44 | scan.With().OfType(); 45 | scan.ExcludeType(); 46 | }); 47 | 48 | // Manually register a service 49 | Register().WithName("Foo").AsSingleton(); 50 | 51 | // Make services a singleton. Useful for types registered by the scanner. 52 | Configure().AsSingleton(); 53 | 54 | // If you want to inject values or objects that are not registered in 55 | // the container, you can do so by adding them using this statement. 56 | // For instances you want the container to create, just specify the type. 57 | Configure().WithConstructorArguments("some string", typeof (IFooService)); 58 | 59 | // Unity follows the greedy constructor pattern when creating instances. 60 | // If you want to use a different constructor, you specify it by listing 61 | // the types of the arguments in the constructor you want it to use. 62 | SelectConstructor(typeof (IFooService)); 63 | 64 | // You can automatically configure the container to call 65 | // a method on any service when they are created 66 | AfterBuildingUp().Call((c, s) => s.Start()); 67 | 68 | // You can automatically configure the container to 69 | // decorate services when they are created 70 | AfterBuildingUp().DecorateWith((c, t) => new FooDecorator(t)); 71 | } 72 | } 73 | 74 | ## Custom conventions 75 | 76 | At the moment, built in conventions includes AddAllConvention, FirstInterfaceConvention, NamingConvention, SetAllPropertiesConvention and ScanForRegistriesConvention. If these doesn't suit you, creating custom conventions is as easy as creating a class that implements the IAssemblyScanner interface. 77 | 78 | public class CustomConvention : IAssemblyScannerConvention 79 | { 80 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 81 | { 82 | if (type == typeof(FooService)) 83 | registry.Register().WithName("Custom"); 84 | } 85 | } 86 | 87 | ## Release Notes 88 | 89 | #### v1.4.1 90 | * Fixed #6: AssemblyScanner not bin folder friendly for web apps 91 | * Added #5: `IUnityContainer.Configure()` returns the `IUnityContainer` so other calls can be chained 92 | 93 | #### v1.4 94 | * Added extension methods to the `IAssemblyScanner` for easier discovery and configuration of conventions 95 | * Added non-generic overload to the `AddAllConvention` 96 | * Added option to scan for internal types in an assembly - `IAssemblyScanner.InternalTypes()` 97 | * Strong named the assembly 98 | * Switched to [Semantic Versioning](http://semver.org/) 99 | 100 | #### v1.3 101 | * Documented public facing methods 102 | * Included the xml documentation in the NuGet package 103 | * Debugging symbols are available on SymbolSource.org 104 | * Several more overloads for adding assemblies to the scanner 105 | * One more overload for adding a registry that takes an instance of a UnityRegistry 106 | * New convention that scans for registries 107 | * New convention that uses an overridable naming convention for registering a type mapping 108 | * MakeSingleton() and MakeSingleton(string) is marked as obsolete. Use Configure().AsSingleton() or Configure().WithName(name).AsSingleton() instead 109 | * ConfigureCtorArgsFor(params object[] args) is marked as obsolete. Use Configure().WithConstructorArguments(params object[]) instead 110 | * Added Transient, PerThread, PerResolve, ExternallyControlled and HierarchicalControlled lifetime scope configuration 111 | 112 | ## License 113 | 114 | http://thedersen.mit-license.org/ -------------------------------------------------------------------------------- /how_to_build.txt: -------------------------------------------------------------------------------- 1 | How to build 2 | ================== 3 | 4 | Prerequisites 5 | ------------- 6 | 7 | 1. Download and install Ruby 1.8.7+ from http://www.ruby-lang.org/en/downloads 8 | 2. At the command prompt run the following to update RubyGems to the latest version: 9 | 10 | gem update --system 11 | 12 | 3. You will need the albacore gem, install this at the command prompt with: 13 | 14 | gem install albacore 15 | 16 | If you have already installed albacore, please update to the lastest version (0.2.2+): 17 | 18 | gem update albacore 19 | 20 | 21 | Building 22 | -------------- 23 | 24 | 1. At the command prompt, navigate to the root folder (should contain rakefile.rb) of the project 25 | 2. To run the default build (which will compile, test and package UnityConfiguration) type the following command: 26 | 27 | rake 28 | 29 | In addition, you can see the full list of all the build tasks by running: 30 | 31 | rake -T 32 | 33 | To run a particular task ('test' for example), use the following command: 34 | 35 | rake test 36 | 37 | After the build has completed, there will be a new folder in the root called "build". It contains the following folders: 38 | 39 | * binaries -> All the UnityConfiguration assembilies and their dependencies 40 | * nuget -> The UnityConfiguration[version].nupkg -------------------------------------------------------------------------------- /rakefile.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'albacore' 3 | require 'rake/clean' 4 | 5 | VERSION = "1.4.1" 6 | DESCRIPTION = "Convention based configuration API for the Microsoft Unity IoC container. With just a few lines of code, you can now registere all your classes in the entire solution. If the built-in conventions doesn't fit your needs, it is very easy to extend with your own." 7 | 8 | OUTPUT = "build" 9 | CONFIGURATION = 'Release' 10 | ASSEMBLY_INFO = 'src/UnityConfiguration/Properties/AssemblyInfo.cs' 11 | SOLUTION_FILE = 'src/UnityConfiguration.sln' 12 | 13 | Albacore.configure do |config| 14 | config.log_level = :verbose 15 | config.msbuild.use :net4 16 | end 17 | 18 | desc "Compiles solution and runs unit tests" 19 | task :default => [:clean, :version, :compile, :test, :publish, :package] 20 | 21 | desc "Executes all NUnit tests" 22 | task :test => [:nunit] 23 | 24 | #Add the folders that should be cleaned as part of the clean task 25 | CLEAN.include(OUTPUT) 26 | CLEAN.include(FileList["src/**/#{CONFIGURATION}"]) 27 | CLEAN.include("TestResult.xml") 28 | CLEAN.include("*.nuspec") 29 | 30 | desc "Update assemblyinfo file for the build" 31 | assemblyinfo :version => [:clean] do |asm| 32 | asm.version = VERSION 33 | asm.company_name = "UnityConfiguration" 34 | asm.product_name = "UnityConfiguration" 35 | asm.title = "UnityConfiguration" 36 | asm.description = DESCRIPTION 37 | asm.copyright = "Copyright (C) 2011 Thomas Pedersen" 38 | asm.output_file = ASSEMBLY_INFO 39 | asm.com_visible = false 40 | end 41 | 42 | desc "Compile solution file" 43 | msbuild :compile => [:version] do |msb| 44 | msb.properties :configuration => CONFIGURATION 45 | msb.targets :Clean, :Build 46 | msb.solution = SOLUTION_FILE 47 | end 48 | 49 | desc "Gathers output files and copies them to the output folder" 50 | task :publish => [:compile] do 51 | Dir.mkdir(OUTPUT) 52 | Dir.mkdir("#{OUTPUT}/binaries") 53 | 54 | FileUtils.cp_r FileList["src/**/#{CONFIGURATION}/*.*"].exclude(/obj\//).exclude(/.Tests/), "#{OUTPUT}/binaries" 55 | end 56 | 57 | desc "Executes NUnit tests" 58 | nunit :nunit => [:compile] do |nunit| 59 | tests = FileList["src/**/#{CONFIGURATION}/*.Tests.dll"].exclude(/obj\//) 60 | 61 | nunit.command = "src/packages/NUnit.2.5.10.11092/Tools/nunit-console-x86.exe" 62 | nunit.assemblies = tests 63 | end 64 | 65 | desc "Creates a NuGet packaged based on the UnityConfiguration.nuspec file" 66 | nugetpack :package => [:publish, :nuspec] do |nuget| 67 | Dir.mkdir("#{OUTPUT}/nuget") 68 | 69 | nuget.command = "src/.nuget/nuget.exe" 70 | nuget.nuspec = "UnityConfiguration.nuspec" 71 | nuget.base_folder = "#{OUTPUT}/binaries/" 72 | nuget.output = "#{OUTPUT}/nuget/" 73 | nuget.symbols = true 74 | end 75 | 76 | desc "Create the nuget package specification" 77 | nuspec do |nuspec| 78 | nuspec.id ="UnityConfiguration" 79 | nuspec.version = VERSION 80 | nuspec.authors = "Thomas Pedersen (thedersen)" 81 | nuspec.description = DESCRIPTION 82 | nuspec.language = "en-US" 83 | nuspec.licenseUrl = "http://thedersen.mit-license.org/" 84 | nuspec.projectUrl = "https://github.com/thedersen/UnityConfiguration" 85 | nuspec.tags = "unity ioc convention auto" 86 | nuspec.file "UnityConfiguration.dll", "lib/net35" 87 | nuspec.file "UnityConfiguration.pdb", "lib/net35" 88 | nuspec.file "UnityConfiguration.xml", "lib/net35" 89 | nuspec.file "..\\..\\src\\**\\*.cs", "src" 90 | nuspec.dependency "Unity", "2.0" 91 | nuspec.output_file = "UnityConfiguration.nuspec" 92 | end 93 | 94 | desc "Pushes and publishes the NuGet package to nuget.org" 95 | nugetpush :release => [:package] do |nuget| 96 | nuget.command = "src/.nuget/nuget.exe" 97 | nuget.package = "#{OUTPUT}/nuget/UnityConfiguration.#{VERSION}.nupkg" 98 | nuget.create_only = false 99 | end -------------------------------------------------------------------------------- /src/.nuget/NuGet.Config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/.nuget/NuGet.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedersen/UnityConfiguration/a91e69fbe7cfdd3c72941303e38f728577f7502d/src/.nuget/NuGet.exe -------------------------------------------------------------------------------- /src/.nuget/NuGet.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildProjectDirectory)\..\ 5 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) 6 | $(NuGetToolsPath)\nuget.exe 7 | $([System.IO.Path]::Combine($(ProjectDir), "packages.config")) 8 | $([System.IO.Path]::Combine($(SolutionDir), "packages")) 9 | $(TargetDir.Trim('\\')) 10 | 11 | 12 | "" 13 | 14 | 15 | false 16 | 17 | 18 | false 19 | 20 | 21 | "$(NuGetExePath)" install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)" 22 | "$(NuGetExePath)" pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols 23 | 24 | 25 | 26 | RestorePackages; 27 | $(BuildDependsOn); 28 | 29 | 30 | 31 | 32 | $(BuildDependsOn); 33 | BuildPackage; 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 46 | 47 | 48 | 49 | 51 | 52 | -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/AddAllConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using NUnit.Framework; 3 | using Unity; 4 | using UnityConfiguration.Services; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class AddAllConventionTests 10 | { 11 | [Test] 12 | public void Adds_all_types_implementing_the_specified_type_with_default_name() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | container.Configure(x => x.Scan(scan => 17 | { 18 | scan.AssemblyContaining(); 19 | scan.WithAddAllConvention().TypesImplementing(); 20 | })); 21 | 22 | Assert.That(container.ResolveAll().Count(), Is.EqualTo(2)); 23 | Assert.That(container.ResolveAll().First(), Is.Not.SameAs(container.ResolveAll().First())); 24 | } 25 | 26 | [Test] 27 | public void Can_add_all_as_singletons() 28 | { 29 | var container = new UnityContainer(); 30 | 31 | container.Configure(x => x.Scan(scan => 32 | { 33 | scan.AssemblyContaining(); 34 | scan.WithAddAllConvention().TypesImplementing().AsSingleton(); 35 | })); 36 | 37 | Assert.That(container.ResolveAll().First(), Is.SameAs(container.ResolveAll().First())); 38 | } 39 | 40 | [Test] 41 | public void Can_add_all_as_singletons_and_override_the_default_naming_convention() 42 | { 43 | var container = new UnityContainer(); 44 | 45 | container.Configure(x => x.Scan(scan => 46 | { 47 | scan.AssemblyContaining(); 48 | scan.WithAddAllConvention().TypesImplementing().WithName(t => "test").AsSingleton(); 49 | })); 50 | 51 | Assert.That(container.Resolve("test"), Is.SameAs(container.Resolve("test"))); 52 | } 53 | 54 | [Test] 55 | public void Can_override_the_default_naming_convention() 56 | { 57 | var container = new UnityContainer(); 58 | 59 | container.Configure(x => x.Scan(scan => 60 | { 61 | scan.AssemblyContaining(); 62 | scan.WithAddAllConvention().TypesImplementing().WithName(t => "test"); 63 | })); 64 | 65 | Assert.That(container.Resolve("test"), Is.Not.Null); 66 | } 67 | 68 | [Test] 69 | public void Can_add_all_implementations_of_an_open_generic() 70 | { 71 | var container = new UnityContainer(); 72 | 73 | container.Configure(x => x.Scan(scan => 74 | { 75 | scan.AssemblyContaining(); 76 | scan.WithAddAllConvention().TypesImplementing(typeof(IHandler<>)); 77 | })); 78 | 79 | Assert.That(container.ResolveAll>().Count(), Is.EqualTo(2)); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/ConstructorConfigurationTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class ConstructorConfigurationTests 9 | { 10 | [Test] 11 | public void Can_configure_ctor_arguments_for_type() 12 | { 13 | var container = new UnityContainer(); 14 | 15 | container.Configure(x => 16 | { 17 | x.Register(); 18 | x.Register(); 19 | x.Configure().WithConstructorArguments("some string", typeof (IFooService)); 20 | }); 21 | 22 | var serviceWithCtorArgs = container.Resolve(); 23 | Assert.That(serviceWithCtorArgs.SomeString, Is.EqualTo("some string")); 24 | Assert.That(serviceWithCtorArgs.FooService, Is.InstanceOf()); 25 | } 26 | 27 | [Test] 28 | public void Can_select_constructor_to_use() 29 | { 30 | var container = new UnityContainer(); 31 | 32 | container.Configure(x => 33 | { 34 | x.Register(); 35 | x.SelectConstructor(); 36 | }); 37 | 38 | var serviceWithCtorArgs = container.Resolve(); 39 | Assert.That(serviceWithCtorArgs.SomeString, Is.Null); 40 | Assert.That(serviceWithCtorArgs.FooService, Is.Null); 41 | } 42 | 43 | [Test] 44 | public void Can_select_constructor_to_use_2() 45 | { 46 | var container = new UnityContainer(); 47 | 48 | container.Configure(x => 49 | { 50 | x.Register(); 51 | x.Register(); 52 | x.SelectConstructor(typeof (IFooService)); 53 | }); 54 | 55 | var serviceWithCtorArgs = container.Resolve(); 56 | Assert.That(serviceWithCtorArgs.SomeString, Is.Null); 57 | Assert.That(serviceWithCtorArgs.FooService, Is.InstanceOf()); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/CustomConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Unity; 4 | using UnityConfiguration.Services; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class CustomConventionTests 10 | { 11 | [Test] 12 | public void Can_create_custom_conventions() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | container.Configure(x => x.Scan(scan => 17 | { 18 | scan.AssemblyContaining(); 19 | scan.With(); 20 | })); 21 | 22 | Assert.That(container.Resolve("Custom"), Is.InstanceOf()); 23 | } 24 | } 25 | 26 | public class CustomConvention : IAssemblyScannerConvention 27 | { 28 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 29 | { 30 | if (type == typeof(FooService)) 31 | registry.Register().WithName("Custom"); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Diagnostics/DiagnosticsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using Unity.Lifetime; 4 | using UnityConfiguration.Services; 5 | 6 | namespace UnityConfiguration.Diagnostics 7 | { 8 | [TestFixture] 9 | public class DiagnosticsTests 10 | { 11 | [Test] 12 | public void Prints_all_configured_types() 13 | { 14 | var container = new UnityContainer() 15 | .RegisterType() 16 | .RegisterType(new ContainerControlledLifetimeManager()) 17 | .RegisterType("Bar", new TransientLifetimeManager()) 18 | .RegisterType("Foo", new ContainerControlledLifetimeManager()); 19 | 20 | var report = container.WhatDoIHave(); 21 | 22 | var expexted = new string[] 23 | { 24 | "Unity.IUnityContainer - Unity.UnityContainer with ContainerLifetimeManager", 25 | "UnityConfiguration.Services.IBarService - UnityConfiguration.Services.BarService named \"Bar\" with TransientLifetimeManager", 26 | "UnityConfiguration.Services.IBarService - UnityConfiguration.Services.BarService with ContainerControlledLifetimeManager", 27 | "UnityConfiguration.Services.IFooService - UnityConfiguration.Services.FooService with TransientLifetimeManager", 28 | "UnityConfiguration.Services.IFooService - UnityConfiguration.Services.FooService named \"Foo\" with ContainerControlledLifetimeManager", 29 | }; 30 | 31 | foreach (var s in expexted) 32 | { 33 | Assert.IsTrue(report.Contains(s)); 34 | } 35 | 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/ExploratoryTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | 4 | namespace UnityConfiguration 5 | { 6 | [TestFixture] 7 | public class ExploratoryTests 8 | { 9 | [Test] 10 | public void Can_resolve_the_container_without_registering_it() 11 | { 12 | var container = new UnityContainer(); 13 | 14 | container.Configure(x => { }); 15 | 16 | Assert.That(container.Resolve(), Is.SameAs(container)); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Extensions.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration 2 | { 3 | public static class Extensions 4 | { 5 | public static T As(this object obj) where T : class 6 | { 7 | return obj as T; 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/FilterTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | using UnityConfiguration.Services.OtherNamespace; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class FilterTests 10 | { 11 | [Test] 12 | public void Can_exclude_namespace_containing_type() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | container.Configure(x => x.Scan(scan => 17 | { 18 | scan.AssemblyContaining(); 19 | scan.With(); 20 | scan.ExcludeNamespaceContaining(); 21 | })); 22 | 23 | Assert.Throws(() => container.Resolve()); 24 | } 25 | 26 | [Test] 27 | public void Can_exclude_type() 28 | { 29 | var container = new UnityContainer(); 30 | 31 | container.Configure(x => x.Scan(scan => 32 | { 33 | scan.AssemblyContaining(); 34 | scan.With(); 35 | scan.ExcludeType(); 36 | })); 37 | 38 | Assert.Throws(() => container.Resolve()); 39 | } 40 | 41 | [Test] 42 | public void Can_exclude_type_using_delegate() 43 | { 44 | var container = new UnityContainer(); 45 | 46 | container.Configure(x => x.Scan(scan => 47 | { 48 | scan.AssemblyContaining(); 49 | scan.With(); 50 | scan.Exclude(t => t == typeof (BarService)); 51 | })); 52 | 53 | Assert.Throws(() => container.Resolve()); 54 | } 55 | 56 | [Test] 57 | public void Can_include_namespace() 58 | { 59 | var container = new UnityContainer(); 60 | 61 | container.Configure(x => x.Scan(scan => 62 | { 63 | scan.AssemblyContaining(); 64 | scan.With(); 65 | scan.IncludeNamespace("UnityConfiguration.Services.OtherNamespace"); 66 | })); 67 | 68 | Assert.Throws(() => container.Resolve()); 69 | Assert.That(container.Resolve(), Is.InstanceOf()); 70 | } 71 | 72 | [Test] 73 | public void Can_include_namespace_containing_type() 74 | { 75 | var container = new UnityContainer(); 76 | 77 | container.Configure(x => x.Scan(scan => 78 | { 79 | scan.AssemblyContaining(); 80 | scan.With(); 81 | scan.IncludeNamespaceContaining(); 82 | })); 83 | 84 | Assert.Throws(() => container.Resolve()); 85 | Assert.That(container.Resolve(), Is.InstanceOf()); 86 | } 87 | 88 | [Test] 89 | public void Can_include_using_delegate() 90 | { 91 | var container = new UnityContainer(); 92 | 93 | container.Configure(x => x.Scan(scan => 94 | { 95 | scan.AssemblyContaining(); 96 | scan.With(); 97 | scan.Include(t => t == typeof (ServiceInOtherNamespace)); 98 | })); 99 | 100 | Assert.Throws(() => container.Resolve()); 101 | Assert.That(container.Resolve(), Is.InstanceOf()); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/FirstInterfaceConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | using NUnit.Framework; 3 | using Unity; 4 | using UnityConfiguration.Services; 5 | using UnityConfiguration.UserInterface; 6 | 7 | namespace UnityConfiguration 8 | { 9 | [TestFixture] 10 | public class FirstInterfaceConventionTests 11 | { 12 | [Test] 13 | public void Can_ignore_interfaces_on_base_classes() 14 | { 15 | var container = new UnityContainer(); 16 | 17 | container.Configure(x => x.Scan(scan => 18 | { 19 | scan.AssemblyContaining(); 20 | scan.WithFirstInterfaceConvention().IgnoreInterfacesOnBaseTypes(); 21 | })); 22 | 23 | Assert.That(container.Resolve(), Is.InstanceOf()); 24 | } 25 | 26 | [Test] 27 | public void Includes_interfaces_on_base_classes_in_search_for_first() 28 | { 29 | var container = new UnityContainer(); 30 | 31 | container.Configure(x => x.Scan(scan => 32 | { 33 | scan.AssemblyContaining(); 34 | scan.WithFirstInterfaceConvention(); 35 | })); 36 | 37 | Assert.That(container.Resolve(), Is.InstanceOf()); 38 | } 39 | 40 | [Test] 41 | public void Registers_all_types_with_the_first_interface_implemented_on_the_type() 42 | { 43 | var container = new UnityContainer(); 44 | 45 | container.Configure(x => x.Scan(scan => 46 | { 47 | scan.AssemblyContaining(); 48 | scan.WithFirstInterfaceConvention(); 49 | })); 50 | 51 | Assert.That(container.Resolve(), Is.InstanceOf()); 52 | Assert.That(container.Resolve(), Is.InstanceOf()); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/LifetimeManagementTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | using UnityConfiguration; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class LifetimeManagementTests 10 | { 11 | [Test] 12 | public void Can_configure_concrete_types_as_singleton() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | UnityConfiguration.UnityExtension.Configure(container, x => x.Register()); 17 | 18 | container.Configure(x => 19 | { 20 | x.Register(); 21 | #pragma warning disable 612,618 // obsolete 22 | x.MakeSingleton(); 23 | #pragma warning restore 612,618 24 | }); 25 | 26 | Assert.That(container.Resolve(), Is.SameAs(container.Resolve())); 27 | } 28 | 29 | [Test] 30 | public void Can_configure_concrete_types_as_singleton_using_register() 31 | { 32 | var container = new UnityContainer(); 33 | 34 | container.Configure(x => 35 | { 36 | x.Register(); 37 | x.Configure().AsSingleton(); 38 | }); 39 | 40 | Assert.That(container.Resolve(), Is.SameAs(container.Resolve())); 41 | } 42 | 43 | [Test] 44 | public void Can_configure_named_registration_as_singleton_using_register() 45 | { 46 | var container = new UnityContainer(); 47 | 48 | container.Configure(x => 49 | { 50 | x.Register().WithName("name"); 51 | x.Configure().WithName("name").AsSingleton(); 52 | }); 53 | 54 | Assert.That(container.Resolve("name"), Is.SameAs(container.Resolve("name"))); 55 | } 56 | 57 | [Test] 58 | public void Can_configure_interfaces_as_singleton() 59 | { 60 | var container = new UnityContainer(); 61 | 62 | container.Configure(x => 63 | { 64 | x.Register(); 65 | #pragma warning disable 612,618 // obsolete 66 | x.MakeSingleton(); 67 | #pragma warning restore 612,618 68 | }); 69 | 70 | Assert.That(container.Resolve(), Is.SameAs(container.Resolve())); 71 | } 72 | 73 | [Test] 74 | public void Can_configure_interfaces_as_singleton_using_register() 75 | { 76 | var container = new UnityContainer(); 77 | 78 | container.Configure(x => 79 | { 80 | x.Register(); 81 | x.Configure().AsSingleton(); 82 | }); 83 | 84 | Assert.That(container.Resolve(), Is.SameAs(container.Resolve())); 85 | } 86 | 87 | 88 | [Test] 89 | public void Can_make_transient_sevice_a_singleton_in_child_container() 90 | { 91 | var container = new UnityContainer() as IUnityContainer; 92 | container.Configure(x => x.Register()); 93 | 94 | var childContainer = container.CreateChildContainer(); 95 | 96 | childContainer.Configure(x => x.Configure().AsSingleton()); 97 | 98 | Assert.That(container.Resolve(), Is.Not.SameAs(container.Resolve())); 99 | Assert.That(container.Resolve(), Is.Not.SameAs(childContainer.Resolve())); 100 | Assert.That(childContainer.Resolve(), Is.SameAs(childContainer.Resolve())); 101 | } 102 | 103 | [Test] 104 | public void Configure_interface_as_singletons_makes_all_registrations_a_singleton() 105 | { 106 | var container = new UnityContainer(); 107 | 108 | container.Configure(x => 109 | { 110 | x.Scan(scan => 111 | { 112 | scan.AssemblyContaining(); 113 | scan.With().TypesImplementing(); 114 | }); 115 | x.Configure().AsSingleton(); 116 | }); 117 | 118 | Assert.That(container.Resolve("Implementation1"), Is.SameAs(container.Resolve("Implementation1"))); 119 | Assert.That(container.Resolve("Implementation2"), Is.SameAs(container.Resolve("Implementation2"))); 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/NamingConventionTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | 4 | namespace UnityConfiguration 5 | { 6 | [TestFixture] 7 | public class NamingConventionTests 8 | { 9 | [Test] 10 | public void Registers_with_default_naming_convention() 11 | { 12 | var container = new UnityContainer(); 13 | 14 | container.Configure(x => x.Scan(scan => 15 | { 16 | scan.AssemblyContaining(); 17 | scan.WithNamingConvention(); 18 | })); 19 | 20 | Assert.That(container.Resolve(), Is.InstanceOf()); 21 | } 22 | 23 | [Test] 24 | public void Can_override_the_default_naming_convention() 25 | { 26 | var container = new UnityContainer(); 27 | 28 | container.Configure(x => x.Scan(scan => 29 | { 30 | scan.AssemblyContaining(); 31 | scan.ExcludeType(); 32 | 33 | scan.WithNamingConvention().WithInterfaceName(t => $"I{t.Name}Service"); 34 | })); 35 | 36 | Assert.That(container.Resolve(), Is.InstanceOf()); 37 | } 38 | 39 | [Test] 40 | public void Does_not_register_when_interface_is_not_found() 41 | { 42 | var container = new UnityContainer(); 43 | 44 | container.Configure(x => x.Scan(scan => 45 | { 46 | scan.AssemblyContaining(); 47 | 48 | scan.WithNamingConvention().WithInterfaceName(t => "IDoNotExist"); 49 | })); 50 | 51 | Assert.That(container.IsRegistered(), Is.False); 52 | } 53 | } 54 | 55 | public class NamedService : IInterface, INamedService 56 | { 57 | } 58 | 59 | public class Named : INamedService 60 | { 61 | } 62 | 63 | public interface INamedService 64 | { 65 | } 66 | 67 | public interface IInterface{} 68 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/OpenGenericTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class OpenGenericTests 9 | { 10 | [Test] 11 | public void Can_connect_implementations_to_open_generic_types() 12 | { 13 | var container = new UnityContainer(); 14 | 15 | container.Configure(x => x.Scan(scan => 16 | { 17 | scan.AssemblyContaining(); 18 | scan.With(); 19 | scan.ExcludeType(); 20 | })); 21 | 22 | Assert.That(container.Resolve>(), Is.InstanceOf()); 23 | Assert.That(container.Resolve>(), Is.InstanceOf()); 24 | } 25 | 26 | [Test] 27 | public void Can_connect_implementations_to_open_generic_types_2() 28 | { 29 | var container = new UnityContainer(); 30 | 31 | container.Configure(x => x.Scan(scan => 32 | { 33 | scan.AssemblyContaining(); 34 | scan.With(); 35 | scan.ExcludeType(); 36 | })); 37 | 38 | Assert.That(container.Resolve>(), Is.InstanceOf()); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/PostBuildUpActionTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using NUnit.Framework.Interfaces; 4 | using Unity; 5 | using UnityConfiguration.Diagnostics; 6 | using UnityConfiguration.Services; 7 | 8 | namespace UnityConfiguration 9 | { 10 | [TestFixture] 11 | public class PostBuildUpActionTests 12 | { 13 | [Test] 14 | public void Can_call_method_on_concrete_after_build_up() 15 | { 16 | var container = new UnityContainer(); 17 | container.Configure(x => x.AfterBuildingUp().Call((c, s) => s.Start())); 18 | 19 | Assert.That(container.Resolve().StartWasCalled); 20 | } 21 | 22 | [Test] 23 | public void Can_call_method_on_interface_after_build_up() 24 | { 25 | var container = new UnityContainer(); 26 | container.Configure(x => 27 | { 28 | x.Register(); 29 | x.AfterBuildingUp().Call((c, s) => s.Start()); 30 | }); 31 | 32 | Assert.That(container.Resolve().StartWasCalled); 33 | } 34 | 35 | [Test] 36 | public void Can_call_method_on_interface_after_build_up_2() 37 | { 38 | var container = new UnityContainer(); 39 | container.Configure(x => 40 | { 41 | x.Register().WithName("1"); 42 | x.Register().WithName("2"); 43 | x.AfterBuildingUp().Call((c, s) => s.Start()); 44 | }); 45 | 46 | Assert.That(container.Resolve("1").StartWasCalled); 47 | Assert.That(container.Resolve("2").StartWasCalled); 48 | } 49 | 50 | [Test] 51 | public void Can_call_method_on_several_interfaces_after_build_up() 52 | { 53 | var container = new UnityContainer(); 54 | container.Configure(x => 55 | { 56 | x.Register(); 57 | x.Register(); 58 | x.AfterBuildingUp().Call((c, s) => s.Start()); 59 | x.AfterBuildingUp().Call((c, s) => s.Stop()); 60 | }); 61 | 62 | Assert.That(container.Resolve().StartWasCalled); 63 | Assert.That(container.Resolve().StopWasCalled); 64 | } 65 | 66 | [Test] 67 | public void Can_decorate_concrete_service_after_build_up() 68 | { 69 | var container = new UnityContainer(); 70 | container.Configure(x => 71 | { 72 | x.Register(); 73 | x.AfterBuildingUp().DecorateWith((c, t) => new FooDecorator(t)); 74 | }); 75 | 76 | var fooService = container.Resolve(); 77 | Assert.That(fooService, Is.InstanceOf()); 78 | Assert.That(fooService.As().InnerService, Is.InstanceOf()); 79 | } 80 | 81 | [Test] 82 | public void Can_decorate_interface_after_build_up() 83 | { 84 | var container = new UnityContainer(); 85 | container.Configure(x => 86 | { 87 | x.Register(); 88 | x.AfterBuildingUp().DecorateWith((c, t) => new FooDecorator(t)); 89 | }); 90 | 91 | var fooService = container.Resolve(); 92 | Assert.That(fooService, Is.InstanceOf()); 93 | Assert.That(fooService.As().InnerService, Is.InstanceOf()); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedersen/UnityConfiguration/a91e69fbe7cfdd3c72941303e38f728577f7502d/src/UnityConfiguration.Tests/Properties/AssemblyInfo.cs -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/PropertyInjectionTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class PropertyInjectionTests 9 | { 10 | [Test] 11 | public void Can_configure_property_injection_using_convention() 12 | { 13 | var container = new UnityContainer(); 14 | container.Configure(x => x.Scan(scan => 15 | { 16 | scan.AssemblyContaining(); 17 | scan.WithFirstInterfaceConvention(); 18 | scan.WithSetAllPropertiesConvention().OfType(); 19 | })); 20 | 21 | Assert.That(container.Resolve().Logger, Is.Not.Null); 22 | } 23 | 24 | [Test] 25 | public void Can_set_property_after_building_up() 26 | { 27 | var container = new UnityContainer(); 28 | container.Configure(x => 29 | { 30 | x.Register(); 31 | x.AfterBuildingUp().Call((c, s) => s.Logger = c.Resolve()); 32 | }); 33 | 34 | Assert.That(container.Resolve().Logger, Is.Not.Null); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/RegistryTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class RegistryTests 9 | { 10 | [Test] 11 | public void Can_initalize_container_with_one_registry() 12 | { 13 | var container = new UnityContainer(); 14 | 15 | container.Configure(x => x.AddRegistry()); 16 | 17 | Assert.That(container.Resolve(), Is.InstanceOf()); 18 | } 19 | 20 | [Test] 21 | public void Can_initalize_container_with_two_registries() 22 | { 23 | var container = new UnityContainer(); 24 | 25 | container.Configure(x => 26 | { 27 | x.AddRegistry(); 28 | x.AddRegistry(new BarRegistry()); 29 | }); 30 | 31 | Assert.That(container.Resolve(), Is.InstanceOf()); 32 | Assert.That(container.Resolve(), Is.InstanceOf()); 33 | } 34 | 35 | [Test] 36 | public void Can_add_same_registry_twice_without_breaking_anything() 37 | { 38 | var container = new UnityContainer(); 39 | 40 | container.Configure(x => 41 | { 42 | x.AddRegistry(); 43 | x.AddRegistry(); 44 | }); 45 | 46 | Assert.That(container.Resolve(), Is.InstanceOf()); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/ScanForRegistriesConventionTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using UnityConfiguration.Services; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class ScanForRegistriesConventionTests 9 | { 10 | [Test] 11 | public void Finds_all_registries() 12 | { 13 | var container = new UnityContainer(); 14 | 15 | container.Configure(x => x.AddRegistry()); 16 | 17 | Assert.That(container.Resolve(), Is.InstanceOf()); 18 | Assert.That(container.Resolve(), Is.InstanceOf()); 19 | } 20 | 21 | [Test] 22 | public void Finds_all_registries_2() 23 | { 24 | var container = new UnityContainer(); 25 | 26 | container.Configure(x => x.Scan(scan => 27 | { 28 | scan.AssemblyContaining(); 29 | scan.ForRegistries(); 30 | scan.WithFirstInterfaceConvention(); 31 | })); 32 | 33 | Assert.That(container.Resolve(), Is.InstanceOf()); 34 | Assert.That(container.Resolve(), Is.InstanceOf()); 35 | } 36 | } 37 | 38 | public class MyRegistry : UnityRegistry 39 | { 40 | public MyRegistry() 41 | { 42 | Scan(scan => 43 | { 44 | scan.AssemblyContaining(); 45 | scan.ForRegistries(); 46 | scan.WithFirstInterfaceConvention(); 47 | }); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/ScanningTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using NUnit.Framework; 3 | using Unity; 4 | using UnityConfiguration.Services; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class ScanningTests 10 | { 11 | [Test] 12 | public void Can_scan_using_several_conventions() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | container.Configure(x => x.Scan(scan => 17 | { 18 | scan.AssemblyContaining(); 19 | scan.With(); 20 | scan.With().TypesImplementing(); 21 | })); 22 | 23 | Assert.That(container.Resolve(), Is.InstanceOf()); 24 | Assert.That(container.Resolve(), Is.InstanceOf()); 25 | Assert.That(container.ResolveAll().Count(), Is.EqualTo(2)); 26 | } 27 | 28 | [Test] 29 | public void Can_scan_assembly() 30 | { 31 | var container = new UnityContainer(); 32 | 33 | container.Configure(x => x.Scan(scan => 34 | { 35 | scan.Assembly(typeof(FooRegistry).Assembly); 36 | scan.With(); 37 | })); 38 | 39 | Assert.That(container.Resolve(), Is.InstanceOf()); 40 | } 41 | 42 | [Test] 43 | public void Can_scan_named_assembly() 44 | { 45 | var container = new UnityContainer(); 46 | 47 | container.Configure(x => x.Scan(scan => 48 | { 49 | scan.Assembly("UnityConfiguration.Tests"); 50 | scan.With(); 51 | })); 52 | 53 | Assert.That(container.Resolve(), Is.InstanceOf()); 54 | } 55 | 56 | [Test] 57 | public void Can_scan_named_assembly_with_extension() 58 | { 59 | var container = new UnityContainer(); 60 | 61 | container.Configure(x => x.Scan(scan => 62 | { 63 | scan.Assembly("UnityConfiguration.Tests.dll"); 64 | scan.With(); 65 | })); 66 | 67 | Assert.That(container.Resolve(), Is.InstanceOf()); 68 | } 69 | 70 | [Test] 71 | public void Can_scan_folder() 72 | { 73 | var container = new UnityContainer(); 74 | 75 | container.Configure(x => x.Scan(scan => 76 | { 77 | scan.AssembliesInDirectory(TestContext.CurrentContext.TestDirectory); 78 | scan.With(); 79 | })); 80 | 81 | var registration = container.Registrations.FirstOrDefault(x => x.RegisteredType == typeof(IFooService)); 82 | 83 | Assert.That(container.Resolve(), Is.InstanceOf()); 84 | } 85 | 86 | [Test] 87 | public void Can_scan_folder_and_exclude_assemblies_by_using_a_predicate() 88 | { 89 | var container = new UnityContainer(); 90 | 91 | container.Configure(x => x.Scan(scan => 92 | { 93 | scan.AssembliesInDirectory(TestContext.CurrentContext.TestDirectory, a => a.GetName().Name != "UnityConfiguration.Tests"); 94 | scan.With(); 95 | })); 96 | 97 | Assert.That(container.IsRegistered(), Is.False); 98 | } 99 | 100 | [Test] 101 | public void Can_scan_base_folder() 102 | { 103 | var container = new UnityContainer(); 104 | 105 | container.Configure(x => x.Scan(scan => 106 | { 107 | scan.AssembliesInBaseDirectory(); 108 | scan.With(); 109 | })); 110 | 111 | Assert.That(container.Resolve(), Is.InstanceOf()); 112 | } 113 | 114 | [Test] 115 | public void Can_scan_base_folder_and_exclude_assemblies_by_using_a_predicate() 116 | { 117 | var container = new UnityContainer(); 118 | 119 | container.Configure(x => x.Scan(scan => 120 | { 121 | scan.AssembliesInBaseDirectory(a => a.GetName().Name != "UnityConfiguration.Tests"); 122 | scan.With(); 123 | })); 124 | 125 | Assert.That(container.IsRegistered(), Is.False); 126 | } 127 | 128 | [Test] 129 | public void Can_scan_for_registries() 130 | { 131 | var container = new UnityContainer(); 132 | 133 | container.Configure(x => x.Scan(scan => 134 | { 135 | scan.AssembliesInBaseDirectory(); 136 | scan.ForRegistries(); 137 | })); 138 | 139 | Assert.That(container.Resolve(), Is.InstanceOf()); 140 | } 141 | 142 | [Test] 143 | public void Can_scan_internal_types_when_specified() 144 | { 145 | var container = new UnityContainer(); 146 | 147 | container.Configure(x => x.Scan(scan => 148 | { 149 | scan.AssembliesInBaseDirectory(); 150 | scan.InternalTypes(); 151 | scan.With(); 152 | })); 153 | 154 | Assert.That(container.Resolve(), Is.InstanceOf()); 155 | } 156 | } 157 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/AnotherMessage.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class AnotherMessage 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/AnotherMessageHandler.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class AnotherMessageHandler : IHandler 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/BarRegistry.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class BarRegistry : UnityRegistry 4 | { 5 | public BarRegistry() 6 | { 7 | Register(); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/BarService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class BarService : IBarService, IFooService 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/FooDecorator.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class FooDecorator : IFooDecorator, IFooService 4 | { 5 | public FooDecorator(IFooService fooService) 6 | { 7 | InnerService = fooService; 8 | } 9 | 10 | public IFooService InnerService { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/FooRegistry.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class FooRegistry : UnityRegistry 4 | { 5 | public FooRegistry() 6 | { 7 | Register(); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/FooService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class FooService : IFooService 4 | { 5 | public ILogger Logger { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IBarService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IBarService 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IFooDecorator.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IFooDecorator 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IFooService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IFooService 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IHandler.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IHandler 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IHaveManyImplementations.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IHaveManyImplementations 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IInternalService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | internal interface IInternalService 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/ILogger.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface ILogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IMapper.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IMapper 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IServiceWithCtorArgs.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IServiceWithCtorArgs 4 | { 5 | string SomeString { get; set; } 6 | IFooService FooService { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IStartable.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IStartable 4 | { 5 | bool StartWasCalled { get; set; } 6 | void Start(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/IStoppable.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public interface IStoppable 4 | { 5 | bool StopWasCalled { get; set; } 6 | void Stop(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/Implementation1.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class Implementation1 : IHaveManyImplementations 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/Implementation2.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class Implementation2 : IHaveManyImplementations 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/InternalService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | internal class InternalService : IInternalService 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/Message.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class Message 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/MessageHandler.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class MessageHandler : IHandler 4 | { 5 | } 6 | 7 | public class MessageHandler2 : IHandler 8 | { 9 | } 10 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/MessageToAnotherMessageMapper.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class MessageToAnotherMessageMapper : IMapper 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/NullLogger.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class NullLogger : ILogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/OtherNamespace/IServiceInOtherNamespace.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services.OtherNamespace 2 | { 3 | public interface IServiceInOtherNamespace 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/OtherNamespace/ServiceInOtherNamespace.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services.OtherNamespace 2 | { 3 | public class ServiceInOtherNamespace : IServiceInOtherNamespace 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/ServiceWithCtorArgs.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class ServiceWithCtorArgs : IServiceWithCtorArgs 4 | { 5 | public ServiceWithCtorArgs() 6 | { 7 | } 8 | 9 | public ServiceWithCtorArgs(IFooService fooService) 10 | { 11 | FooService = fooService; 12 | } 13 | 14 | public ServiceWithCtorArgs(string someString, IFooService fooService) 15 | { 16 | SomeString = someString; 17 | FooService = fooService; 18 | } 19 | 20 | #region IServiceWithCtorArgs Members 21 | 22 | public string SomeString { get; set; } 23 | public IFooService FooService { get; set; } 24 | 25 | #endregion 26 | } 27 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/StartableService1.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class StartableService1 : IStartable 4 | { 5 | #region IStartable Members 6 | 7 | public void Start() 8 | { 9 | StartWasCalled = true; 10 | } 11 | 12 | public bool StartWasCalled { get; set; } 13 | 14 | #endregion 15 | } 16 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/StartableService2.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class StartableService2 : IStartable 4 | { 5 | #region IStartable Members 6 | 7 | public void Start() 8 | { 9 | StartWasCalled = true; 10 | } 11 | 12 | public bool StartWasCalled { get; set; } 13 | 14 | #endregion 15 | } 16 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/Services/StoppableService.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.Services 2 | { 3 | public class StoppableService : IStoppable 4 | { 5 | #region IStoppable Members 6 | 7 | public void Stop() 8 | { 9 | StopWasCalled = true; 10 | } 11 | 12 | public bool StopWasCalled { get; set; } 13 | 14 | #endregion 15 | } 16 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/TypeRegistrationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Unity; 4 | using UnityConfiguration.Services; 5 | 6 | namespace UnityConfiguration 7 | { 8 | [TestFixture] 9 | public class TypeRegistrationTests 10 | { 11 | [Test] 12 | public void Can_register_a_func_with_parameters() 13 | { 14 | var container = new UnityContainer(); 15 | 16 | var myService = new BarService(); 17 | container.Configure(x => x.Register>(c => i => myService)); 18 | 19 | Assert.That(container.Resolve>()(1), Is.SameAs(myService)); 20 | } 21 | 22 | [Test] 23 | public void Can_register_named_instance() 24 | { 25 | var container = new UnityContainer(); 26 | container.Configure(x => x.Register().WithName("name")); 27 | 28 | Assert.That(container.Resolve("name"), Is.InstanceOf()); 29 | } 30 | 31 | [Test] 32 | public void Can_register_named_instance_using_factory_delegate() 33 | { 34 | var container = new UnityContainer(); 35 | 36 | var myService = new BarService(); 37 | container.Configure(x => x.Register(c => myService).WithName("name")); 38 | 39 | Assert.That(container.Resolve("name"), Is.SameAs(myService)); 40 | } 41 | 42 | [Test] 43 | public void Can_register_named_singleton_instance() 44 | { 45 | var container = new UnityContainer(); 46 | container.Configure(x => x.Register().WithName("name").AsSingleton()); 47 | 48 | Assert.That(container.Resolve("name"), Is.SameAs(container.Resolve("name"))); 49 | } 50 | 51 | [Test] 52 | public void Can_register_singleton() 53 | { 54 | var container = new UnityContainer(); 55 | 56 | container.Configure(x => x.Register().AsSingleton()); 57 | 58 | Assert.That(container.Resolve(), Is.InstanceOf()); 59 | Assert.That(container.Resolve(), Is.SameAs(container.Resolve())); 60 | } 61 | 62 | [Test] 63 | public void Can_register_transient_type() 64 | { 65 | var container = new UnityContainer(); 66 | 67 | container.Configure(x => x.Register()); 68 | 69 | Assert.That(container.Resolve(), Is.InstanceOf()); 70 | Assert.That(container.Resolve(), Is.Not.SameAs(container.Resolve())); 71 | } 72 | 73 | [Test] 74 | public void Can_register_using_factory_delegate() 75 | { 76 | var container = new UnityContainer(); 77 | 78 | var myService = new BarService(); 79 | container.Configure(x => x.Register(c => myService)); 80 | 81 | Assert.That(container.Resolve(), Is.SameAs(myService)); 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/UnityConfiguration.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8.0.30703 8 | 2.0 9 | {337E95CC-230A-4DAC-86BE-F26998402B42} 10 | Library 11 | Properties 12 | UnityConfiguration 13 | UnityConfiguration.Tests 14 | v4.8 15 | 512 16 | 17 | ..\..\src\ 18 | true 19 | 20 | 21 | 22 | 23 | true 24 | full 25 | false 26 | bin\Debug\ 27 | DEBUG;TRACE 28 | prompt 29 | 4 30 | false 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | false 40 | 41 | 42 | 43 | ..\packages\CommonServiceLocator.2.0.5\lib\net48\CommonServiceLocator.dll 44 | 45 | 46 | ..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll 47 | 48 | 49 | 50 | 51 | 52 | ..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | ..\packages\Unity.5.11.7\lib\net48\Unity.Abstractions.dll 61 | 62 | 63 | ..\packages\Unity.5.11.7\lib\net48\Unity.Container.dll 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | UserControl 120 | 121 | 122 | MyView.cs 123 | 124 | 125 | 126 | 127 | {D5FD47DA-D607-46DC-895E-2C823905D147} 128 | UnityConfiguration 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 140 | 141 | 142 | 143 | 150 | -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/UnityExtensionsConfigurationTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Unity; 3 | using Unity.Extension; 4 | 5 | namespace UnityConfiguration 6 | { 7 | [TestFixture] 8 | public class UnityExtensionsConfigurationTests 9 | { 10 | [Test] 11 | public void Can_configure_unity_extensions() 12 | { 13 | const int number = 1; 14 | var container = new UnityContainer(); 15 | 16 | container.Configure(x => 17 | { 18 | x.AddExtension(); 19 | x.ConfigureExtension(c => c.SetNumber(number)); 20 | }); 21 | 22 | Assert.That(MyExtension.Number, Is.EqualTo(number)); 23 | } 24 | 25 | [Test] 26 | public void Should_return_container() 27 | { 28 | var container = new UnityContainer(); 29 | 30 | var result = container.Configure(x => { }); 31 | 32 | Assert.That(result, Is.SameAs(container)); 33 | } 34 | } 35 | 36 | public class MyExtension : UnityContainerExtension 37 | { 38 | public static int Number; 39 | 40 | protected override void Initialize() 41 | { 42 | } 43 | 44 | public void SetNumber(int number) 45 | { 46 | Number = number; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/UserInterface/IMyView.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.UserInterface 2 | { 3 | public interface IMyView 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/UserInterface/MyView.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration.UserInterface 2 | { 3 | partial class MyView 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Component Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | components = new System.ComponentModel.Container(); 32 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 33 | } 34 | 35 | #endregion 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/UserInterface/MyView.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Forms; 2 | 3 | namespace UnityConfiguration.UserInterface 4 | { 5 | public partial class MyView : UserControl, IMyView 6 | { 7 | public MyView() 8 | { 9 | InitializeComponent(); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/UnityConfiguration.Tests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/UnityConfiguration.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityConfiguration", "UnityConfiguration\UnityConfiguration.csproj", "{D5FD47DA-D607-46DC-895E-2C823905D147}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityConfiguration.Tests", "UnityConfiguration.Tests\UnityConfiguration.Tests.csproj", "{337E95CC-230A-4DAC-86BE-F26998402B42}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F1037DC0-D71C-4572-8D61-D88352BFBBE1}" 9 | ProjectSection(SolutionItems) = preProject 10 | .nuget\NuGet.exe = .nuget\NuGet.exe 11 | .nuget\NuGet.targets = .nuget\NuGet.targets 12 | EndProjectSection 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {D5FD47DA-D607-46DC-895E-2C823905D147}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {D5FD47DA-D607-46DC-895E-2C823905D147}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {D5FD47DA-D607-46DC-895E-2C823905D147}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {D5FD47DA-D607-46DC-895E-2C823905D147}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {337E95CC-230A-4DAC-86BE-F26998402B42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {337E95CC-230A-4DAC-86BE-F26998402B42}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {337E95CC-230A-4DAC-86BE-F26998402B42}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {337E95CC-230A-4DAC-86BE-F26998402B42}.Release|Any CPU.Build.0 = Release|Any CPU 28 | EndGlobalSection 29 | GlobalSection(SolutionProperties) = preSolution 30 | HideSolutionNode = FALSE 31 | EndGlobalSection 32 | GlobalSection(ExtensibilityGlobals) = postSolution 33 | EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35 34 | EndGlobalSection 35 | EndGlobal 36 | -------------------------------------------------------------------------------- /src/UnityConfiguration/AddAllConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Unity.Lifetime; 4 | 5 | namespace UnityConfiguration 6 | { 7 | /// 8 | /// Convention for registering several types implementing the same interface. 9 | /// By default it is registered with the name of the type, but this can be overridden. 10 | /// 11 | public class AddAllConvention : IAssemblyScannerConvention, ILifetimePolicyExpression 12 | { 13 | private Func getName = t => t.Name; 14 | private Type interfaceType; 15 | private Action lifetimePolicyAction; 16 | 17 | /// 18 | /// Specify the type to register multiple instances of. 19 | /// 20 | /// The type to register multiple instances of. 21 | /// 22 | /// An instance of the that can be used to 23 | /// further configure the convention. 24 | /// 25 | public AddAllConvention TypesImplementing() 26 | { 27 | interfaceType = typeof (T); 28 | return this; 29 | } 30 | 31 | /// 32 | /// Specify the type to register multiple instances of. 33 | /// 34 | /// The type to register multiple instances of. 35 | /// 36 | /// An instance of the that can be used to 37 | /// further configure the convention. 38 | /// 39 | public AddAllConvention TypesImplementing(Type type) 40 | { 41 | interfaceType = type; 42 | return this; 43 | } 44 | 45 | /// 46 | /// Specify how to resolve the name for the registration. 47 | /// 48 | /// The function to create the name for the specified type. 49 | /// 50 | /// An instance of the that can be used to 51 | /// further configure the convention. 52 | /// 53 | public AddAllConvention WithName(Func func) 54 | { 55 | getName = func; 56 | return this; 57 | } 58 | 59 | public void Using() where T : LifetimeManager, new() 60 | { 61 | lifetimePolicyAction = x => x.Using(); 62 | } 63 | 64 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 65 | { 66 | Type typeFrom = null; 67 | if (type.CanBeCastTo(interfaceType)) 68 | { 69 | typeFrom = interfaceType; 70 | } 71 | else if(type.ImplementsInterfaceTemplate(interfaceType)) 72 | { 73 | 74 | typeFrom = type.GetInterfaces().FirstOrDefault(i => i.GetGenericTypeDefinition() == interfaceType); 75 | } 76 | 77 | if (typeFrom != null && type.CanBeCreated()) 78 | { 79 | var expression = registry.Register(typeFrom, type).WithName(getName(type)); 80 | 81 | if (lifetimePolicyAction != null) 82 | lifetimePolicyAction(expression); 83 | } 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/AddNewExtensionExpression.cs: -------------------------------------------------------------------------------- 1 | using Unity; 2 | using Unity.Extension; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public class AddNewExtensionExpression : Expression where T : UnityContainerExtension, new() 7 | { 8 | internal override void Execute(IUnityContainer container) 9 | { 10 | container.AddNewExtension(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/AssemblyScanner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text.RegularExpressions; 7 | 8 | namespace UnityConfiguration 9 | { 10 | public class AssemblyScanner : IAssemblyScanner 11 | { 12 | private readonly List assemblies = new List(); 13 | private readonly List conventions = new List(); 14 | private readonly CompositeFilter filter = new CompositeFilter(); 15 | private Func> getTypes = a => a.GetExportedTypes(); 16 | 17 | public void Assembly(Assembly assembly) 18 | { 19 | if (!assemblies.Contains(assembly)) 20 | assemblies.Add(assembly); 21 | } 22 | 23 | public void Assembly(string assemblyName) 24 | { 25 | assemblyName = Regex.Replace(assemblyName, ".dll$", string.Empty); 26 | Assembly(AppDomain.CurrentDomain.Load(assemblyName)); 27 | } 28 | 29 | public void AssemblyContaining() 30 | { 31 | Assembly(typeof(T).Assembly); 32 | } 33 | 34 | public void AssembliesInBaseDirectory() 35 | { 36 | AssembliesInBaseDirectory(x => true); 37 | } 38 | 39 | public void AssembliesInBaseDirectory(Predicate predicate) 40 | { 41 | AssembliesInDirectory(AppDomain.CurrentDomain.BaseDirectory, predicate); 42 | AssembliesInDirectory(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath, predicate); 43 | } 44 | 45 | public void AssembliesInDirectory(string path) 46 | { 47 | AssembliesInDirectory(path, a => true); 48 | } 49 | 50 | public void AssembliesInDirectory(string path, Predicate predicate) 51 | { 52 | if (!Directory.Exists(path)) 53 | return; 54 | 55 | var assemblyPaths = from file in Directory.GetFiles(path) 56 | let extension = Path.GetExtension(file) 57 | where extension.Equals(".exe", StringComparison.OrdinalIgnoreCase) || 58 | extension.Equals(".dll", StringComparison.OrdinalIgnoreCase) 59 | select file; 60 | 61 | foreach (var assemblyPath in assemblyPaths) 62 | { 63 | Assembly assembly = null; 64 | try 65 | { 66 | assembly = System.Reflection.Assembly.LoadFrom(assemblyPath); 67 | } 68 | catch 69 | { 70 | // ignore 71 | } 72 | 73 | if (assembly != null && predicate(assembly)) 74 | Assembly(assembly); 75 | } 76 | } 77 | 78 | public void InternalTypes() 79 | { 80 | getTypes = a => a.GetTypes(); 81 | } 82 | 83 | public TConvention With() where TConvention : IAssemblyScannerConvention, new() 84 | { 85 | var convention = new TConvention(); 86 | conventions.Add(convention); 87 | 88 | return convention; 89 | } 90 | 91 | public void Exclude(Predicate exclude) 92 | { 93 | filter.Excludes += exclude; 94 | } 95 | 96 | public void ExcludeNamespace(string @namespace) 97 | { 98 | Exclude(type => type.IsInNamespace(@namespace)); 99 | } 100 | 101 | public void ExcludeNamespaceContaining() 102 | { 103 | ExcludeNamespace(typeof(T).Namespace); 104 | } 105 | 106 | public void ExcludeType() 107 | { 108 | Exclude(type => type == typeof(T)); 109 | } 110 | 111 | public void Include(Predicate include) 112 | { 113 | filter.Includes += include; 114 | } 115 | 116 | public void IncludeNamespace(string @namespace) 117 | { 118 | Include(type => type.IsInNamespace(@namespace)); 119 | } 120 | 121 | public void IncludeNamespaceContaining() 122 | { 123 | IncludeNamespace(typeof(T).Namespace); 124 | } 125 | 126 | public void Scan(IUnityRegistry registry) 127 | { 128 | GetTypes().ForEach(type => ApplyConventions(type, registry)); 129 | } 130 | 131 | private IEnumerable GetTypes() 132 | { 133 | return assemblies.SelectMany(getTypes).Where(t => filter.Matches(t)); 134 | } 135 | 136 | private void ApplyConventions(Type type, IUnityRegistry registry) 137 | { 138 | conventions.ForEach(c => c.Process(type, registry)); 139 | } 140 | } 141 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/CompositeFilter.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2009 Jeremy D. Miller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | namespace UnityConfiguration 18 | { 19 | public class CompositeFilter 20 | { 21 | private readonly CompositePredicate excludes = new CompositePredicate(); 22 | private readonly CompositePredicate includes = new CompositePredicate(); 23 | 24 | public CompositePredicate Includes 25 | { 26 | get { return includes; } 27 | set { } 28 | } 29 | 30 | public CompositePredicate Excludes 31 | { 32 | get { return excludes; } 33 | set { } 34 | } 35 | 36 | public bool Matches(T target) 37 | { 38 | return Includes.MatchesAny(target) && Excludes.DoesNotMatcheAny(target); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/CompositePredicate.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2009 Jeremy D. Miller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Linq; 20 | 21 | namespace UnityConfiguration 22 | { 23 | public class CompositePredicate 24 | { 25 | private readonly List> filters = new List>(); 26 | private Predicate matchesAll = x => true; 27 | private Predicate matchesAny = x => true; 28 | private Predicate matchesNone = x => false; 29 | 30 | private void Add(Predicate filter) 31 | { 32 | matchesAll = x => filters.All(predicate => predicate(x)); 33 | matchesAny = x => filters.Any(predicate => predicate(x)); 34 | matchesNone = x => !MatchesAny(x); 35 | 36 | filters.Add(filter); 37 | } 38 | 39 | public static CompositePredicate operator +(CompositePredicate invokes, Predicate filter) 40 | { 41 | invokes.Add(filter); 42 | return invokes; 43 | } 44 | 45 | public bool MatchesAll(T target) 46 | { 47 | return matchesAll(target); 48 | } 49 | 50 | public bool MatchesAny(T target) 51 | { 52 | return matchesAny(target); 53 | } 54 | 55 | public bool MatchesNone(T target) 56 | { 57 | return matchesNone(target); 58 | } 59 | 60 | public bool DoesNotMatcheAny(T target) 61 | { 62 | return filters.Count == 0 ? true : !MatchesAny(target); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/ConfigureExtensionExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | using Unity.Extension; 4 | 5 | namespace UnityConfiguration 6 | { 7 | public class ConfigureExtensionExpression : Expression where T : IUnityContainerExtensionConfigurator 8 | { 9 | private readonly Action configAction; 10 | 11 | public ConfigureExtensionExpression(Action configAction) 12 | { 13 | this.configAction = configAction; 14 | } 15 | 16 | internal override void Execute(IUnityContainer container) 17 | { 18 | configAction(container.Configure()); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/ConventionExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration 2 | { 3 | /// 4 | /// Extends the with convenion methods for configuring the conventions. 5 | /// 6 | public static class ConventionExtensions 7 | { 8 | /// 9 | /// Adds a convention to the scanner that will look for and include all derived classes it finds. 10 | /// 11 | public static ScanForRegistriesConvention ForRegistries(this IAssemblyScanner scanner) 12 | { 13 | return scanner.With(); 14 | } 15 | 16 | /// 17 | /// Adds a convention to the scanner that registers all types by the first interface defined for the type. 18 | /// 19 | public static FirstInterfaceConvention WithFirstInterfaceConvention(this IAssemblyScanner scanner) 20 | { 21 | return scanner.With(); 22 | } 23 | 24 | /// 25 | /// Adds a convention to the scanner that registers all types by a naming convention. 26 | /// By default it tries to find an interface with the same name as the 27 | /// service, prefixed with an I, but this can be overridden. 28 | /// 29 | public static NamingConvention WithNamingConvention(this IAssemblyScanner scanner) 30 | { 31 | return scanner.With(); 32 | } 33 | 34 | /// 35 | /// Adds a convention to the scanner that registers several types implementing the same interface. 36 | /// By default it is registered with the name of the type, but this can be overridden. 37 | /// 38 | public static AddAllConvention WithAddAllConvention(this IAssemblyScanner scanner) 39 | { 40 | return scanner.With(); 41 | } 42 | 43 | /// 44 | /// Adds a convention to the scanner that looks for a writable property of a specified type 45 | /// and configures injection for it. Mostly useful for optional dependencies like loggers etc. 46 | /// 47 | public static SetAllPropertiesConvention WithSetAllPropertiesConvention(this IAssemblyScanner scanner) 48 | { 49 | return scanner.With(); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/DecoratorExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | using Unity.Builder; 4 | using Unity.Extension; 5 | using Unity.Strategies; 6 | 7 | namespace UnityConfiguration 8 | { 9 | public class DecoratorExtension : UnityContainerExtension where T : class 10 | { 11 | private readonly Func func; 12 | 13 | public DecoratorExtension(Func func) 14 | { 15 | this.func = func; 16 | } 17 | 18 | protected override void Initialize() 19 | { 20 | Context.Strategies.Add(new DecoratorStrategy(func, Container), UnityBuildStage.PostInitialization); 21 | } 22 | } 23 | 24 | public class DecoratorStrategy : BuilderStrategy where T : class 25 | { 26 | private readonly IUnityContainer container; 27 | private readonly Func func; 28 | 29 | public DecoratorStrategy(Func func, IUnityContainer container) 30 | { 31 | this.func = func; 32 | this.container = container; 33 | } 34 | 35 | public override void PostBuildUp(ref BuilderContext context) 36 | { 37 | if (context.Existing is T obj) 38 | context.Existing = func(container, obj); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/Diagnostics/WhatDoIHaveExtensionMethod.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Text; 4 | using Unity; 5 | using Unity.Registration; 6 | 7 | namespace UnityConfiguration.Diagnostics 8 | { 9 | public static class WhatDoIHaveExtensionMethod 10 | { 11 | public static string WhatDoIHave(this IUnityContainer container) 12 | { 13 | var stringBuilder = new StringBuilder(); 14 | 15 | var registrations = container.Registrations.Select(ToRegistrationString).ToList(); 16 | registrations.Sort(); 17 | registrations.ForEach(s => stringBuilder.AppendLine(s)); 18 | 19 | return stringBuilder.ToString(); 20 | } 21 | 22 | private static string ToRegistrationString(IContainerRegistration registration) 23 | { 24 | return $"{registration.RegisteredType.FullName} - {registration.MappedToType.FullName}{Named(registration)}{AsSingleton(registration)}"; 25 | } 26 | 27 | private static string Named(IContainerRegistration registration) 28 | { 29 | return registration.Name != null ? $" named \"{registration.Name}\"" : null; 30 | } 31 | 32 | private static string AsSingleton(IContainerRegistration registration) 33 | { 34 | return registration.LifetimeManager != null ? $" with {registration.LifetimeManager.GetType().Name}" : null; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/EnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public static class EnumerableExtensions 7 | { 8 | public static void ForEach(this IEnumerable enumerable, Action action) 9 | { 10 | foreach (var obj in enumerable) 11 | { 12 | action(obj); 13 | } 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/Expression.cs: -------------------------------------------------------------------------------- 1 | using Unity; 2 | 3 | namespace UnityConfiguration 4 | { 5 | public abstract class Expression : IHideObjectMembers 6 | { 7 | internal abstract void Execute(IUnityContainer container); 8 | } 9 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/FactoryRegistrationExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | using Unity.Injection; 4 | 5 | namespace UnityConfiguration 6 | { 7 | public class FactoryRegistrationExpression : Expression 8 | { 9 | private readonly Func factoryDelegate; 10 | private string name; 11 | 12 | public FactoryRegistrationExpression(Func factoryDelegate) 13 | { 14 | this.factoryDelegate = factoryDelegate; 15 | } 16 | 17 | /// 18 | /// Specify a name for this registration. 19 | /// 20 | /// The name for this registration. 21 | public void WithName(string name) 22 | { 23 | this.name = name; 24 | } 25 | 26 | internal override void Execute(IUnityContainer container) 27 | { 28 | container.RegisterFactory(name, c => (object) factoryDelegate(c)); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/FirstInterfaceConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace UnityConfiguration 5 | { 6 | /// 7 | /// Convention for registering all types by the first interface defined for the type. 8 | /// 9 | public class FirstInterfaceConvention : IAssemblyScannerConvention 10 | { 11 | private bool ignoreBaseTypes; 12 | 13 | /// 14 | /// Determines whether or not to ignore interfaces on base types. Default false. 15 | /// 16 | public void IgnoreInterfacesOnBaseTypes() 17 | { 18 | ignoreBaseTypes = true; 19 | } 20 | 21 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 22 | { 23 | if (!type.IsConcrete() || !type.CanBeCreated()) 24 | return; 25 | 26 | var interfaceType = GetInterfaceType(type); 27 | 28 | if (interfaceType != null) 29 | registry.Register(interfaceType, type); 30 | } 31 | 32 | private Type GetInterfaceType(Type type) 33 | { 34 | var interfaces = type.GetInterfaces(); 35 | var interfaceType = interfaces.FirstOrDefault(); 36 | 37 | if (!ignoreBaseTypes || type.BaseType == null) 38 | return interfaceType; 39 | 40 | foreach (var @interface in interfaces) 41 | { 42 | if (!type.BaseType.ImplementsInterface(@interface)) 43 | return @interface; 44 | } 45 | 46 | return interfaceType; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/IAssemblyScanner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public interface IAssemblyScanner : IHideObjectMembers 7 | { 8 | /// 9 | /// Add an assembly to scan. 10 | /// 11 | /// The assembly to scan. 12 | void Assembly(Assembly assembly); 13 | 14 | /// 15 | /// Add an assembly to scan given its display name. 16 | /// 17 | /// The display name of the assembly to scan (e.g. UnityConfiguration). 18 | void Assembly(string assemblyName); 19 | 20 | /// 21 | /// Add an assembly to scan by specifying a type it contains. 22 | /// 23 | /// A type that resides in the assembly to scan. 24 | void AssemblyContaining(); 25 | 26 | /// 27 | /// Add all assemblies in the application base directory of the current app domain to the scanner. 28 | /// 29 | void AssembliesInBaseDirectory(); 30 | 31 | /// 32 | /// Add all assemblies in the application base directory of the current app domain to the scanner. 33 | /// 34 | /// A predicate used for filtering out assemblies. 35 | void AssembliesInBaseDirectory(Predicate predicate); 36 | 37 | /// 38 | /// Add all assemblies in the specified path to the scanner. 39 | /// 40 | /// The path to scan for assemblies. 41 | void AssembliesInDirectory(string path); 42 | 43 | /// 44 | /// Add all assemblies in the specified path to the scanner. 45 | /// 46 | /// The path to scan for assemblies. 47 | /// A predicate used for filtering out assemblies. 48 | void AssembliesInDirectory(string path, Predicate predicate); 49 | 50 | /// 51 | /// Include internal types in an assembly when scanning. 52 | /// 53 | void InternalTypes(); 54 | 55 | /// 56 | /// Add a convention to use when scanning. 57 | /// 58 | /// The type of the convention. 59 | /// An instance of the convention that can be used for configuring the convention. 60 | TConvention With() where TConvention : IAssemblyScannerConvention, new(); 61 | 62 | /// 63 | /// Exclude the specified type when scanning. 64 | /// 65 | /// Type of which to exclude. 66 | void ExcludeType(); 67 | 68 | /// 69 | /// Exclude types that match the specified predicate when scanning. 70 | /// 71 | /// The predicate to use for matching. 72 | void Exclude(Predicate exclude); 73 | 74 | /// 75 | /// Exclude all types in the same namespace as the specified type or its sub namespaces 76 | /// when scanning. 77 | /// 78 | /// A type in the namespace to exclude. 79 | void ExcludeNamespaceContaining(); 80 | 81 | /// 82 | /// Exclude all types in the specified namespace or its sub namespaces when scanning. 83 | /// 84 | /// The namespace to exclude. 85 | void ExcludeNamespace(string @namespace); 86 | 87 | /// 88 | /// Only include types that match the specified predicate when scanning. 89 | /// 90 | /// The predicate to use for matching. 91 | void Include(Predicate include); 92 | 93 | /// 94 | /// Only include types in the same namespace as the specified type or its sub namespaces 95 | /// when scanning. 96 | /// 97 | /// A type in the namespace to include. 98 | void IncludeNamespaceContaining(); 99 | 100 | /// 101 | /// Only include types in the specified namespace or its sub namespaces when scanning. 102 | /// 103 | /// The namespace to include. 104 | void IncludeNamespace(string @namespace); 105 | } 106 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/IAssemblyScannerConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UnityConfiguration 4 | { 5 | /// 6 | /// Interface implemented by all scanner conventions. 7 | /// Defines a method to process each type the scanner finds. 8 | /// 9 | public interface IAssemblyScannerConvention : IHideObjectMembers 10 | { 11 | /// 12 | /// Gets called for each type the finds. 13 | /// 14 | /// The type to process. 15 | /// 16 | /// The instance of the 17 | /// that can be used to register the type. 18 | /// 19 | void Process(Type type, IUnityRegistry registry); 20 | } 21 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/IHideObjectMembers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | 4 | namespace UnityConfiguration 5 | { 6 | /// 7 | /// Created by Daniel Cazzulino http://www.clariusconsulting.net/blogs/kzu/archive/2008/03/10/58301.aspx 8 | /// 9 | [EditorBrowsable(EditorBrowsableState.Never)] 10 | public interface IHideObjectMembers 11 | { 12 | [EditorBrowsable(EditorBrowsableState.Never)] 13 | bool Equals(object obj); 14 | 15 | [EditorBrowsable(EditorBrowsableState.Never)] 16 | int GetHashCode(); 17 | 18 | [EditorBrowsable(EditorBrowsableState.Never)] 19 | Type GetType(); 20 | 21 | [EditorBrowsable(EditorBrowsableState.Never)] 22 | string ToString(); 23 | } 24 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/ILifetimePolicyExpression.cs: -------------------------------------------------------------------------------- 1 | using Unity.Lifetime; 2 | 3 | namespace UnityConfiguration 4 | { 5 | public interface ILifetimePolicyExpression : IHideObjectMembers 6 | { 7 | /// 8 | /// Specify how lifetime should be managed by the container, by specifying a . 9 | /// 10 | /// The type of the to use. 11 | void Using() where T : LifetimeManager, new(); 12 | } 13 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/IRegistrationExpression.cs: -------------------------------------------------------------------------------- 1 | namespace UnityConfiguration 2 | { 3 | public interface IRegistrationExpression : ILifetimePolicyExpression 4 | { 5 | /// 6 | /// Specify arguments that will be passed to the constructor when constructing the type. 7 | /// If some of the parameters should be resolved from the container, specify its type. 8 | /// 9 | /// Value or type of the parameters. 10 | /// 11 | /// WithConstructorArguments(42, "some string", typeof(IBar)); 12 | /// 13 | ILifetimePolicyExpression WithConstructorArguments(params object[] args); 14 | } 15 | 16 | public interface INamedRegistrationExpression : IRegistrationExpression 17 | { 18 | /// 19 | /// Specify a name for this registration mapping. 20 | /// 21 | /// The name for this registration mapping. 22 | IRegistrationExpression WithName(string name); 23 | } 24 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/IUnityRegistry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using Unity; 4 | using Unity.Extension; 5 | 6 | namespace UnityConfiguration 7 | { 8 | public interface IUnityRegistry : IHideObjectMembers 9 | { 10 | /// 11 | /// Scan a set of assemblies. 12 | /// 13 | /// A nested closure for configuring the . 14 | void Scan(Action action); 15 | 16 | /// 17 | /// Import a into this. 18 | /// 19 | /// Type of the to import. 20 | void AddRegistry() where T : UnityRegistry, new(); 21 | 22 | /// 23 | /// Import a into this. 24 | /// 25 | /// An instance of the to import. 26 | void AddRegistry(UnityRegistry registry); 27 | 28 | /// 29 | /// Register a type mapping in the container. 30 | /// 31 | /// The type that will be requested. 32 | /// The type that will actually be returned. 33 | /// 34 | /// An instance of a that can be used to 35 | /// further configure the registration. 36 | /// 37 | RegistrationExpression Register(Type typeFrom, Type typeTo); 38 | 39 | /// 40 | /// Register a type mapping in the container. 41 | /// 42 | /// The type that will be requested. 43 | /// The type that will actually be returned. 44 | /// 45 | /// An instance of a that can be used to 46 | /// further configure the registration. 47 | /// 48 | RegistrationExpression Register() where TTo : TFrom; 49 | 50 | /// 51 | /// Register a type mapping in the container by using a factory delegate. 52 | /// 53 | /// The type that will be requested. 54 | /// The factory delegate that will be used to 55 | /// construct the type that will actually be returned. 56 | /// 57 | /// An instance of a that can be used to 58 | /// further configure the registration. 59 | /// 60 | FactoryRegistrationExpression Register(Func factoryDelegate); 61 | 62 | /// 63 | /// Configure a type mapping in the container. 64 | /// 65 | /// The type to configure. Can be both the requested or the returned type. 66 | /// 67 | /// An instance of a that can be used to 68 | /// further configure the registration. 69 | /// 70 | RegistrationExpression Configure(Type type); 71 | 72 | /// 73 | /// Configure a type mapping in the container. 74 | /// 75 | /// The type to configure. Can be both the requested or the returned type. 76 | /// 77 | /// An instance of a that can be used to 78 | /// further configure the registration. 79 | /// 80 | RegistrationExpression Configure(); 81 | 82 | /// 83 | /// A shortcut method to make a registered type a singleton. Mostly useful for making types registered 84 | /// by a convention. 85 | /// 86 | /// The type to make singleton. Can be both an interface or a concrete type. 87 | [Obsolete("Use Configure().AsSingleton() instead.")] 88 | [EditorBrowsable(EditorBrowsableState.Never)] 89 | void MakeSingleton(); 90 | 91 | /// 92 | /// A shortcut method to make a registered type a singleton. Mostly useful for making types registered 93 | /// by a convention. 94 | /// 95 | /// The type to make singleton. Can be both an interface or a concrete type. 96 | /// Name of the instance. 97 | [Obsolete("Use Configure().WithName(name).AsSingleton() instead.")] 98 | [EditorBrowsable(EditorBrowsableState.Never)] 99 | void MakeSingleton(string namedInstance); 100 | 101 | /// 102 | /// Specify parameters that will be passed to the constructor when constructing the type. 103 | /// If some of the parameters should be resolved from the container, specify its type. 104 | /// 105 | /// The type to configure. 106 | /// Value or type of the parameters. 107 | [Obsolete("Use Configure().WithConstructorArguments(params object[])")] 108 | [EditorBrowsable(EditorBrowsableState.Never)] 109 | void ConfigureCtorArgsFor(params object[] args); 110 | 111 | /// 112 | /// Select the constructor to be used when constructing the type by specifying 113 | /// the types of the parameters in the constructor to use. 114 | /// 115 | /// The type to configure. 116 | /// The types of the parameters or empty to specify default constructor. 117 | void SelectConstructor(params Type[] args); 118 | 119 | /// 120 | /// Add a to the container. 121 | /// 122 | /// Type of the extension to add. 123 | /// 124 | AddNewExtensionExpression AddExtension() where T : UnityContainerExtension, new(); 125 | 126 | /// 127 | /// Confgure an extension already registered in the container. 128 | /// 129 | /// Type of the extension to configure. 130 | /// A nested closure that can be used to configure the extension. 131 | /// 132 | ConfigureExtensionExpression ConfigureExtension(Action configAction) where T : IUnityContainerExtensionConfigurator; 133 | 134 | /// 135 | /// Allows for some actions to be applied to a type after it is constructed. 136 | /// 137 | /// The type to apply actions on. 138 | /// 139 | /// An instance of a that holds the actions 140 | /// that can be applied. 141 | /// 142 | PostBuildUpExpression AfterBuildingUp() where T : class; 143 | } 144 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/LifetimeExtensions.cs: -------------------------------------------------------------------------------- 1 | using Unity.Lifetime; 2 | 3 | namespace UnityConfiguration 4 | { 5 | public static class LifetimeExtensions 6 | { 7 | /// 8 | /// Indicates that only a single instance should be created, and then 9 | /// it should be re-used for all subsequent requests. 10 | /// 11 | public static void AsSingleton(this ILifetimePolicyExpression expression) 12 | { 13 | expression.Using(); 14 | } 15 | 16 | /// 17 | /// Indicates that instances should not be re-used, nor have 18 | /// their lifecycle managed by Unity. 19 | /// 20 | public static void AsTransient(this ILifetimePolicyExpression expression) 21 | { 22 | expression.Using(); 23 | } 24 | 25 | /// 26 | /// Indicates that instances should be re-used within the same thread. 27 | /// 28 | public static void AsPerThread(this ILifetimePolicyExpression expression) 29 | { 30 | expression.Using(); 31 | } 32 | 33 | /// 34 | /// Indicates that instances should be re-used within the same build up object graph. 35 | /// 36 | public static void AsPerResolve(this ILifetimePolicyExpression expression) 37 | { 38 | expression.Using(); 39 | } 40 | 41 | /// 42 | /// Indicates that Unity maintains only a weak reference to the objects it creates. 43 | /// Instances are re-used as long as the weak reference is alive. 44 | /// 45 | public static void AsExternallyControlled(this ILifetimePolicyExpression expression) 46 | { 47 | expression.Using(); 48 | } 49 | 50 | /// 51 | /// Acts like a singleton, except that in the presence of child containers, 52 | /// each child gets it's own instance of the object, instead of sharing one in the common parent. 53 | /// 54 | public static void AsHierarchicalControlled(this ILifetimePolicyExpression expression) 55 | { 56 | expression.Using(); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/NamingConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UnityConfiguration 4 | { 5 | /// 6 | /// Convention for registering all types by a naming convention. 7 | /// By default it tries to find an interface with the same name as the 8 | /// service, prefixed with an I, but this can be overridden. 9 | /// 10 | public class NamingConvention : IAssemblyScannerConvention 11 | { 12 | private Func getInterfaceName = t => $"I{t.Name}"; 13 | 14 | /// 15 | /// Specify how to resolve the name of the interface. 16 | /// 17 | /// The function to create the name of the interface. 18 | public void WithInterfaceName(Func func) 19 | { 20 | getInterfaceName = func; 21 | } 22 | 23 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 24 | { 25 | var @interface = FindInterface(type); 26 | 27 | if(@interface != null) 28 | registry.Register(@interface, type); 29 | } 30 | 31 | private Type FindInterface(Type type) 32 | { 33 | var interfaceName = getInterfaceName(type); 34 | 35 | return type.GetInterface(interfaceName); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/PostBuildUpActionExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | using Unity.Builder; 4 | using Unity.Extension; 5 | using Unity.Strategies; 6 | 7 | namespace UnityConfiguration 8 | { 9 | public class PostBuildUpActionExtension : UnityContainerExtension where T : class 10 | { 11 | private readonly Action action; 12 | 13 | public PostBuildUpActionExtension(Action action) 14 | { 15 | this.action = action; 16 | } 17 | 18 | protected override void Initialize() 19 | { 20 | Context.Strategies.Add(new PostBuildUpActionStrategy(action, Container), 21 | UnityBuildStage.PostInitialization); 22 | } 23 | } 24 | 25 | public class PostBuildUpActionStrategy : BuilderStrategy where T : class 26 | { 27 | private readonly Action action; 28 | private readonly IUnityContainer container; 29 | 30 | public PostBuildUpActionStrategy(Action action, IUnityContainer container) 31 | { 32 | this.action = action; 33 | this.container = container; 34 | } 35 | 36 | public override void PostBuildUp(ref BuilderContext context) 37 | { 38 | if (context.Existing is T obj) 39 | action(container, obj); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/PostBuildUpExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public class PostBuildUpExpression : Expression where T : class 7 | { 8 | private Action action; 9 | private Func decoratorFunc; 10 | 11 | /// 12 | /// Call a method or a property setter on the instance after it is constructed. 13 | /// 14 | /// 15 | public void Call(Action action) 16 | { 17 | this.action = action; 18 | } 19 | 20 | /// 21 | /// Decorate the instance with another class after it is constructed. 22 | /// 23 | /// The function used to construct the class to decorate with. 24 | public void DecorateWith(Func func) 25 | { 26 | decoratorFunc = func; 27 | } 28 | 29 | internal override void Execute(IUnityContainer container) 30 | { 31 | if (decoratorFunc != null) 32 | container.AddExtension(new DecoratorExtension(decoratorFunc)); 33 | 34 | if (action != null) 35 | container.AddExtension(new PostBuildUpActionExtension(action)); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | [assembly: AssemblyTitle("UnityConfiguration")] 4 | [assembly: AssemblyDescription("Convention based configuration API for the Microsoft Unity IoC container. With just a few lines of code, you can now registere all your classes in the entire solution. If the built-in conventions doesn't fit your needs, it is very easy to extend with your own.")] 5 | [assembly: AssemblyCompany("UnityConfiguration")] 6 | [assembly: AssemblyProduct("UnityConfiguration")] 7 | [assembly: AssemblyCopyright("Copyright (C) 2011 Thomas Pedersen")] 8 | [assembly: ComVisible(false)] 9 | [assembly: AssemblyVersion("1.4.1")] 10 | 11 | -------------------------------------------------------------------------------- /src/UnityConfiguration/RegistrationExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | using Unity.Injection; 4 | using Unity.Lifetime; 5 | 6 | namespace UnityConfiguration 7 | { 8 | public class RegistrationExpression : Expression, INamedRegistrationExpression 9 | { 10 | private readonly Type typeFrom; 11 | private Type typeTo; 12 | private InjectionMember[] injectionMembers; 13 | private Func typeLifetimeManagerFunc; 14 | private string name; 15 | 16 | public RegistrationExpression(Type typeFrom, Type typeTo) 17 | { 18 | this.typeFrom = typeFrom; 19 | this.typeTo = typeTo; 20 | typeLifetimeManagerFunc = () => new TransientLifetimeManager(); 21 | injectionMembers = new InjectionMember[0]; 22 | } 23 | 24 | public IRegistrationExpression WithName(string name) 25 | { 26 | this.name = name; 27 | return this; 28 | } 29 | 30 | public ILifetimePolicyExpression WithConstructorArguments(params object[] args) 31 | { 32 | WithInjectionMembers(new InjectionConstructor(args)); 33 | return this; 34 | } 35 | 36 | public void Using() where T : LifetimeManager, new() 37 | { 38 | typeLifetimeManagerFunc = () => (ITypeLifetimeManager) new T(); 39 | } 40 | 41 | internal void WithInjectionMembers(params InjectionMember[] injectionMember) 42 | { 43 | injectionMembers = injectionMember; 44 | } 45 | 46 | internal override void Execute(IUnityContainer container) 47 | { 48 | if (typeFrom == null && !typeTo.IsConcrete()) 49 | { 50 | container.Registrations.ForEach(c => 51 | { 52 | if (c.RegisteredType == typeTo) 53 | container.RegisterType(c.MappedToType, c.Name, typeLifetimeManagerFunc(), injectionMembers); 54 | 55 | }); 56 | } 57 | else 58 | { 59 | container.RegisterType(typeFrom, typeTo, name, typeLifetimeManagerFunc(), injectionMembers); 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/ScanForRegistriesConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UnityConfiguration 4 | { 5 | /// 6 | /// Convention that looks for and imports it 7 | /// into the current . 8 | /// 9 | public class ScanForRegistriesConvention : IAssemblyScannerConvention 10 | { 11 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 12 | { 13 | if (type.CanBeCastTo(typeof(UnityRegistry)) && type.CanBeCreated()) 14 | registry.AddRegistry((UnityRegistry) type.GetConstructor(Type.EmptyTypes).Invoke(null)); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/SetAllPropertiesConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using Unity.Injection; 6 | 7 | namespace UnityConfiguration 8 | { 9 | /// 10 | /// Convention that looks for a writable property of a specified type 11 | /// and configures injection for it. Mostly useful for optional dependencies 12 | /// like loggers etc. 13 | /// 14 | public class SetAllPropertiesConvention : IAssemblyScannerConvention 15 | { 16 | private Type interfaceType; 17 | 18 | /// 19 | /// Specify the type of the property to inject. 20 | /// 21 | /// The type of the property to inject. 22 | public void OfType() 23 | { 24 | interfaceType = typeof (T); 25 | } 26 | 27 | void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry) 28 | { 29 | var properties = 30 | type.GetProperties().Where(p => p.CanWrite && p.PropertyType == interfaceType); 31 | 32 | foreach (var property in properties) 33 | { 34 | registry.Register(null, type).WithInjectionMembers(new InjectionProperty(property.Name)); 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/TypeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public static class TypeExtensions 7 | { 8 | public static bool IsConcrete(this Type type) 9 | { 10 | return !type.IsAbstract && !type.IsInterface; 11 | } 12 | 13 | public static bool CanBeCreated(this Type type) 14 | { 15 | return type.IsConcrete() && type.GetConstructors().Length > 0; 16 | } 17 | 18 | public static bool CanBeCastTo(this Type typeFrom, Type typeTo) 19 | { 20 | if (typeFrom == null) 21 | return false; 22 | 23 | if (typeFrom.IsInterface || typeFrom.IsAbstract) 24 | return false; 25 | 26 | if (IsOpenGeneric(typeFrom)) 27 | return false; 28 | 29 | return typeTo.IsAssignableFrom(typeFrom); 30 | } 31 | 32 | public static bool IsOpenGeneric(this Type type) 33 | { 34 | return type.IsGenericTypeDefinition || type.ContainsGenericParameters; 35 | } 36 | 37 | public static bool IsInNamespace(this Type type, string @namespace) 38 | { 39 | if (type.Namespace != null) 40 | return type.Namespace.StartsWith(@namespace); 41 | 42 | return false; 43 | } 44 | 45 | public static bool ImplementsInterface(this Type type, Type implements) 46 | { 47 | return implements.IsInterface && type.GetInterface(implements.Name) != null; 48 | } 49 | 50 | public static bool ImplementsInterfaceTemplate(this Type type, Type templateType) 51 | { 52 | if (!type.IsConcrete()) 53 | return false; 54 | 55 | return type.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/UnityConfiguration.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {D5FD47DA-D607-46DC-895E-2C823905D147} 9 | Library 10 | Properties 11 | UnityConfiguration 12 | UnityConfiguration 13 | v4.8 14 | 512 15 | 16 | ..\..\src\ 17 | true 18 | 19 | 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | bin\Debug\UnityConfiguration.xml 28 | 1591 29 | true 30 | false 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | 1591 40 | bin\Release\UnityConfiguration.xml 41 | true 42 | false 43 | 44 | 45 | true 46 | 47 | 48 | UnityConfiguration.snk 49 | 50 | 51 | 52 | ..\packages\CommonServiceLocator.2.0.5\lib\net48\CommonServiceLocator.dll 53 | 54 | 55 | 56 | 57 | ..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll 58 | 59 | 60 | ..\packages\Unity.5.11.7\lib\net48\Unity.Abstractions.dll 61 | 62 | 63 | ..\packages\Unity.5.11.7\lib\net48\Unity.Container.dll 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 112 | -------------------------------------------------------------------------------- /src/UnityConfiguration/UnityConfiguration.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedersen/UnityConfiguration/a91e69fbe7cfdd3c72941303e38f728577f7502d/src/UnityConfiguration/UnityConfiguration.snk -------------------------------------------------------------------------------- /src/UnityConfiguration/UnityExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Unity; 3 | 4 | namespace UnityConfiguration 5 | { 6 | public static class UnityExtension 7 | { 8 | /// 9 | /// Configure an instance of an . 10 | /// 11 | /// The container to configure. 12 | /// An expression used for configuring the container. 13 | public static IUnityContainer Configure(this IUnityContainer container, Action expression) 14 | { 15 | var registry = new UnityRegistry(); 16 | 17 | expression(registry); 18 | 19 | registry.Configure(container); 20 | 21 | return container; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/UnityRegistry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using Unity; 5 | using Unity.Extension; 6 | 7 | namespace UnityConfiguration 8 | { 9 | public class UnityRegistry : IUnityRegistry 10 | { 11 | private readonly List configurations = new List(); 12 | private readonly List extensions = new List(); 13 | private readonly List registrations = new List(); 14 | private readonly List registries = new List(); 15 | 16 | public void Scan(Action action) 17 | { 18 | var assemblyScanner = new AssemblyScanner(); 19 | 20 | action(assemblyScanner); 21 | assemblyScanner.Exclude(t => t == GetType()); 22 | assemblyScanner.Scan(this); 23 | } 24 | 25 | public void AddRegistry() where T : UnityRegistry, new() 26 | { 27 | AddRegistry(new T()); 28 | } 29 | 30 | public void AddRegistry(UnityRegistry registry) 31 | { 32 | registries.Add(registry); 33 | } 34 | 35 | public RegistrationExpression Register(Type typeFrom, Type typeTo) 36 | { 37 | var registrationExpression = new RegistrationExpression(typeFrom, typeTo); 38 | registrations.Add(registrationExpression); 39 | return registrationExpression; 40 | } 41 | 42 | public RegistrationExpression Register() where TTo : TFrom 43 | { 44 | return Register(typeof (TFrom), typeof (TTo)); 45 | } 46 | 47 | public FactoryRegistrationExpression Register(Func factoryDelegate) 48 | { 49 | var factoryRegistrationExpression = new FactoryRegistrationExpression(factoryDelegate); 50 | registrations.Add(factoryRegistrationExpression); 51 | 52 | return factoryRegistrationExpression; 53 | } 54 | 55 | public RegistrationExpression Configure(Type type) 56 | { 57 | return Register(null, type); 58 | } 59 | 60 | public RegistrationExpression Configure() 61 | { 62 | return Configure(typeof (T)); 63 | } 64 | 65 | [Obsolete("Use Configure().AsSingleton() instead.")] 66 | [EditorBrowsable(EditorBrowsableState.Never)] 67 | public void MakeSingleton() 68 | { 69 | Configure().AsSingleton(); 70 | } 71 | 72 | [Obsolete("Use Configure().WithName(name).AsSingleton() instead.")] 73 | [EditorBrowsable(EditorBrowsableState.Never)] 74 | public void MakeSingleton(string namedInstance) 75 | { 76 | Configure().WithName(namedInstance).AsSingleton(); 77 | } 78 | 79 | [Obsolete("Use Configure().WithConstructorArguments(params object[])")] 80 | [EditorBrowsable(EditorBrowsableState.Never)] 81 | public void ConfigureCtorArgsFor(params object[] args) 82 | { 83 | Configure().WithConstructorArguments(args); 84 | } 85 | 86 | public void SelectConstructor(params Type[] args) 87 | { 88 | Configure().WithConstructorArguments(args); 89 | } 90 | 91 | public AddNewExtensionExpression AddExtension() where T : UnityContainerExtension, new() 92 | { 93 | var extensionExpression = new AddNewExtensionExpression(); 94 | extensions.Add(extensionExpression); 95 | return extensionExpression; 96 | } 97 | 98 | public ConfigureExtensionExpression ConfigureExtension(Action configAction) where T : IUnityContainerExtensionConfigurator 99 | { 100 | var configureExtensionExpression = new ConfigureExtensionExpression(configAction); 101 | configurations.Add(configureExtensionExpression); 102 | 103 | return configureExtensionExpression; 104 | } 105 | 106 | public PostBuildUpExpression AfterBuildingUp() where T : class 107 | { 108 | var afterBuildUpExpression = new PostBuildUpExpression(); 109 | extensions.Add(afterBuildUpExpression); 110 | return afterBuildUpExpression; 111 | } 112 | 113 | public virtual void Configure(IUnityContainer container) 114 | { 115 | registries.ForEach(x => x.Configure(container)); 116 | extensions.ForEach(expression => expression.Execute(container)); 117 | registrations.ForEach(expression => expression.Execute(container)); 118 | configurations.ForEach(expression => expression.Execute(container)); 119 | } 120 | } 121 | } -------------------------------------------------------------------------------- /src/UnityConfiguration/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/UnityConfiguration/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | --------------------------------------------------------------------------------