Skip to content

Commit

Permalink
TEMP: possibly split to first take the changes in the various unit te…
Browse files Browse the repository at this point in the history
…sts and then the removal of ContextAwareTypeUtility
  • Loading branch information
MichaelKetting committed Feb 27, 2024
1 parent f74b975 commit 2c93ad1
Show file tree
Hide file tree
Showing 30 changed files with 208 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,74 +70,13 @@ public TypeDiscoveryConfiguration ()
}

/// <summary>
/// Gets or sets the <see cref="TypeDiscoveryMode"/> to be used for type discovery.
/// </summary>
/// <value>The <see cref="TypeDiscoveryMode"/> to be used for type discovery.</value>
[ConfigurationProperty("mode", DefaultValue = TypeDiscoveryMode.Automatic, IsRequired = false)]
public TypeDiscoveryMode Mode
{
get { return (TypeDiscoveryMode)this["mode"]; }
set { this["mode"] = value; }
}

/// <summary>
/// Gets a <see cref="RootAssembliesElement"/> describing specific root assemblies to be used. This is only relevant
/// if <see cref="Mode"/> is set to <see cref="TypeDiscoveryMode.SpecificRootAssemblies"/>. In this mode, an
/// <see cref="AssemblyFinderTypeDiscoveryService"/> is created, and the given root assemblies are employed for type discovery.
/// Note that even if an assembly is specified as a root assembly, the default filtering rules (<see cref="ApplicationAssemblyLoaderFilter"/>)
/// still apply even for that assembly.
/// Gets a <see cref="RootAssembliesElement"/> describing specific root assemblies to be used.
/// </summary>
/// <value>A <see cref="RootAssembliesElement"/> describing specific root assemblies to be used.</value>
[ConfigurationProperty("specificRootAssemblies", IsRequired = false)]
public RootAssembliesElement SpecificRootAssemblies
{
get { return (RootAssembliesElement)this["specificRootAssemblies"]; }
}

/// <summary>
/// Creates an <see cref="ITypeDiscoveryService"/> instance as indicated by <see cref="Mode"/>.
/// </summary>
/// <returns>A new <see cref="ITypeDiscoveryService"/> that discovers types as indicated by <see cref="Mode"/>.</returns>
public ITypeDiscoveryService CreateTypeDiscoveryService ()
{
switch (Mode)
{
case TypeDiscoveryMode.SpecificRootAssemblies:
return CreateServiceWithSpecificRootAssemblies();
default:
return CreateServiceWithAutomaticDiscovery();
}
}

private ITypeDiscoveryService CreateServiceWithSpecificRootAssemblies ()
{
var assemblyLoader = CreateAllAssemblyLoader();
var rootAssemblyFinder = SpecificRootAssemblies.CreateRootAssemblyFinder(assemblyLoader);
return CreateServiceWithAssemblyFinder(rootAssemblyFinder);
}

private ITypeDiscoveryService CreateServiceWithAutomaticDiscovery ()
{
var assemblyLoader = CreateApplicationAssemblyLoader();
var searchPathRootAssemblyFinder = SearchPathRootAssemblyFinder.CreateForCurrentAppDomain(false, assemblyLoader);
return CreateServiceWithAssemblyFinder(searchPathRootAssemblyFinder);
}

private ITypeDiscoveryService CreateServiceWithAssemblyFinder (IRootAssemblyFinder customRootAssemblyFinder)
{
var filteringAssemblyLoader = CreateApplicationAssemblyLoader();
var assemblyFinder = new CachingAssemblyFinderDecorator(new AssemblyFinder(customRootAssemblyFinder, filteringAssemblyLoader));
return new AssemblyFinderTypeDiscoveryService(assemblyFinder);
}

private IAssemblyLoader CreateApplicationAssemblyLoader ()
{
return new FilteringAssemblyLoader(ApplicationAssemblyLoaderFilter.Instance);
}

private IAssemblyLoader CreateAllAssemblyLoader ()
{
return new FilteringAssemblyLoader(new LoadAllAssemblyLoaderFilter());
}
}
}
21 changes: 3 additions & 18 deletions Remotion/Core/Core/Reflection/ContextAwareTypeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
//
using System;
using System.ComponentModel.Design;
using System.Threading;
using Remotion.Configuration.TypeDiscovery;
using Remotion.ServiceLocation;

namespace Remotion.Reflection
Expand All @@ -29,28 +27,15 @@ namespace Remotion.Reflection
/// <threadsafety static="true" instance="false"/>
public static class ContextAwareTypeUtility
{
/// <summary>Workaround to allow reflection to reset the fields since setting a static readonly field is not supported in .NET 3.0 and later.</summary>
private class Fields
{
public readonly Lazy<ITypeDiscoveryService> DefaultTypeDiscoveryService =
new Lazy<ITypeDiscoveryService>(
() => TypeDiscoveryConfiguration.Current.CreateTypeDiscoveryService(),
LazyThreadSafetyMode.ExecutionAndPublication);
}

private static readonly Fields s_fields = new Fields();

/// <summary>
/// Gets the current context-specific <see cref="ITypeDiscoveryService"/>. If an <see cref="T:System.ComponentModel.Design.IDesignerHost"/> is available,
/// the designer's <see cref="ITypeDiscoveryService"/> is returned. Otherwise, the <see cref="T:Remotion.Configuration.TypeDiscovery.TypeDiscoveryConfiguration"/>
/// is used to create a new <see cref="ITypeDiscoveryService"/> when the property is first retrieved. That instance is stored for later uses.
/// Gets the current context-specific <see cref="ITypeDiscoveryService"/>.
/// </summary>
/// <returns>The current context-specific <see cref="ITypeDiscoveryService"/>.</returns>
[Obsolete("Retrieve via the application's IoC container, e.g. SafeServiceLocator.Current.GetInstance<ITypeDiscoveryService>(). (Version 6.0.0)")]
public static ITypeDiscoveryService GetTypeDiscoveryService ()
{
// Here you could choose to get the ITypeDiscoveryService from IDesignerHost.GetService (typeof (ITypeDiscoveryService)) instead of the resolved one.

return s_fields.DefaultTypeDiscoveryService.Value;
return SafeServiceLocator.Current.GetInstance<ITypeDiscoveryService>();
}

/// <summary>
Expand Down
9 changes: 0 additions & 9 deletions Remotion/Core/Core/Schemas/TypeDiscoveryConfiguration.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@
</xs:complexType>
</xs:element>
</xs:choice>

<xs:attribute name="mode" default="Automatic" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Automatic"/>
<xs:enumeration value="SpecificRootAssemblies"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
// along with re-motion; if not, see http://www.gnu.org/licenses.
//
using System;
using Remotion.Configuration.TypeDiscovery;
using Remotion.Reflection.TypeDiscovery.AssemblyFinding;
using Remotion.Reflection.TypeDiscovery.AssemblyLoading;
using Remotion.Utilities;

namespace Remotion.ServiceLocation
Expand Down Expand Up @@ -63,5 +66,22 @@ public static void Register<TService> (this IBootstrapServiceConfiguration boots
typeof(TService),
ServiceImplementationInfo.CreateSingle(() => instance, LifetimeKind.Singleton)));
}

/// <summary>
/// The given root assemblies are employed for type discovery.
/// </summary>
/// <param name="bootstrapServiceConfiguration"></param>
public static void RegisterSpecificRootAssemblies (this IBootstrapServiceConfiguration bootstrapServiceConfiguration)
{
ArgumentUtility.CheckNotNull("bootstrapServiceConfiguration", bootstrapServiceConfiguration);

var assemblyLoader = new FilteringAssemblyLoader(new LoadAllAssemblyLoaderFilter());
var specificRootAssemblies = TypeDiscoveryConfiguration.Current.SpecificRootAssemblies;
var namedFinder = specificRootAssemblies.ByName.CreateRootAssemblyFinder(assemblyLoader);
var filePatternFinder = specificRootAssemblies.ByFile.CreateRootAssemblyFinder(assemblyLoader);
var rootAssemblyFinder = new CompositeRootAssemblyFinder(new IRootAssemblyFinder[] { namedFinder, filePatternFinder });

bootstrapServiceConfiguration.Register<IRootAssemblyFinder>(rootAssemblyFinder);
}
}
}
11 changes: 7 additions & 4 deletions Remotion/Core/Core/ServiceLocation/DefaultServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using Remotion.Logging;
using Remotion.Reflection;
using Remotion.Reflection.TypeDiscovery;
using Remotion.Utilities;

namespace Remotion.ServiceLocation
Expand Down Expand Up @@ -68,9 +67,13 @@ public sealed partial class DefaultServiceLocator : IServiceLocator

public static DefaultServiceLocator CreateWithBootstrappedServices ()
{
var defaultServiceLocator = new DefaultServiceLocator(new DefaultServiceConfigurationDiscoveryService(ContextAwareTypeUtility.GetTypeDiscoveryService()));

var bootstrapServiceLocatorEntries = SafeServiceLocator.BootstrapConfiguration.GetRegistrations();

var provider = new DefaultServiceLocatorProvider(new BootstrapServiceConfigurationDiscoveryService());
var bootstrapServiceLocator = provider.GetServiceLocator(bootstrapServiceLocatorEntries);
var typeDiscoveryService = bootstrapServiceLocator.GetInstance<ITypeDiscoveryService>();

var defaultServiceLocator = new DefaultServiceLocator(new DefaultServiceConfigurationDiscoveryService(typeDiscoveryService));
foreach (var serviceConfigurationEntry in bootstrapServiceLocatorEntries)
defaultServiceLocator.Register(serviceConfigurationEntry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// along with re-motion; if not, see http://www.gnu.org/licenses.
//
using System;
using System.ComponentModel.Design;
using NUnit.Framework;
using Remotion.Development.NUnit.UnitTesting;
using Remotion.Development.UnitTesting;
using Remotion.ExtensibleEnums.Infrastructure;
using Remotion.ExtensibleEnums.UnitTests.TestDomain;
using Remotion.Reflection;
using Remotion.ServiceLocation;

namespace Remotion.ExtensibleEnums.UnitTests.Infrastructure
Expand All @@ -30,12 +30,15 @@ public class ExtensibleEnumDefinitionCacheTest
{
private ExtensibleEnumDefinitionCache _cache;
private DefaultServiceLocator _serviceLocator;
private ITypeDiscoveryService _typeDiscoveryService;

[SetUp]
public void SetUp ()
{
_serviceLocator = DefaultServiceLocator.CreateWithBootstrappedServices();
_cache = new ExtensibleEnumDefinitionCache(new ExtensibleEnumValueDiscoveryService());
_typeDiscoveryService = _serviceLocator.GetInstance<ITypeDiscoveryService>();

_cache = new ExtensibleEnumDefinitionCache(new ExtensibleEnumValueDiscoveryService(_typeDiscoveryService));
}

[Test]
Expand All @@ -44,7 +47,7 @@ public void Initialization ()
Assert.That(_cache.ValueDiscoveryService, Is.InstanceOf(typeof(ExtensibleEnumValueDiscoveryService)));
Assert.That(
((ExtensibleEnumValueDiscoveryService)_cache.ValueDiscoveryService).TypeDiscoveryService,
Is.SameAs(ContextAwareTypeUtility.GetTypeDiscoveryService()));
Is.SameAs(_typeDiscoveryService));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ public class ExtensibleEnumValueDiscoveryService : IExtensibleEnumValueDiscovery

private readonly bool _excludeGlobalTypes = !AssemblyTypeCache.IsGacAssembly(typeof(ExtensibleEnum<>).Assembly);

public ExtensibleEnumValueDiscoveryService ()
{
_typeDiscoveryService = ContextAwareTypeUtility.GetTypeDiscoveryService();
}

protected ExtensibleEnumValueDiscoveryService (ITypeDiscoveryService typeDiscoveryService)
public ExtensibleEnumValueDiscoveryService (ITypeDiscoveryService typeDiscoveryService)
{
ArgumentUtility.CheckNotNull("typeDiscoveryService", typeDiscoveryService);

Expand Down
12 changes: 8 additions & 4 deletions Remotion/Core/ServiceLocation.PerformanceTestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@
// along with re-motion; if not, see http://www.gnu.org/licenses.
//
using System;
using System.ComponentModel.Design;
using System.Linq;
using log4net.Config;
using Remotion.Reflection;
using Remotion.ServiceLocation;
using Remotion.Utilities;

namespace Core.ServiceLocation.PerformanceTestConsole
namespace Remotion.ServiceLocation.PerformanceTestConsole
{
internal static class Program
{
private static void Main (string[] args)
{
Console.WriteLine("{0} - Application started", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss,fff"));
XmlConfigurator.Configure();
var typeDiscoveryService = ContextAwareTypeUtility.GetTypeDiscoveryService();

var bootstrapServiceLocatorEntries = SafeServiceLocator.BootstrapConfiguration.GetRegistrations();
var provider = new DefaultServiceLocatorProvider(new BootstrapServiceConfigurationDiscoveryService());
var bootstrapServiceLocator = provider.GetServiceLocator(bootstrapServiceLocatorEntries);
var typeDiscoveryService = bootstrapServiceLocator.GetInstance<ITypeDiscoveryService>();

var domainObjectType = Type.GetType("Remotion.Data.DomainObjects.DomainObject, Remotion.Data.DomainObjects", true, false);
typeDiscoveryService.GetTypes(domainObjectType, false);
typeDiscoveryService.GetTypes(domainObjectType, false);
Expand Down

This file was deleted.

Loading

0 comments on commit 2c93ad1

Please sign in to comment.