Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public class FakeClass
_ => new FakeOptions(Path.Combine(AppContext.BaseDirectory,
Path.Combine(AppContext.BaseDirectory, "Fixtures/Dynamic"))));
serviceCollection.AddSingleton(_ => syntaxTreeResolverMock.Object);
serviceCollection.AddAppModel();
serviceCollection.AddAppTypeResolver();
serviceCollection.AddAppModelIfNotExist();
serviceCollection.AddAppTypeResolverIfNotExist();
serviceCollection.AddSingleton<CompilerFactory>();
serviceCollection.AddSingleton<ICompilerFactory>(s => s.GetRequiredService<CompilerFactory>());
serviceCollection.AddSingleton<DynamicallyCompiledAssemblyResolver>();
Expand Down Expand Up @@ -126,8 +126,8 @@ public class FakeClass
_ => new FakeOptions(Path.Combine(AppContext.BaseDirectory,
Path.Combine(AppContext.BaseDirectory, "Fixtures/Dynamic"))));
serviceCollection.AddSingleton(_ => syntaxTreeResolverMock.Object);
serviceCollection.AddAppModel();
serviceCollection.AddAppTypeResolver();
serviceCollection.AddAppModelIfNotExist();
serviceCollection.AddAppTypeResolverIfNotExist();
serviceCollection.AddSingleton<CompilerFactory>();
serviceCollection.AddSingleton<ICompilerFactory>(s => s.GetRequiredService<CompilerFactory>());
serviceCollection.AddSingleton<DynamicallyCompiledAssemblyResolver>();
Expand Down Expand Up @@ -184,8 +184,8 @@ public class FakeClass
_ => new FakeOptions(Path.Combine(AppContext.BaseDirectory,
Path.Combine(AppContext.BaseDirectory, "Fixtures/Dynamic"))));
serviceCollection.AddSingleton(_ => syntaxTreeResolverMock.Object);
serviceCollection.AddAppModel();
serviceCollection.AddAppTypeResolver();
serviceCollection.AddAppModelIfNotExist();
serviceCollection.AddAppTypeResolverIfNotExist();
serviceCollection.AddSingleton<CompilerFactory>();
serviceCollection.AddSingleton<ICompilerFactory>(s => s.GetRequiredService<CompilerFactory>());
serviceCollection.AddSingleton<DynamicallyCompiledAssemblyResolver>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ public static class ServiceCollectionExtensions
/// <param name="assembly">The assembly loading apps from</param>
public static IServiceCollection AddAppsFromAssembly(this IServiceCollection services, Assembly assembly)
{
// We make sure we only add AppModel services once
if (!services.Any(n => n.ImplementationType == typeof(AppModelImpl)))
services
.AddAppModel()
.AddAppTypeResolver();

services.AddSingleton<IAssemblyResolver>(new AssemblyResolver(assembly));
return services;
return services
.AddAppModelIfNotExist()
.AddAppTypeResolverIfNotExist()
.AddSingleton<IAssemblyResolver>(new AssemblyResolver(assembly));
}

/// <summary>
Expand All @@ -33,11 +29,9 @@ public static IServiceCollection AddAppsFromAssembly(this IServiceCollection ser
/// <param name="type">The type of the app to add</param>
public static IServiceCollection AddAppFromType(this IServiceCollection services, Type type)
{
// We make sure we only add AppModel services once
if (services.All(n => n.ImplementationType != typeof(AppModelImpl)))
services.AddAppModel();

return services.AddSingleton<IAppTypeResolver>(new SingleAppResolver(type));
return services
.AddAppModelIfNotExist()
.AddSingleton<IAppTypeResolver>(new SingleAppResolver(type));
}

/// <summary>
Expand All @@ -47,17 +41,15 @@ public static IServiceCollection AddAppFromType(this IServiceCollection services
public static IServiceCollection AddAppsFromSource(this IServiceCollection services)
{
// We make sure we only add AppModel services once
if (services.All(n => n.ImplementationType != typeof(AppModelImpl)))
services
.AddAppModel()
.AddAppTypeResolver();


services
.AddAppModelIfNotExist()
.AddAppTypeResolverIfNotExist()
.AddSingleton<CompilerFactory>()
.AddSingleton<ICompilerFactory>(s => s.GetRequiredService<CompilerFactory>())
.AddSingleton<SyntaxTreeResolver>()
.AddSingleton<ISyntaxTreeResolver>(s => s.GetRequiredService<SyntaxTreeResolver>());

// We need to compile it here so we can dynamically add the service providers
var assemblyResolver =
ActivatorUtilities.CreateInstance<DynamicallyCompiledAssemblyResolver>(services.BuildServiceProvider());
Expand All @@ -84,8 +76,12 @@ private static IServiceCollection RegisterDynamicFunctions(this IServiceCollecti
return services;
}

internal static IServiceCollection AddAppModel(this IServiceCollection services)
internal static IServiceCollection AddAppModelIfNotExist(this IServiceCollection services)
{
// Check if we already registered
if (services.Any(n => n.ImplementationType == typeof(AppModelImpl)))
return services;

services
.AddSingleton<AppModelImpl>()
.AddSingleton<IAppModel>(s => s.GetRequiredService<AppModelImpl>())
Expand All @@ -97,8 +93,12 @@ internal static IServiceCollection AddAppModel(this IServiceCollection services)
return services;
}

internal static IServiceCollection AddAppTypeResolver(this IServiceCollection services)
internal static IServiceCollection AddAppTypeResolverIfNotExist(this IServiceCollection services)
{
// Check if we already registered
if (services.Any(n => n.ImplementationType == typeof(AppTypeResolver)))
return services;

services
.AddSingleton<AppTypeResolver>()
.AddSingleton<IAppTypeResolver>(s => s.GetRequiredService<AppTypeResolver>());
Expand Down