thedersen/UnityConfiguration — Issue #3: Add Additional Extension Points
Author: DamianReeves
Created: 2011-10-20 14:54:19+00:00
State: open

The current IAssemblyScanner implementation and IUnityRegistry implementation are closed off in such a manner that you cannot define your own assembly scanning strategies without completely reimplementing IAssemblyScanner, IUnityRegistry (you can derive from UnityRegistry and hide the Scan implementation), and reimplementing the Configure extension method.

I for example would like to scan for types which are internal to my included assemblies, but unless I reimplement IAssemblyScanner I am limited to the types available by Assembly.GetExportedTypes(). I think some sprinking of protected and virtual would allow easier extensibility.

Without this I have to do things like this

public interface IAssemblyScanner: UnityConfiguration.IAssemblyScanner {
void Scan(IUnityRegistry registry);
}

public class UnityRegistry: UnityConfiguration.UnityRegistry {
private readonly Func _scannerFactory;
public UnityRegistry():this(null) {}

public UnityRegistry(Func scannerFactory) {
if (scannerFactory == null) {
scannerFactory = () => new AssemblyScanner();
}
_scannerFactory = scannerFactory;
}

public void Scan(Action action) {
var assemblyScanner = _scannerFactory();
action(assemblyScanner);
assemblyScanner.Exclude(t => t == GetType());
assemblyScanner.Scan(this);
}
}

public class AssemblyScanner : IAssemblyScanner {
private readonly List explicitIncludes = new List();
//IAsembly scanner implementation

protected virtual IEnumerable GetExportedTypes() {
return assemblies.SelectMany(a => a.GetExportedTypes()).Union(explicitIncludes).Where(t => filter.Matches(t));
}
}