-
-
Notifications
You must be signed in to change notification settings - Fork 120
Option to disable assembly scanning when type missing from registry #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
@@ -298,6 +298,32 @@ public void Register_AssemblyWithFunc_CallsAssemblyScanner() | |||
} | |||
|
|||
[Fact] | |||
public void Register_Assembly_CallsAssemblyScannerWhenAssemblyScanningEnabled() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the convention used to name the unit tests in this project so I will welcome suggestions if you can think of a better name ;)
Mmm appveyor is complaining that the code coverage is too low :( |
That is not your fault. It is caused by code being added prior to your PR.
I will review your PR ASAP :)
…On Mon, Feb 6, 2017 at 10:57 AM, Benjamin ***@***.***> wrote:
Mmm appveyor is complaining that the code coverage is too low :(
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#343 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA_HWbKufdVwyIyFmv8SlcBnNU6cPoXhks5rZu6PgaJpZM4L2fcP>
.
|
Hello, Is this pull request still relevant? It looks like Lightinject now has a slightly different signature on void Scan(Assembly assembly, IServiceRegistry serviceRegistry, Func<ILifetime> lifetime, Func<Type, Type, bool> shouldRegister, Func<Type, Type, string> serviceNameProvider); Maybe the What about the service name provider? Could this extensibility point be used to disable assembly scanning? Thanks! |
It's been a while since there has been activity in this conversation thread... That said, I think would still be useful to have an option to control the behaviour of LightInject when it comes to looking for ICompositionRoot implementations. Out of the box, the This behaviour is great IMHO when you get started and or are dealing with a small project. With this in mind, and having had a look at the LightInject source a little bit, I ended up: Creating a new public class ExcludingCompositionRootTypeExtractor : ITypeExtractor
{
private readonly string excludePattern;
private readonly ITypeExtractor compositionRootTypeExtractor;
/// <param name="compositionRootTypeExtractor"></param>
/// <param name="excludePattern">If that pattern is found in the filename, it is ignored.</param>
public ExcludingCompositionRootTypeExtractor(string excludePattern, ITypeExtractor compositionRootTypeExtractor)
{
this.excludePattern = excludePattern;
this.compositionRootTypeExtractor = compositionRootTypeExtractor;
}
public Type[] Execute(Assembly assembly)
{
// If the assembly matches the pattern don't try to extract composition roots
if (assembly.FullName.IndexOf(excludePattern, StringComparison.Ordinal) >= 0)
{
return Type.EmptyTypes;
}
return compositionRootTypeExtractor.Execute(assembly);
}
} Then I defined an extension method that can be called immediately after creating the container to place my public static IServiceContainer HackCompositionRootTypeExtractorPipeline(this ServiceContainer serviceContainer)
{
var concreteTypeExtractor = new CachedTypeExtractor(new ConcreteTypeExtractor());
var compositionRootTypeExtractor = new CachedTypeExtractor(new ExcludingCompositionRootTypeExtractor("Watchdog.Relational", new CompositionRootTypeExtractor(new CompositionRootAttributeExtractor())));
serviceContainer.CompositionRootTypeExtractor = compositionRootTypeExtractor;
serviceContainer.AssemblyScanner = new AssemblyScanner(concreteTypeExtractor,
serviceContainer.CompositionRootTypeExtractor,
serviceContainer.CompositionRootExecutor,
serviceContainer.GenericArgumentMapper);
return serviceContainer;
} And where the container is created you can call the hack method to change the composition root type extractor's behaviour: var container = new ServiceContainer(containerOptions).HackCompositionRootTypeExtractorPipeline();
@seesharper Would you be interested in seeing a pull request where a callback can be defined on the container options and when that callback exists an extra |
Follow up on discussion #103.
Proposed change: to have an option to disable the default behaviour that scans assemblies for
ICompositionRoot
implementation when a type is missing from the service registry.Having the option
ScanAssembliesWhenRegistrationNotFound
set to true by default doesn't change the current behaviour.