From 268b4c609f438c6a066f1b1718499e9a75e422c7 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 08:59:10 +0100 Subject: [PATCH 1/7] Implement IncludeNone for assembly specification #660 Original issue: Bifrost#5 --- Source/Bifrost/Bifrost.csproj | 4 +++ .../AssembliesConfigurationBuilder.cs | 14 ++++++++++- .../Assemblies/AssembliesStartingWith.cs | 21 ++++++++++++++++ .../AssemblyRuleBuilderExtensions.cs | 13 +++++++++- .../Configuration/Assemblies/IncludeNone.cs | 24 ++++++++++++++++++ .../Assemblies/IncludeNoneExtensions.cs | 25 +++++++++++++++++++ .../Assemblies/IncludeNoneRule.cs | 20 +++++++++++++++ Source/Bifrost/Execution/AssemblyProvider.cs | 12 +-------- 8 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 Source/Bifrost/Configuration/Assemblies/AssembliesStartingWith.cs create mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeNone.cs create mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs create mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeNoneRule.cs diff --git a/Source/Bifrost/Bifrost.csproj b/Source/Bifrost/Bifrost.csproj index 535cb4051..65eeb83a7 100644 --- a/Source/Bifrost/Bifrost.csproj +++ b/Source/Bifrost/Bifrost.csproj @@ -54,6 +54,10 @@ Properties\CommonAssemblyInfo.cs + + + + diff --git a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs b/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs index 03a7ff7fb..9128d010e 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs @@ -14,7 +14,6 @@ public class AssembliesConfigurationBuilder /// public IAssemblyRuleBuilder RuleBuilder { get; private set; } - /// /// Include all assemblies with possible exceptions /// @@ -27,5 +26,18 @@ public IncludeAll IncludeAll() RuleBuilder = includeAll; return includeAll; } + + /// + /// Include no assemblies. + /// + /// + /// Returns the configuration object for the rule. + /// + public IncludeNone IncludeNone() + { + var includeNone = new IncludeNone(); + RuleBuilder = includeNone; + return includeNone; + } } } diff --git a/Source/Bifrost/Configuration/Assemblies/AssembliesStartingWith.cs b/Source/Bifrost/Configuration/Assemblies/AssembliesStartingWith.cs new file mode 100644 index 000000000..98faba2d3 --- /dev/null +++ b/Source/Bifrost/Configuration/Assemblies/AssembliesStartingWith.cs @@ -0,0 +1,21 @@ +using System.Linq; +using Bifrost.Specifications; + +namespace Bifrost.Configuration.Assemblies +{ + /// + /// Rule representing an addition to , + /// that includes assemblies in files starting with any of the given names. + /// + public class AssembliesStartingWith : Specification + { + /// + /// Initializes an instance of . + /// + /// List of assembly name prefixes. + public AssembliesStartingWith(params string[] names) + { + Predicate = a => names.Any(a.StartsWith); + } + } +} diff --git a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs b/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs index 941588803..c1bc987e7 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs @@ -11,7 +11,6 @@ namespace Bifrost.Configuration.Assemblies /// public static class AssemblyRuleBuilderExtensions { - /// /// Excludes specified assemblies /// @@ -23,5 +22,17 @@ public static IAssemblyRuleBuilder ExcludeAssembliesStartingWith(this IAssemblyR assemblyBuilder.Specification = assemblyBuilder.Specification.And(new ExceptAssembliesStartingWith(names)); return assemblyBuilder; } + + /// + /// Includes specified assemblies. + /// + /// to build upon. + /// Names that assemblies should be starting with. + /// Chained + public static IAssemblyRuleBuilder IncludeAssembliesStartingWith(this IAssemblyRuleBuilder assemblyBuilder, params string[] names) + { + assemblyBuilder.Specification = assemblyBuilder.Specification.And(new AssembliesStartingWith(names)); + return assemblyBuilder; + } } } diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs b/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs new file mode 100644 index 000000000..7f4ebbfc1 --- /dev/null +++ b/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs @@ -0,0 +1,24 @@ +using Bifrost.Specifications; + +namespace Bifrost.Configuration.Assemblies +{ + /// + /// Represents the builder for building the and + /// possible exceptions. + /// + public class IncludeNone : IAssemblyRuleBuilder + { + /// + /// Initializes an instance of . + /// + public IncludeNone() + { + Specification = new IncludeNoneRule(); + } + + /// + /// Gets the . + /// + public Specification Specification { get; set; } + } +} diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs b/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs new file mode 100644 index 000000000..b2502ad02 --- /dev/null +++ b/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs @@ -0,0 +1,25 @@ +using Bifrost.Extensions; +using Bifrost.Specifications; + +namespace Bifrost.Configuration.Assemblies +{ + /// + /// Extensions for . + /// + public static class IncludeNoneExtensions + { + /// + /// Include all assemblies that has a name starting with the given name + /// + /// Configuration object + /// Names of assemblies to exclude + /// Chain of configuration object + public static IncludeNone ExceptAssembliesStartingWith(this IncludeNone includeNone, params string[] assemblyNames) + { + var specification = includeNone.Specification; + assemblyNames.ForEach(assemblyName => specification = specification.Or(new AssembliesStartingWith(assemblyName))); + includeNone.Specification = specification; + return includeNone; + } + } +} diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeNoneRule.cs b/Source/Bifrost/Configuration/Assemblies/IncludeNoneRule.cs new file mode 100644 index 000000000..ab2f4d123 --- /dev/null +++ b/Source/Bifrost/Configuration/Assemblies/IncludeNoneRule.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using Bifrost.Specifications; + +namespace Bifrost.Configuration.Assemblies +{ + /// + /// Represents a rule specific to assemblies + /// that matches no assemblies. + /// + public class IncludeNoneRule : Specification + { + /// + /// Initializes an instance of . + /// + public IncludeNoneRule() + { + Predicate = a => false; + } + } +} diff --git a/Source/Bifrost/Execution/AssemblyProvider.cs b/Source/Bifrost/Execution/AssemblyProvider.cs index a08ec21c7..ff0604de8 100644 --- a/Source/Bifrost/Execution/AssemblyProvider.cs +++ b/Source/Bifrost/Execution/AssemblyProvider.cs @@ -106,22 +106,12 @@ void AddAssembly(Assembly assembly) !_assemblyUtility.IsAssemblyDynamic(assembly)) { _assemblies.Add(assembly); - - if (assembly.FullName.Contains("Web")) - { - var i = 0; - i++; - } _contractToImplementorsMap.Feed(assembly.GetTypes()); SpecifyRules(assembly); + // TODO: This is not needed when assemblies become opt-in. ReapplyFilter(); } } } - - bool Matches(AssemblyName a, AssemblyName b) - { - return a.ToString() == b.ToString(); - } } } From 935a488cbc1af68da38b56e1c0947c7b6e4ae313 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:10:23 +0100 Subject: [PATCH 2/7] Use Assembly opt-in by default. Breaking change! #660 Clients must now implement ICanSpecifyAssemblies to include their assemblies in Bifrost. Original issue Bifrost#5 --- Source/Bifrost.Autofac/AssemblySpecifier.cs | 38 ------------------- Source/Bifrost.Autofac/Bifrost.Autofac.csproj | 1 - Source/Bifrost.Client/AssemblySpecifier.cs | 28 -------------- Source/Bifrost.Client/Bifrost.Client.csproj | 3 +- .../AssemblySpecifier.cs | 24 ------------ .../Bifrost.EntityFramework.csproj | 1 - .../AssemblySpecifier.cs | 24 ------------ .../Bifrost.FluentValidation.csproj | 1 - Source/Bifrost.JSON/AssemblySpecifier.cs | 24 ------------ Source/Bifrost.JSON/Bifrost.JSON.csproj | 1 - Source/Bifrost.MongoDb/AssemblySpecifier.cs | 24 ------------ Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj | 1 - .../Bifrost.NHibernate/AssemblySpecifier.cs | 26 ------------- .../Bifrost.NHibernate.csproj | 1 - Source/Bifrost.Ninject/AssemblySpecifier.cs | 24 ------------ Source/Bifrost.Ninject/Bifrost.Ninject.csproj | 1 - Source/Bifrost.QuickStart.WPF/App.xaml.cs | 3 +- Source/Bifrost.RavenDB/AssemblySpecifier.cs | 24 ------------ Source/Bifrost.RavenDB/Bifrost.RavenDB.csproj | 1 - .../AssemblySpecifier.cs | 24 ------------ .../Bifrost.SimpleInjector.csproj | 1 - .../Bifrost.StructureMap/AssemblySpecifier.cs | 24 ------------ .../Bifrost.StructureMap.csproj | 1 - Source/Bifrost.Web/AssemblySpecifier.cs | 25 ------------ Source/Bifrost.Web/Bifrost.Web.csproj | 1 - Source/Bifrost/AssemblySpecifier.cs | 6 +-- Source/Bifrost/Bifrost.csproj | 5 --- .../AssembliesConfigurationBuilder.cs | 13 ------- .../AssemblyRuleBuilderExtensions.cs | 12 ------ .../ExceptAssembliesStartingWith.cs | 25 ------------ .../Configuration/Assemblies/IncludeAll.cs | 28 -------------- .../Assemblies/IncludeAllExtensions.cs | 29 -------------- .../Assemblies/IncludeAllRule.cs | 23 ----------- Source/Bifrost/Configuration/Configure.cs | 2 +- Source/Bifrost/Execution/AssemblyProvider.cs | 8 ---- .../Execution/DefaultAssemblySpecifier.cs | 25 ------------ 36 files changed, 4 insertions(+), 498 deletions(-) delete mode 100644 Source/Bifrost.Autofac/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.Client/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.EntityFramework/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.FluentValidation/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.JSON/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.MongoDb/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.NHibernate/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.Ninject/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.RavenDB/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.SimpleInjector/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.StructureMap/AssemblySpecifier.cs delete mode 100644 Source/Bifrost.Web/AssemblySpecifier.cs delete mode 100644 Source/Bifrost/Configuration/Assemblies/ExceptAssembliesStartingWith.cs delete mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeAll.cs delete mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeAllExtensions.cs delete mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeAllRule.cs delete mode 100644 Source/Bifrost/Execution/DefaultAssemblySpecifier.cs diff --git a/Source/Bifrost.Autofac/AssemblySpecifier.cs b/Source/Bifrost.Autofac/AssemblySpecifier.cs deleted file mode 100644 index 47854821d..000000000 --- a/Source/Bifrost.Autofac/AssemblySpecifier.cs +++ /dev/null @@ -1,38 +0,0 @@ -#region License -// -// Copyright (c) 2008-2015, Dolittle (http://www.dolittle.com) -// -// Licensed under the MIT License (http://opensource.org/licenses/MIT) -// -// You may not use this file except in compliance with the License. -// You may obtain a copy of the license at -// -// http://github.com/dolittle/Bifrost/blob/master/MIT-LICENSE.txt -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.Autofac -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Autofac" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.Autofac/Bifrost.Autofac.csproj b/Source/Bifrost.Autofac/Bifrost.Autofac.csproj index 478c635bb..ed37ea0c5 100644 --- a/Source/Bifrost.Autofac/Bifrost.Autofac.csproj +++ b/Source/Bifrost.Autofac/Bifrost.Autofac.csproj @@ -55,7 +55,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.Client/AssemblySpecifier.cs b/Source/Bifrost.Client/AssemblySpecifier.cs deleted file mode 100644 index d7c279797..000000000 --- a/Source/Bifrost.Client/AssemblySpecifier.cs +++ /dev/null @@ -1,28 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Presentation", - "vshost", - "WindowsBase", - "UIAutomation", - "Castle.Core" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.Client/Bifrost.Client.csproj b/Source/Bifrost.Client/Bifrost.Client.csproj index 363952e69..3d7528805 100644 --- a/Source/Bifrost.Client/Bifrost.Client.csproj +++ b/Source/Bifrost.Client/Bifrost.Client.csproj @@ -15,7 +15,7 @@ true - + true full false @@ -62,7 +62,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.EntityFramework/AssemblySpecifier.cs b/Source/Bifrost.EntityFramework/AssemblySpecifier.cs deleted file mode 100644 index 3929cd04b..000000000 --- a/Source/Bifrost.EntityFramework/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.EntityFramework -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "EntityFramework" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.EntityFramework/Bifrost.EntityFramework.csproj b/Source/Bifrost.EntityFramework/Bifrost.EntityFramework.csproj index 8336c15e2..87004be52 100644 --- a/Source/Bifrost.EntityFramework/Bifrost.EntityFramework.csproj +++ b/Source/Bifrost.EntityFramework/Bifrost.EntityFramework.csproj @@ -49,7 +49,6 @@ - diff --git a/Source/Bifrost.FluentValidation/AssemblySpecifier.cs b/Source/Bifrost.FluentValidation/AssemblySpecifier.cs deleted file mode 100644 index 42347834d..000000000 --- a/Source/Bifrost.FluentValidation/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.FluentValidation -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "FluentValidation" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.FluentValidation/Bifrost.FluentValidation.csproj b/Source/Bifrost.FluentValidation/Bifrost.FluentValidation.csproj index 759166e80..e42c43401 100644 --- a/Source/Bifrost.FluentValidation/Bifrost.FluentValidation.csproj +++ b/Source/Bifrost.FluentValidation/Bifrost.FluentValidation.csproj @@ -49,7 +49,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.JSON/AssemblySpecifier.cs b/Source/Bifrost.JSON/AssemblySpecifier.cs deleted file mode 100644 index 191e260a2..000000000 --- a/Source/Bifrost.JSON/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.JSON -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Newtonsoft.Json" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.JSON/Bifrost.JSON.csproj b/Source/Bifrost.JSON/Bifrost.JSON.csproj index 0105d2361..67798e8c6 100644 --- a/Source/Bifrost.JSON/Bifrost.JSON.csproj +++ b/Source/Bifrost.JSON/Bifrost.JSON.csproj @@ -48,7 +48,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.MongoDb/AssemblySpecifier.cs b/Source/Bifrost.MongoDb/AssemblySpecifier.cs deleted file mode 100644 index 22a959f13..000000000 --- a/Source/Bifrost.MongoDb/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.MongoDB -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "MongoDB" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj b/Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj index 6e19a63fb..0a3904bfd 100644 --- a/Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj +++ b/Source/Bifrost.MongoDb/Bifrost.MongoDB.csproj @@ -52,7 +52,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.NHibernate/AssemblySpecifier.cs b/Source/Bifrost.NHibernate/AssemblySpecifier.cs deleted file mode 100644 index 175429fdc..000000000 --- a/Source/Bifrost.NHibernate/AssemblySpecifier.cs +++ /dev/null @@ -1,26 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.NHibernate -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "FluentNHibernate", - "Iesi.Collections", - "NHibernate" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.NHibernate/Bifrost.NHibernate.csproj b/Source/Bifrost.NHibernate/Bifrost.NHibernate.csproj index b30e2b41a..d84896aa2 100644 --- a/Source/Bifrost.NHibernate/Bifrost.NHibernate.csproj +++ b/Source/Bifrost.NHibernate/Bifrost.NHibernate.csproj @@ -52,7 +52,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.Ninject/AssemblySpecifier.cs b/Source/Bifrost.Ninject/AssemblySpecifier.cs deleted file mode 100644 index c409261e0..000000000 --- a/Source/Bifrost.Ninject/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.Ninject -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Ninject" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.Ninject/Bifrost.Ninject.csproj b/Source/Bifrost.Ninject/Bifrost.Ninject.csproj index 1b1fc9a68..83cc29f06 100644 --- a/Source/Bifrost.Ninject/Bifrost.Ninject.csproj +++ b/Source/Bifrost.Ninject/Bifrost.Ninject.csproj @@ -52,7 +52,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.QuickStart.WPF/App.xaml.cs b/Source/Bifrost.QuickStart.WPF/App.xaml.cs index 9f5119646..dce669315 100644 --- a/Source/Bifrost.QuickStart.WPF/App.xaml.cs +++ b/Source/Bifrost.QuickStart.WPF/App.xaml.cs @@ -1,6 +1,5 @@ using System.Windows; using Bifrost.Configuration; -using Bifrost.Configuration.Assemblies; namespace Bifrost.QuickStart.WPF { @@ -11,7 +10,7 @@ public partial class App : Application { static App() { - Configure.DiscoverAndConfigure(a => a.IncludeAll()); //.ExceptAssembliesStartingWith("System","Microsoft","mscor","FluentValidation","Ninject")); + Configure.DiscoverAndConfigure(); } } } diff --git a/Source/Bifrost.RavenDB/AssemblySpecifier.cs b/Source/Bifrost.RavenDB/AssemblySpecifier.cs deleted file mode 100644 index d04e08a57..000000000 --- a/Source/Bifrost.RavenDB/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.RavenDB -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Raven" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.RavenDB/Bifrost.RavenDB.csproj b/Source/Bifrost.RavenDB/Bifrost.RavenDB.csproj index 969fc116d..a9a5f3293 100644 --- a/Source/Bifrost.RavenDB/Bifrost.RavenDB.csproj +++ b/Source/Bifrost.RavenDB/Bifrost.RavenDB.csproj @@ -51,7 +51,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.SimpleInjector/AssemblySpecifier.cs b/Source/Bifrost.SimpleInjector/AssemblySpecifier.cs deleted file mode 100644 index 8566eec2b..000000000 --- a/Source/Bifrost.SimpleInjector/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.SimpleInjector -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "SimpleInjector" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.SimpleInjector/Bifrost.SimpleInjector.csproj b/Source/Bifrost.SimpleInjector/Bifrost.SimpleInjector.csproj index 30e434648..d4bb1ea68 100644 --- a/Source/Bifrost.SimpleInjector/Bifrost.SimpleInjector.csproj +++ b/Source/Bifrost.SimpleInjector/Bifrost.SimpleInjector.csproj @@ -49,7 +49,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.StructureMap/AssemblySpecifier.cs b/Source/Bifrost.StructureMap/AssemblySpecifier.cs deleted file mode 100644 index 6d5efe69b..000000000 --- a/Source/Bifrost.StructureMap/AssemblySpecifier.cs +++ /dev/null @@ -1,24 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.StructureMap -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "StructureMap" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.StructureMap/Bifrost.StructureMap.csproj b/Source/Bifrost.StructureMap/Bifrost.StructureMap.csproj index 93286f4d3..c922f7627 100644 --- a/Source/Bifrost.StructureMap/Bifrost.StructureMap.csproj +++ b/Source/Bifrost.StructureMap/Bifrost.StructureMap.csproj @@ -45,7 +45,6 @@ Properties\CommonAssemblyInfo.cs - diff --git a/Source/Bifrost.Web/AssemblySpecifier.cs b/Source/Bifrost.Web/AssemblySpecifier.cs deleted file mode 100644 index 41d0ad976..000000000 --- a/Source/Bifrost.Web/AssemblySpecifier.cs +++ /dev/null @@ -1,25 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; -using Bifrost.Execution; - -namespace Bifrost.Web -{ - /// - /// Reperesents an assembly specifier for client aspects - /// - public class AssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "Owin", - "WebActivator" - ); - } -#pragma warning disable 1591 // Xml Comments - } -} diff --git a/Source/Bifrost.Web/Bifrost.Web.csproj b/Source/Bifrost.Web/Bifrost.Web.csproj index 85c4eee5b..f14a83a53 100644 --- a/Source/Bifrost.Web/Bifrost.Web.csproj +++ b/Source/Bifrost.Web/Bifrost.Web.csproj @@ -55,7 +55,6 @@ - diff --git a/Source/Bifrost/AssemblySpecifier.cs b/Source/Bifrost/AssemblySpecifier.cs index 604345fa2..1edddec65 100644 --- a/Source/Bifrost/AssemblySpecifier.cs +++ b/Source/Bifrost/AssemblySpecifier.cs @@ -15,11 +15,7 @@ public class AssemblySpecifier : ICanSpecifyAssemblies #pragma warning disable 1591 // Xml Comments public void Specify(IAssemblyRuleBuilder builder) { - builder.ExcludeAssembliesStartingWith( - "System", - "mscorlib", - "Microsoft" - ); + builder.IncludeAssembliesStartingWith("Bifrost"); } #pragma warning disable 1591 // Xml Comments } diff --git a/Source/Bifrost/Bifrost.csproj b/Source/Bifrost/Bifrost.csproj index 65eeb83a7..04816f950 100644 --- a/Source/Bifrost/Bifrost.csproj +++ b/Source/Bifrost/Bifrost.csproj @@ -132,11 +132,7 @@ - - - - @@ -148,7 +144,6 @@ - diff --git a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs b/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs index 9128d010e..7ce49fdf0 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs @@ -14,19 +14,6 @@ public class AssembliesConfigurationBuilder /// public IAssemblyRuleBuilder RuleBuilder { get; private set; } - /// - /// Include all assemblies with possible exceptions - /// - /// - /// Returns the configuration object for the rule - /// - public IncludeAll IncludeAll() - { - var includeAll = new IncludeAll(); - RuleBuilder = includeAll; - return includeAll; - } - /// /// Include no assemblies. /// diff --git a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs b/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs index c1bc987e7..3550e4a82 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs @@ -11,18 +11,6 @@ namespace Bifrost.Configuration.Assemblies /// public static class AssemblyRuleBuilderExtensions { - /// - /// Excludes specified assemblies - /// - /// to build upon - /// Names that assemblies should not be starting with - /// Chained - public static IAssemblyRuleBuilder ExcludeAssembliesStartingWith(this IAssemblyRuleBuilder assemblyBuilder, params string[] names) - { - assemblyBuilder.Specification = assemblyBuilder.Specification.And(new ExceptAssembliesStartingWith(names)); - return assemblyBuilder; - } - /// /// Includes specified assemblies. /// diff --git a/Source/Bifrost/Configuration/Assemblies/ExceptAssembliesStartingWith.cs b/Source/Bifrost/Configuration/Assemblies/ExceptAssembliesStartingWith.cs deleted file mode 100644 index c2fba0ded..000000000 --- a/Source/Bifrost/Configuration/Assemblies/ExceptAssembliesStartingWith.cs +++ /dev/null @@ -1,25 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using System.Linq; -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Rule representing an exception for , - /// excluding assembies starting with - /// - public class ExceptAssembliesStartingWith : Specification - { - /// - /// Initializes an instance of - /// - /// - public ExceptAssembliesStartingWith(params string[] names) - { - Predicate = a => !names.Any(n => a.StartsWith(n)); - } - } -} diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeAll.cs b/Source/Bifrost/Configuration/Assemblies/IncludeAll.cs deleted file mode 100644 index 73bdbb5b7..000000000 --- a/Source/Bifrost/Configuration/Assemblies/IncludeAll.cs +++ /dev/null @@ -1,28 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Represents the builder for building the and - /// possible exceptions - /// - public class IncludeAll : IAssemblyRuleBuilder - { - /// - /// Initializes an instance of - /// - public IncludeAll() - { - Specification = new IncludeAllRule(); - } - - /// - /// Gets the - /// - public Specification Specification { get; set; } - } -} diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeAllExtensions.cs b/Source/Bifrost/Configuration/Assemblies/IncludeAllExtensions.cs deleted file mode 100644 index eee7d8fad..000000000 --- a/Source/Bifrost/Configuration/Assemblies/IncludeAllExtensions.cs +++ /dev/null @@ -1,29 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Extensions; -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Extensions for - /// - public static class IncludeAllExtensions - { - /// - /// Include all except for assemblies that has a name starting with the given name - /// - /// Configuration object - /// Names of assemblies to exclude - /// Chain of configuration object - public static IncludeAll ExceptAssembliesStartingWith(this IncludeAll includeAll, params string[] assemblyNames) - { - var specification = includeAll.Specification; - assemblyNames.ForEach(assemblyName => specification = specification.And(new ExceptAssembliesStartingWith(assemblyName))); - includeAll.Specification = specification; - return includeAll; - } - } -} diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeAllRule.cs b/Source/Bifrost/Configuration/Assemblies/IncludeAllRule.cs deleted file mode 100644 index 83329b0bb..000000000 --- a/Source/Bifrost/Configuration/Assemblies/IncludeAllRule.cs +++ /dev/null @@ -1,23 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Represents a rule specific to assemblies - /// and used for the - /// - public class IncludeAllRule : Specification - { - /// - /// Initializes an instance of - /// - public IncludeAllRule() - { - Predicate = a => true; - } - } -} diff --git a/Source/Bifrost/Configuration/Configure.cs b/Source/Bifrost/Configuration/Configure.cs index ebe92fb91..cc57881ef 100644 --- a/Source/Bifrost/Configuration/Configure.cs +++ b/Source/Bifrost/Configuration/Configure.cs @@ -280,7 +280,7 @@ static AssembliesConfigurationBuilder BuildAssembliesConfigurationIfCallbackDefi { var builder = new AssembliesConfigurationBuilder(); if (assembliesConfigurationBuilderCallback != null) assembliesConfigurationBuilderCallback(builder); - if (builder.RuleBuilder == null) builder.IncludeAll(); + if (builder.RuleBuilder == null) builder.IncludeNone(); return builder; } diff --git a/Source/Bifrost/Execution/AssemblyProvider.cs b/Source/Bifrost/Execution/AssemblyProvider.cs index ff0604de8..f56509a0f 100644 --- a/Source/Bifrost/Execution/AssemblyProvider.cs +++ b/Source/Bifrost/Execution/AssemblyProvider.cs @@ -92,12 +92,6 @@ void SpecifyRules(Assembly assembly) _assemblySpecifiers.SpecifyUsingSpecifiersFrom(assembly); } - void ReapplyFilter() - { - var assembliesToRemove = _assemblies.Where(a => !_assemblyFilters.ShouldInclude(a.GetName().Name)).ToArray(); - assembliesToRemove.ForEach((a) =>_assemblies.Remove(a)); - } - void AddAssembly(Assembly assembly) { lock (_lockObject) @@ -108,8 +102,6 @@ void AddAssembly(Assembly assembly) _assemblies.Add(assembly); _contractToImplementorsMap.Feed(assembly.GetTypes()); SpecifyRules(assembly); - // TODO: This is not needed when assemblies become opt-in. - ReapplyFilter(); } } } diff --git a/Source/Bifrost/Execution/DefaultAssemblySpecifier.cs b/Source/Bifrost/Execution/DefaultAssemblySpecifier.cs deleted file mode 100644 index 424260d64..000000000 --- a/Source/Bifrost/Execution/DefaultAssemblySpecifier.cs +++ /dev/null @@ -1,25 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Configuration.Assemblies; - -namespace Bifrost.Execution -{ - /// - /// Represents a default assembly specifier - /// - public class DefaultAssemblySpecifier : ICanSpecifyAssemblies - { -#pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) - { - builder.ExcludeAssembliesStartingWith( - "System", - "mscorlib", - "Microsoft" - ); - } -#pragma warning restore 1591 // Xml Comments - } -} From a3811d69f1da50da36a538d165507945397a7dcb Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:29:48 +0100 Subject: [PATCH 3/7] Simplify assembly specification #660 Original issue: Bifrost#5 --- .../given/a_configure_instance.cs | 2 +- Source/Bifrost/AssemblySpecifier.cs | 6 +- Source/Bifrost/Bifrost.csproj | 7 +- .../Assemblies/AssembliesConfiguration.cs | 30 ------- .../AssembliesConfigurationBuilder.cs | 30 ------- ....cs => AssemblyConfigurationExtensions.cs} | 15 ++-- ...Builder.cs => IAssembliesConfiguration.cs} | 7 +- .../Configuration/Assemblies/IncludeNone.cs | 5 +- .../Assemblies/IncludeNoneExtensions.cs | 25 ------ Source/Bifrost/Configuration/Configure.cs | 90 ++++++++++++------- .../Configuration/Defaults/DefaultBindings.cs | 10 +-- Source/Bifrost/Configuration/IConfigure.cs | 2 +- Source/Bifrost/Execution/AssemblyFilters.cs | 4 +- .../Bifrost/Execution/AssemblySpecifiers.cs | 17 ++-- .../Execution/ICanSpecifyAssemblies.cs | 8 +- 15 files changed, 98 insertions(+), 160 deletions(-) delete mode 100644 Source/Bifrost/Configuration/Assemblies/AssembliesConfiguration.cs delete mode 100644 Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs rename Source/Bifrost/Configuration/Assemblies/{AssemblyRuleBuilderExtensions.cs => AssemblyConfigurationExtensions.cs} (50%) rename Source/Bifrost/Configuration/Assemblies/{IAssemblyRuleBuilder.cs => IAssembliesConfiguration.cs} (73%) delete mode 100644 Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs diff --git a/Source/Bifrost.Specs/Configuration/for_Configure/given/a_configure_instance.cs b/Source/Bifrost.Specs/Configuration/for_Configure/given/a_configure_instance.cs index 3762e01ba..85099b2e2 100644 --- a/Source/Bifrost.Specs/Configuration/for_Configure/given/a_configure_instance.cs +++ b/Source/Bifrost.Specs/Configuration/for_Configure/given/a_configure_instance.cs @@ -73,7 +73,7 @@ public class a_configure_instance type_importer_mock = new Mock(); container_mock.Setup(c => c.Get()).Returns(type_importer_mock.Object); - configure_instance = Configure.With(container_mock.Object, default_conventions_mock.Object, default_bindings_mock.Object, new AssembliesConfiguration(null)); + configure_instance = Configure.With(container_mock.Object, default_conventions_mock.Object, default_bindings_mock.Object, Mock.Of()); configurators_mock = new Mock>(); configurators_mock.Setup(c => c.GetEnumerator()).Returns(new List().GetEnumerator()); diff --git a/Source/Bifrost/AssemblySpecifier.cs b/Source/Bifrost/AssemblySpecifier.cs index 1edddec65..e0d5ce3a9 100644 --- a/Source/Bifrost/AssemblySpecifier.cs +++ b/Source/Bifrost/AssemblySpecifier.cs @@ -8,14 +8,14 @@ namespace Bifrost { /// - /// Reperesents an assembly specifier for client aspects + /// Represents an assembly specifier for client aspects. /// public class AssemblySpecifier : ICanSpecifyAssemblies { #pragma warning disable 1591 // Xml Comments - public void Specify(IAssemblyRuleBuilder builder) + public void Specify(IAssembliesConfiguration configuration) { - builder.IncludeAssembliesStartingWith("Bifrost"); + configuration.IncludeAssembliesStartingWith("Bifrost"); } #pragma warning disable 1591 // Xml Comments } diff --git a/Source/Bifrost/Bifrost.csproj b/Source/Bifrost/Bifrost.csproj index 04816f950..df049e22e 100644 --- a/Source/Bifrost/Bifrost.csproj +++ b/Source/Bifrost/Bifrost.csproj @@ -56,7 +56,6 @@ - @@ -129,10 +128,8 @@ - - - - + + diff --git a/Source/Bifrost/Configuration/Assemblies/AssembliesConfiguration.cs b/Source/Bifrost/Configuration/Assemblies/AssembliesConfiguration.cs deleted file mode 100644 index 5d8f1bc28..000000000 --- a/Source/Bifrost/Configuration/Assemblies/AssembliesConfiguration.cs +++ /dev/null @@ -1,30 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Represents the configuration for Assemblies - /// - public class AssembliesConfiguration - { - IAssemblyRuleBuilder _assemblyRuleBuilder; - - /// - /// Initializes a new instance of - /// - /// that builds the rules - public AssembliesConfiguration(IAssemblyRuleBuilder assemblyRuleBuilder) - { - _assemblyRuleBuilder = assemblyRuleBuilder; - } - - /// - /// Gets the specification used to specifying which assemblies to include - /// - public Specification Specification { get { return _assemblyRuleBuilder.Specification; } } - } -} diff --git a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs b/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs deleted file mode 100644 index 7ce49fdf0..000000000 --- a/Source/Bifrost/Configuration/Assemblies/AssembliesConfigurationBuilder.cs +++ /dev/null @@ -1,30 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) 2008-2017 Dolittle. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Represents a builder for building configuration used by - /// - public class AssembliesConfigurationBuilder - { - /// - /// Gets the rule builder used - /// - public IAssemblyRuleBuilder RuleBuilder { get; private set; } - - /// - /// Include no assemblies. - /// - /// - /// Returns the configuration object for the rule. - /// - public IncludeNone IncludeNone() - { - var includeNone = new IncludeNone(); - RuleBuilder = includeNone; - return includeNone; - } - } -} diff --git a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs b/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs similarity index 50% rename from Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs rename to Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs index 3550e4a82..9a7436cc4 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssemblyRuleBuilderExtensions.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs @@ -7,20 +7,21 @@ namespace Bifrost.Configuration.Assemblies { /// - /// Provides extensions for + /// Provides extensions for . /// - public static class AssemblyRuleBuilderExtensions + public static class AssemblyConfigurationExtensions { /// /// Includes specified assemblies. /// - /// to build upon. + /// to build upon. /// Names that assemblies should be starting with. - /// Chained - public static IAssemblyRuleBuilder IncludeAssembliesStartingWith(this IAssemblyRuleBuilder assemblyBuilder, params string[] names) + /// Chained + public static IAssembliesConfiguration IncludeAssembliesStartingWith( + this IAssembliesConfiguration assembliesConfiguration, params string[] names) { - assemblyBuilder.Specification = assemblyBuilder.Specification.And(new AssembliesStartingWith(names)); - return assemblyBuilder; + assembliesConfiguration.Specification = assembliesConfiguration.Specification.Or(new AssembliesStartingWith(names)); + return assembliesConfiguration; } } } diff --git a/Source/Bifrost/Configuration/Assemblies/IAssemblyRuleBuilder.cs b/Source/Bifrost/Configuration/Assemblies/IAssembliesConfiguration.cs similarity index 73% rename from Source/Bifrost/Configuration/Assemblies/IAssemblyRuleBuilder.cs rename to Source/Bifrost/Configuration/Assemblies/IAssembliesConfiguration.cs index c6f19a3be..b7859ea27 100644 --- a/Source/Bifrost/Configuration/Assemblies/IAssemblyRuleBuilder.cs +++ b/Source/Bifrost/Configuration/Assemblies/IAssembliesConfiguration.cs @@ -7,13 +7,12 @@ namespace Bifrost.Configuration.Assemblies { /// - /// Defines a rule builder for building configuration for assemblies and how to include - /// or exclude assemblies + /// Defines a configuration for how to include or exclude assemblies. /// - public interface IAssemblyRuleBuilder + public interface IAssembliesConfiguration { /// - /// Get the specification to use + /// Get the specification to use. /// Specification Specification { get; set; } } diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs b/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs index 7f4ebbfc1..2ea5389cc 100644 --- a/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs +++ b/Source/Bifrost/Configuration/Assemblies/IncludeNone.cs @@ -3,10 +3,9 @@ namespace Bifrost.Configuration.Assemblies { /// - /// Represents the builder for building the and - /// possible exceptions. + /// Represents the for building the . /// - public class IncludeNone : IAssemblyRuleBuilder + public class IncludeNone : IAssembliesConfiguration { /// /// Initializes an instance of . diff --git a/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs b/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs deleted file mode 100644 index b2502ad02..000000000 --- a/Source/Bifrost/Configuration/Assemblies/IncludeNoneExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Bifrost.Extensions; -using Bifrost.Specifications; - -namespace Bifrost.Configuration.Assemblies -{ - /// - /// Extensions for . - /// - public static class IncludeNoneExtensions - { - /// - /// Include all assemblies that has a name starting with the given name - /// - /// Configuration object - /// Names of assemblies to exclude - /// Chain of configuration object - public static IncludeNone ExceptAssembliesStartingWith(this IncludeNone includeNone, params string[] assemblyNames) - { - var specification = includeNone.Specification; - assemblyNames.ForEach(assemblyName => specification = specification.Or(new AssembliesStartingWith(assemblyName))); - includeNone.Specification = specification; - return includeNone; - } - } -} diff --git a/Source/Bifrost/Configuration/Configure.cs b/Source/Bifrost/Configuration/Configure.cs index cc57881ef..03c401f0e 100644 --- a/Source/Bifrost/Configuration/Configure.cs +++ b/Source/Bifrost/Configuration/Configure.cs @@ -28,12 +28,16 @@ public class Configure : IConfigure /// public static Configure Instance { get; private set; } - - Configure(IContainer container, BindingLifecycle defaultLifecycle, IDefaultConventions defaultConventions, IDefaultBindings defaultBindings, AssembliesConfiguration assembliesConfiguration) + Configure( + IContainer container, + BindingLifecycle defaultLifecycle, + IDefaultConventions defaultConventions, + IDefaultBindings defaultBindings, + IAssembliesConfiguration assembliesConfiguration) { SystemName = "[Not Set]"; - AssembliesConfiguration = assembliesConfiguration; + Assemblies = assembliesConfiguration; container.DefaultLifecycle = defaultLifecycle; container.Bind(this); @@ -50,15 +54,16 @@ public class Configure : IConfigure /// Configure by letting Bifrost discover anything that implements the discoverable configuration interfaces /// /// - public static Configure DiscoverAndConfigure(Action assembliesConfigurationBuilderCallback = null, IEnumerable additionalAssemblyProviders = null) + public static Configure DiscoverAndConfigure( + IAssembliesConfiguration assembliesConfiguration = null, + IEnumerable additionalAssemblyProviders = null) { - IContractToImplementorsMap contractToImplementorsMap; - var assembliesConfigurationBuilder = BuildAssembliesConfigurationIfCallbackDefined(assembliesConfigurationBuilderCallback); + assembliesConfiguration = assembliesConfiguration ?? new IncludeNone(); - contractToImplementorsMap = new ContractToImplementorsMap(); + IContractToImplementorsMap contractToImplementorsMap = new ContractToImplementorsMap(); var executingAssembly = typeof(Configure).GetTypeInfo().Assembly; contractToImplementorsMap.Feed(executingAssembly.GetTypes()); - var assemblySpecifiers = new AssemblySpecifiers(contractToImplementorsMap, new TypeFinder(), assembliesConfigurationBuilder.RuleBuilder); + var assemblySpecifiers = new AssemblySpecifiers(contractToImplementorsMap, new TypeFinder(), assembliesConfiguration); assemblySpecifiers.SpecifyUsingSpecifiersFrom(executingAssembly); var assemblyProviders = new List @@ -70,10 +75,8 @@ public static Configure DiscoverAndConfigure(Action and the Lifecycle of objects set to none /// /// to configure with - /// to use + /// to use /// to use for providing assemblies /// for keeping track of the relationship between contracts and implementors /// Configuration object to continue configuration on - public static Configure With(IContainer container, AssembliesConfiguration assembliesConfiguration, IAssemblyProvider assemblyProvider, IContractToImplementorsMap contractToImplementorsMap) + public static Configure With( + IContainer container, + IAssembliesConfiguration assembliesConfiguration, + IAssemblyProvider assemblyProvider, + IContractToImplementorsMap contractToImplementorsMap) { - return With(container, BindingLifecycle.Transient, assembliesConfiguration, assemblyProvider, contractToImplementorsMap); + return With( + container, + BindingLifecycle.Transient, + assembliesConfiguration, + assemblyProvider, + contractToImplementorsMap); } /// @@ -112,13 +124,22 @@ public static Configure With(IContainer container, AssembliesConfiguration assem /// /// to configure with /// Default for object creation/management - /// to use + /// to use /// to use for providing assemblies /// for keeping track of the relationship between contracts and implementors /// Configuration object to continue configuration on - public static Configure With(IContainer container, BindingLifecycle defaultObjectLifecycle, AssembliesConfiguration assembliesConfiguration, IAssemblyProvider assemblyProvider, IContractToImplementorsMap contractToImplementorsMap) + public static Configure With( + IContainer container, + BindingLifecycle defaultObjectLifecycle, + IAssembliesConfiguration assembliesConfiguration, + IAssemblyProvider assemblyProvider, + IContractToImplementorsMap contractToImplementorsMap) { - return With(container, defaultObjectLifecycle, new DefaultConventions(container), new DefaultBindings(assembliesConfiguration, assemblyProvider, contractToImplementorsMap), assembliesConfiguration); + return With( + container, + defaultObjectLifecycle, + new DefaultConventions(container), + new DefaultBindings(assembliesConfiguration, assemblyProvider, contractToImplementorsMap), assembliesConfiguration); } /// @@ -136,14 +157,22 @@ public static void Reset() /// to configure with /// to use /// to use - /// to use + /// to use /// - public static Configure With(IContainer container, IDefaultConventions defaultConventions, IDefaultBindings defaultBindings, AssembliesConfiguration assembliesConfiguration) + public static Configure With( + IContainer container, + IDefaultConventions defaultConventions, + IDefaultBindings defaultBindings, + IAssembliesConfiguration assembliesConfiguration) { - return With(container, BindingLifecycle.Transient, defaultConventions, defaultBindings, assembliesConfiguration); + return With( + container, + BindingLifecycle.Transient, + defaultConventions, + defaultBindings, + assembliesConfiguration); } - /// /// Configure with a specific , and /// @@ -151,9 +180,14 @@ public static Configure With(IContainer container, IDefaultConventions defaultCo /// Default for object creation/management /// to use /// to use - /// to use + /// to use /// - public static Configure With(IContainer container, BindingLifecycle defaultObjectLifecycle, IDefaultConventions defaultConventions, IDefaultBindings defaultBindings, AssembliesConfiguration assembliesConfiguration) + public static Configure With( + IContainer container, + BindingLifecycle defaultObjectLifecycle, + IDefaultConventions defaultConventions, + IDefaultBindings defaultBindings, + IAssembliesConfiguration assembliesConfiguration) { if (Instance == null) { @@ -170,7 +204,6 @@ public static Configure With(IContainer container, BindingLifecycle defaultObjec public IContainer Container { get; private set; } public string SystemName { get; set; } public Assembly EntryAssembly { get; private set; } - public AssembliesConfiguration AssembliesConfiguration { get; private set; } public IDefaultStorageConfiguration DefaultStorage { get; set; } public ICommandsConfiguration Commands { get; private set; } public IEventsConfiguration Events { get; private set; } @@ -183,7 +216,7 @@ public static Configure With(IContainer container, BindingLifecycle defaultObjec public ICallContextConfiguration CallContext { get; private set; } public IExecutionContextConfiguration ExecutionContext { get; private set; } public ISecurityConfiguration Security { get; private set; } - public AssembliesConfiguration Assemblies { get; private set; } + public IAssembliesConfiguration Assemblies { get; private set; } public IQualityAssurance QualityAssurance { get; private set; } public CultureInfo Culture { get; set; } public CultureInfo UICulture { get; set; } @@ -276,15 +309,6 @@ static Type DiscoverCanCreateContainerType(IEnumerable assemblies) return createContainerType; } - static AssembliesConfigurationBuilder BuildAssembliesConfigurationIfCallbackDefined(Action assembliesConfigurationBuilderCallback) - { - var builder = new AssembliesConfigurationBuilder(); - if (assembliesConfigurationBuilderCallback != null) assembliesConfigurationBuilderCallback(builder); - if (builder.RuleBuilder == null) builder.IncludeNone(); - return builder; - } - - static void ThrowIfAmbiguousMatchFoundForCanCreateContainer(Type createContainerType) { if (createContainerType != null) diff --git a/Source/Bifrost/Configuration/Defaults/DefaultBindings.cs b/Source/Bifrost/Configuration/Defaults/DefaultBindings.cs index 7a73c7203..8ad10d141 100644 --- a/Source/Bifrost/Configuration/Defaults/DefaultBindings.cs +++ b/Source/Bifrost/Configuration/Defaults/DefaultBindings.cs @@ -12,14 +12,14 @@ namespace Bifrost.Configuration.Defaults /// public class DefaultBindings : IDefaultBindings { - AssembliesConfiguration _assembliesConfiguration; - IAssemblyProvider _assemblyProvider; - IContractToImplementorsMap _contractToImplentorsMap; + readonly IAssembliesConfiguration _assembliesConfiguration; + readonly IAssemblyProvider _assemblyProvider; + readonly IContractToImplementorsMap _contractToImplentorsMap; /// /// Initializes a new instance of /// - public DefaultBindings(AssembliesConfiguration assembliesConfiguration, IAssemblyProvider assemblyProvider, IContractToImplementorsMap contractToImplentorsMap) + public DefaultBindings(IAssembliesConfiguration assembliesConfiguration, IAssemblyProvider assemblyProvider, IContractToImplementorsMap contractToImplentorsMap) { _assembliesConfiguration = assembliesConfiguration; _assemblyProvider = assemblyProvider; @@ -31,7 +31,7 @@ public void Initialize(IContainer container) { container.Bind(container); container.Bind(_contractToImplentorsMap); - container.Bind(_assembliesConfiguration); + container.Bind(_assembliesConfiguration); container.Bind(_assemblyProvider); container.Bind(typeof(global::Bifrost.Execution.Assemblies), BindingLifecycle.Singleton); container.Bind(typeof(TypeDiscoverer), BindingLifecycle.Singleton); diff --git a/Source/Bifrost/Configuration/IConfigure.cs b/Source/Bifrost/Configuration/IConfigure.cs index 0bdec900d..cec6ebaaf 100644 --- a/Source/Bifrost/Configuration/IConfigure.cs +++ b/Source/Bifrost/Configuration/IConfigure.cs @@ -95,7 +95,7 @@ public interface IConfigure /// /// Gets the configuration for assemblies and how they are treated /// - AssembliesConfiguration Assemblies { get; } + IAssembliesConfiguration Assemblies { get; } /// /// Gets or sets the culture to use in Bifrost diff --git a/Source/Bifrost/Execution/AssemblyFilters.cs b/Source/Bifrost/Execution/AssemblyFilters.cs index 9b8a1eaf4..c6213d20b 100644 --- a/Source/Bifrost/Execution/AssemblyFilters.cs +++ b/Source/Bifrost/Execution/AssemblyFilters.cs @@ -11,13 +11,13 @@ namespace Bifrost.Execution /// public class AssemblyFilters : IAssemblyFilters { - AssembliesConfiguration _assembliesConfiguration; + readonly IAssembliesConfiguration _assembliesConfiguration; /// /// Initializes an instance of /// /// - public AssemblyFilters(AssembliesConfiguration assembliesConfiguration) + public AssemblyFilters(IAssembliesConfiguration assembliesConfiguration) { _assembliesConfiguration = assembliesConfiguration; } diff --git a/Source/Bifrost/Execution/AssemblySpecifiers.cs b/Source/Bifrost/Execution/AssemblySpecifiers.cs index b1c058087..4a53b9562 100644 --- a/Source/Bifrost/Execution/AssemblySpecifiers.cs +++ b/Source/Bifrost/Execution/AssemblySpecifiers.cs @@ -15,20 +15,23 @@ namespace Bifrost.Execution /// public class AssemblySpecifiers : IAssemblySpecifiers { - ITypeFinder _typeFinder; - IContractToImplementorsMap _contractToImplementorsMap; - IAssemblyRuleBuilder _assemblyRuleBuilder; + readonly ITypeFinder _typeFinder; + readonly IContractToImplementorsMap _contractToImplementorsMap; + readonly IAssembliesConfiguration _assembliesConfiguration; /// /// Initializes a new instance of /// /// for keeping track of the relationship between contracts and implementors /// to use for finding types - /// used for building the rules for assemblies - public AssemblySpecifiers(IContractToImplementorsMap contractToImplementorsMap, ITypeFinder typeFinder, IAssemblyRuleBuilder assemblyRuleBuilder) + /// used for building the rules for assemblies + public AssemblySpecifiers( + IContractToImplementorsMap contractToImplementorsMap, + ITypeFinder typeFinder, + IAssembliesConfiguration assembliesConfiguration) { _typeFinder = typeFinder; - _assemblyRuleBuilder = assemblyRuleBuilder; + _assembliesConfiguration = assembliesConfiguration; _contractToImplementorsMap = contractToImplementorsMap; } @@ -42,7 +45,7 @@ public void SpecifyUsingSpecifiersFrom(Assembly assembly) .ForEach(type => { var specifier = Activator.CreateInstance(type) as ICanSpecifyAssemblies; - specifier.Specify(_assemblyRuleBuilder); + specifier.Specify(_assembliesConfiguration); }); } #pragma warning restore 1591 // Xml Comments diff --git a/Source/Bifrost/Execution/ICanSpecifyAssemblies.cs b/Source/Bifrost/Execution/ICanSpecifyAssemblies.cs index ba84f7e88..cf2eb52fd 100644 --- a/Source/Bifrost/Execution/ICanSpecifyAssemblies.cs +++ b/Source/Bifrost/Execution/ICanSpecifyAssemblies.cs @@ -8,7 +8,7 @@ namespace Bifrost.Execution { /// - /// Specifies what assemblies to include + /// Specifies what assemblies to include or not. /// /// /// Typically used by implementations of to @@ -21,9 +21,9 @@ namespace Bifrost.Execution public interface ICanSpecifyAssemblies : IConvention { /// - /// Method that gets called for specifying which assemblies to include or not + /// Method that gets called for specifying which assemblies to include or not. /// - /// object to build specification on - void Specify(IAssemblyRuleBuilder builder); + /// object to build specification on. + void Specify(IAssembliesConfiguration configuration); } } From 9acf4bd34d78c98ac4f3981e38c8b20f4982a46d Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:29:48 +0100 Subject: [PATCH 4/7] More fixes --- Source/Bifrost/Configuration/Configure.cs | 24 ++++---- .../Execution/AppDomainAssemblyProvider.cs | 56 ++++++++++-------- Source/Bifrost/Execution/AssemblyProvider.cs | 50 ++++++++++------ .../Bifrost/Execution/AssemblySpecifiers.cs | 59 ++++++++++++------- .../Bifrost/Execution/IAssemblySpecifiers.cs | 7 ++- 5 files changed, 120 insertions(+), 76 deletions(-) diff --git a/Source/Bifrost/Configuration/Configure.cs b/Source/Bifrost/Configuration/Configure.cs index 03c401f0e..3379f7746 100644 --- a/Source/Bifrost/Configuration/Configure.cs +++ b/Source/Bifrost/Configuration/Configure.cs @@ -58,25 +58,27 @@ public static Configure DiscoverAndConfigure( IAssembliesConfiguration assembliesConfiguration = null, IEnumerable additionalAssemblyProviders = null) { - assembliesConfiguration = assembliesConfiguration ?? new IncludeNone(); - - IContractToImplementorsMap contractToImplementorsMap = new ContractToImplementorsMap(); - var executingAssembly = typeof(Configure).GetTypeInfo().Assembly; - contractToImplementorsMap.Feed(executingAssembly.GetTypes()); - var assemblySpecifiers = new AssemblySpecifiers(contractToImplementorsMap, new TypeFinder(), assembliesConfiguration); - assemblySpecifiers.SpecifyUsingSpecifiersFrom(executingAssembly); - var assemblyProviders = new List { #if(NET461) new AppDomainAssemblyProvider(), #endif - //new DefaultAssemblyProvider() - new FileSystemAssemblyProvider(new FileSystem()) + //new DefaultAssemblyProvider(), + new FileSystemAssemblyProvider(new FileSystem()), }; - if (additionalAssemblyProviders != null) assemblyProviders.AddRange(additionalAssemblyProviders); + if (additionalAssemblyProviders != null) + { + assemblyProviders.AddRange(additionalAssemblyProviders); + } + + assembliesConfiguration = assembliesConfiguration ?? new IncludeNone(); + var assemblySpecifiers = new AssemblySpecifiers(assembliesConfiguration); + var executingAssembly = typeof(Configure).GetTypeInfo().Assembly; + IContractToImplementorsMap contractToImplementorsMap = new ContractToImplementorsMap(); + contractToImplementorsMap.Feed(executingAssembly.GetTypes()); + assemblySpecifiers.SpecifyUsingSpecifiersFrom(executingAssembly); var assemblyProvider = new AssemblyProvider( assemblyProviders, new AssemblyFilters(assembliesConfiguration), diff --git a/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs b/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs index 70acbee43..8a4ad5598 100644 --- a/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs +++ b/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs @@ -2,58 +2,64 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ + using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; namespace Bifrost.Execution { /// - /// Represents an implementation of that provides assemblies from the current + /// Represents an implementation of that provides assemblies from the current . /// public class AppDomainAssemblyProvider : ICanProvideAssemblies { + private readonly IDictionary _loadedAssemblies; + /// - /// Initializes a new instance of + /// Initializes a new instance of . /// public AppDomainAssemblyProvider() { AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded; + _loadedAssemblies = BuildAssemblyMap(); } -#pragma warning disable 1591 // Xml Comments - public event AssemblyAdded AssemblyAdded = (a) => { }; - - public IEnumerable AvailableAssemblies + private void AssemblyLoaded(object sender, AssemblyLoadEventArgs args) { - get + var assembly = args.LoadedAssembly; + if (!assembly.IsDynamic) { - return AppDomain.CurrentDomain.GetAssemblies() - .Where(assembly => !assembly.IsDynamic) - .Select(assembly => - { - var name = assembly.GetName(); - var assemblyInfo = new AssemblyInfo(name.Name, assembly.Location); - return assemblyInfo; - }); + _loadedAssemblies[AssemblyInfoFromAssembly(assembly)] = assembly; + AssemblyAdded(assembly); } } - public Assembly Get(AssemblyInfo assemblyInfo) + private static IDictionary BuildAssemblyMap() { - return AppDomain.CurrentDomain.GetAssemblies() - .Where(assembly => - !assembly.IsDynamic && - assembly.GetName().Name == assemblyInfo.Name).SingleOrDefault(); + return AppDomain.CurrentDomain + .GetAssemblies() + .Where(a => !a.IsDynamic) + .ToDictionary(AssemblyInfoFromAssembly, a => a); } -#pragma warning restore 1591 // Xml Comments - void AssemblyLoaded(object sender, AssemblyLoadEventArgs args) + private static AssemblyInfo AssemblyInfoFromAssembly(Assembly assembly) { + return new AssemblyInfo(assembly.GetName().Name, assembly.Location); + } - AssemblyAdded(args.LoadedAssembly); +#pragma warning disable 1591 // Xml Comments + public event AssemblyAdded AssemblyAdded = a => { }; + + public IEnumerable AvailableAssemblies => _loadedAssemblies.Keys; + + public Assembly Get(AssemblyInfo assemblyInfo) + { + Assembly assembly; + _loadedAssemblies.TryGetValue(assemblyInfo, out assembly); + return assembly; } +#pragma warning restore 1591 // Xml Comments } -} +} \ No newline at end of file diff --git a/Source/Bifrost/Execution/AssemblyProvider.cs b/Source/Bifrost/Execution/AssemblyProvider.cs index f56509a0f..90b32017c 100644 --- a/Source/Bifrost/Execution/AssemblyProvider.cs +++ b/Source/Bifrost/Execution/AssemblyProvider.cs @@ -67,43 +67,59 @@ void HookUpAssemblyAddedForProviders() void AssemblyLoaded(Assembly assembly) { - if (_assemblyUtility.IsAssemblyDynamic(assembly)) return; + if (_assemblyUtility.IsAssemblyDynamic(assembly)) + { + return; + } - var assemblyFile = new FileInfo(assembly.Location); - if (!_assemblyFilters.ShouldInclude(assemblyFile.Name)) return; + var newSpecifiers = _assemblySpecifiers.SpecifyUsingSpecifiersFrom(assembly); AddAssembly(assembly); + if (newSpecifiers) + { + RecalculateAssemblies(); + } } void Populate() { - foreach (var provider in _assemblyProviders) - { - var assembliesToInclude = provider.AvailableAssemblies.Where( - a => - _assemblyFilters.ShouldInclude(a.FileName) && - _assemblyUtility.IsAssembly(a) - ); - assembliesToInclude.Select(provider.Get).ForEach(AddAssembly); - } + var assemblies = AvailableAssemblies(); + assemblies.ForEach(a => _assemblySpecifiers.SpecifyUsingSpecifiersFrom(a)); + assemblies.ForEach(AddAssembly); } - void SpecifyRules(Assembly assembly) + IList AvailableAssemblies() { - _assemblySpecifiers.SpecifyUsingSpecifiersFrom(assembly); + return _assemblyProviders + .SelectMany(p => p.AvailableAssemblies, (provider, assemblyInfo) => new {provider, assemblyInfo}) + .GroupBy(pair => pair.assemblyInfo.Name) + .Select(group => group.First()) + .Where(pair => _assemblyUtility.IsAssembly(pair.assemblyInfo)) + .Select(pair => pair.provider.Get(pair.assemblyInfo)) + .Where(assembly => !assembly.IsDynamic) + .ToList(); + } + + bool ShouldInclude(Assembly assembly) + { + return _assemblyFilters.ShouldInclude(new FileInfo(assembly.Location).Name); } void AddAssembly(Assembly assembly) { lock (_lockObject) { - if (!_assemblies.Contains(assembly, comparer) && - !_assemblyUtility.IsAssemblyDynamic(assembly)) + if (!_assemblies.Contains(assembly, comparer) && ShouldInclude(assembly)) { _assemblies.Add(assembly); _contractToImplementorsMap.Feed(assembly.GetTypes()); - SpecifyRules(assembly); } } } + + void RecalculateAssemblies() + { + var assemblies = AvailableAssemblies(); + assemblies.ForEach(AddAssembly); + } } } diff --git a/Source/Bifrost/Execution/AssemblySpecifiers.cs b/Source/Bifrost/Execution/AssemblySpecifiers.cs index 4a53b9562..a974a4607 100644 --- a/Source/Bifrost/Execution/AssemblySpecifiers.cs +++ b/Source/Bifrost/Execution/AssemblySpecifiers.cs @@ -3,6 +3,7 @@ * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Bifrost.Configuration.Assemblies; @@ -15,38 +16,56 @@ namespace Bifrost.Execution /// public class AssemblySpecifiers : IAssemblySpecifiers { - readonly ITypeFinder _typeFinder; - readonly IContractToImplementorsMap _contractToImplementorsMap; + static readonly object LockObject = new object(); + readonly IAssembliesConfiguration _assembliesConfiguration; + readonly ISet _specifiedAssemblies; /// /// Initializes a new instance of /// - /// for keeping track of the relationship between contracts and implementors - /// to use for finding types - /// used for building the rules for assemblies - public AssemblySpecifiers( - IContractToImplementorsMap contractToImplementorsMap, - ITypeFinder typeFinder, - IAssembliesConfiguration assembliesConfiguration) + public AssemblySpecifiers(IAssembliesConfiguration assembliesConfiguration) { - _typeFinder = typeFinder; _assembliesConfiguration = assembliesConfiguration; - _contractToImplementorsMap = contractToImplementorsMap; + _specifiedAssemblies = new HashSet(); } #pragma warning disable 1591 // Xml Comments - public void SpecifyUsingSpecifiersFrom(Assembly assembly) + public bool SpecifyUsingSpecifiersFrom(Assembly assembly) { - _typeFinder - .FindMultiple(_contractToImplementorsMap) - .Where(t => t.GetTypeInfo().Assembly.FullName == assembly.FullName) - .Where(type => type.HasDefaultConstructor()) - .ForEach(type => + lock (LockObject) + { + if (_specifiedAssemblies.Contains(assembly.FullName)) + { + return false; + } + + _specifiedAssemblies.Add(assembly.FullName); + + var specified = false; + if (MayReferenceICanSpecifyAssemblies(assembly)) { - var specifier = Activator.CreateInstance(type) as ICanSpecifyAssemblies; - specifier.Specify(_assembliesConfiguration); - }); + assembly + .GetTypes() + .Where(t => t.GetTypeInfo().GetInterfaces().Contains(typeof(ICanSpecifyAssemblies))) + .Where(t => t.HasDefaultConstructor()) + .ForEach(t => + { + var specifier = Activator.CreateInstance(t) as ICanSpecifyAssemblies; + specifier.Specify(_assembliesConfiguration); + specified = true; + }); + } + + return specified; + } + } + + static bool MayReferenceICanSpecifyAssemblies(Assembly assembly) + { + return + assembly.FullName.Contains("Bifrost") || + assembly.GetReferencedAssemblies().Any(a => a.FullName.Contains("Bifrost")); } #pragma warning restore 1591 // Xml Comments } diff --git a/Source/Bifrost/Execution/IAssemblySpecifiers.cs b/Source/Bifrost/Execution/IAssemblySpecifiers.cs index acdcb363b..bac9df4e2 100644 --- a/Source/Bifrost/Execution/IAssemblySpecifiers.cs +++ b/Source/Bifrost/Execution/IAssemblySpecifiers.cs @@ -12,9 +12,10 @@ namespace Bifrost.Execution public interface IAssemblySpecifiers { /// - /// Specifies using specifiers found in a specific + /// Specifies using specifiers found in a specific /// - /// to find specifiers from - void SpecifyUsingSpecifiersFrom(Assembly assembly); + /// to find specifiers from + /// Whether any new specifiers was found in the assembly. + bool SpecifyUsingSpecifiersFrom(Assembly assembly); } } From 01d6a64352485d6e095bf6973d16a2ede3410e11 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:29:48 +0100 Subject: [PATCH 5/7] Include AssemblySpecifier for Web projects --- Source/Bifrost.Default/AssemblySpecifier.cs | 18 ++++++++++++++++++ Source/Bifrost.Default/Bifrost.Default.csproj | 1 + Source/Bifrost.QuickStart/AssemblySpecifier.cs | 18 ++++++++++++++++++ .../Bifrost.QuickStart.csproj | 1 + 4 files changed, 38 insertions(+) create mode 100644 Source/Bifrost.Default/AssemblySpecifier.cs create mode 100644 Source/Bifrost.QuickStart/AssemblySpecifier.cs diff --git a/Source/Bifrost.Default/AssemblySpecifier.cs b/Source/Bifrost.Default/AssemblySpecifier.cs new file mode 100644 index 000000000..a148f0f61 --- /dev/null +++ b/Source/Bifrost.Default/AssemblySpecifier.cs @@ -0,0 +1,18 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) 2008-2017 Dolittle. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +using Bifrost.Configuration.Assemblies; +using Bifrost.Execution; + +namespace Web +{ + public class AssemblySpecifier : ICanSpecifyAssemblies + { + public void Specify(IAssembliesConfiguration configuration) + { + configuration.IncludeAssembliesStartingWith("Web.dll"); + } + } +} diff --git a/Source/Bifrost.Default/Bifrost.Default.csproj b/Source/Bifrost.Default/Bifrost.Default.csproj index a9173b7f3..9e6e38a5d 100644 --- a/Source/Bifrost.Default/Bifrost.Default.csproj +++ b/Source/Bifrost.Default/Bifrost.Default.csproj @@ -77,6 +77,7 @@ Properties\CommonAssemblyInfo.cs + diff --git a/Source/Bifrost.QuickStart/AssemblySpecifier.cs b/Source/Bifrost.QuickStart/AssemblySpecifier.cs new file mode 100644 index 000000000..a148f0f61 --- /dev/null +++ b/Source/Bifrost.QuickStart/AssemblySpecifier.cs @@ -0,0 +1,18 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) 2008-2017 Dolittle. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +using Bifrost.Configuration.Assemblies; +using Bifrost.Execution; + +namespace Web +{ + public class AssemblySpecifier : ICanSpecifyAssemblies + { + public void Specify(IAssembliesConfiguration configuration) + { + configuration.IncludeAssembliesStartingWith("Web.dll"); + } + } +} diff --git a/Source/Bifrost.QuickStart/Bifrost.QuickStart.csproj b/Source/Bifrost.QuickStart/Bifrost.QuickStart.csproj index 585a7d2a9..48d1b5cd4 100644 --- a/Source/Bifrost.QuickStart/Bifrost.QuickStart.csproj +++ b/Source/Bifrost.QuickStart/Bifrost.QuickStart.csproj @@ -169,6 +169,7 @@ + From a6e8e3f80e6e940c4cab0122921fb7cfbf6a3320 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:29:48 +0100 Subject: [PATCH 6/7] Better error messages --- Source/Bifrost/Bifrost.csproj | 1 + .../Execution/AppDomainAssemblyProvider.cs | 1 + .../AssemblySpecificationException.cs | 34 ++++++++++++++ .../Bifrost/Execution/AssemblySpecifiers.cs | 46 +++++++++++++++---- 4 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 Source/Bifrost/Execution/AssemblySpecificationException.cs diff --git a/Source/Bifrost/Bifrost.csproj b/Source/Bifrost/Bifrost.csproj index df049e22e..2be076624 100644 --- a/Source/Bifrost/Bifrost.csproj +++ b/Source/Bifrost/Bifrost.csproj @@ -133,6 +133,7 @@ + diff --git a/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs b/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs index 8a4ad5598..b05adbaed 100644 --- a/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs +++ b/Source/Bifrost/Execution/AppDomainAssemblyProvider.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; namespace Bifrost.Execution { diff --git a/Source/Bifrost/Execution/AssemblySpecificationException.cs b/Source/Bifrost/Execution/AssemblySpecificationException.cs new file mode 100644 index 000000000..6ef105f7a --- /dev/null +++ b/Source/Bifrost/Execution/AssemblySpecificationException.cs @@ -0,0 +1,34 @@ +using System; + +namespace Bifrost.Execution +{ + /// + /// The exception that is thrown when specification of assemblies fails. + /// + public class AssemblySpecificationException : Exception + { + /// + /// Initializes an instance of . + /// + public AssemblySpecificationException() + { + } + + /// + /// Initializes an instance of . + /// + /// Message with details about the exception. + public AssemblySpecificationException(string message) : base(message) + { + } + + /// + /// Initializes an instance of . + /// + /// Message with details about the exception. + /// The exception that caused this exception. + public AssemblySpecificationException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/Source/Bifrost/Execution/AssemblySpecifiers.cs b/Source/Bifrost/Execution/AssemblySpecifiers.cs index a974a4607..80997148c 100644 --- a/Source/Bifrost/Execution/AssemblySpecifiers.cs +++ b/Source/Bifrost/Execution/AssemblySpecifiers.cs @@ -45,22 +45,48 @@ public bool SpecifyUsingSpecifiersFrom(Assembly assembly) var specified = false; if (MayReferenceICanSpecifyAssemblies(assembly)) { - assembly - .GetTypes() - .Where(t => t.GetTypeInfo().GetInterfaces().Contains(typeof(ICanSpecifyAssemblies))) - .Where(t => t.HasDefaultConstructor()) - .ForEach(t => - { - var specifier = Activator.CreateInstance(t) as ICanSpecifyAssemblies; - specifier.Specify(_assembliesConfiguration); - specified = true; - }); + try + { + specified = assembly + .GetTypes() + .Where(t => t.GetTypeInfo().GetInterfaces().Contains(typeof(ICanSpecifyAssemblies))) + .Select(SpecifyFrom) + .ToList() + .Count > 0; + } + catch (ReflectionTypeLoadException e) + { + throw new AssemblySpecificationException( + $"Error while reflecting on the types of {assembly.FullName}. Loader exceptions encountered:\n" + + string.Join("\n", e.LoaderExceptions.Select(l => l.Message).Distinct())); + } } return specified; } } + ICanSpecifyAssemblies SpecifyFrom(Type type) + { + try + { + var specifier = Activator.CreateInstance(type) as ICanSpecifyAssemblies; + specifier.Specify(_assembliesConfiguration); + return specifier; + } + catch (MissingMethodException) + { + throw new AssemblySpecificationException( + $"Could not create instance of type {type.FullName}. It must have a default constructor."); + } + catch (Exception e) + { + throw new AssemblySpecificationException( + $"Error while specifying assemblies from {type.FullName}: {e.Message}", + e); + } + } + static bool MayReferenceICanSpecifyAssemblies(Assembly assembly) { return From 65240d365b8f8c4f5257eb90863845d34e659365 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 25 Jan 2017 09:29:48 +0100 Subject: [PATCH 7/7] Still keep exclude --- .../Assemblies/AssemblyConfigurationExtensions.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs b/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs index 9a7436cc4..7585d7c3d 100644 --- a/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs +++ b/Source/Bifrost/Configuration/Assemblies/AssemblyConfigurationExtensions.cs @@ -23,5 +23,18 @@ public static IAssembliesConfiguration IncludeAssembliesStartingWith( assembliesConfiguration.Specification = assembliesConfiguration.Specification.Or(new AssembliesStartingWith(names)); return assembliesConfiguration; } + + /// + /// Excludes specified assemblies. + /// + /// to build upon. + /// Names that assemblies should not be starting with. + /// Chained + public static IAssembliesConfiguration ExcludeAssembliesStartingWith( + this IAssembliesConfiguration assembliesConfiguration, params string[] names) + { + assembliesConfiguration.Specification = assembliesConfiguration.Specification.AndNot(new AssembliesStartingWith(names)); + return assembliesConfiguration; + } } }