Skip to content

Instantly share code, notes, and snippets.

@hazzik
Created August 6, 2012 21:49
Show Gist options
  • Save hazzik/3278770 to your computer and use it in GitHub Desktop.
Save hazzik/3278770 to your computer and use it in GitHub Desktop.
public class IoCAwareScanner : DefaultModelMetadataScanner
{
private readonly Action<Type, Type> registerAsTransient;
internal static readonly Type ModelMetadataConfigurationType = typeof(IModelMetadataConfiguration);
protected IoCAwareScanner(Action<Type, Type> registerAsTransient)
{
this.registerAsTransient = registerAsTransient;
}
public override IEnumerable<IModelMetadataConfiguration> GetConfigurations()
{
RegisterTypes();
return DependencyResolver.Current.GetServices<IModelMetadataConfiguration>();
}
private void RegisterTypes()
{
IEnumerable<Assembly> referencedAssemblies = GetReferencedAssemblies();
var configurationTypes = FilterConfigurationTypes(referencedAssemblies.SelectMany(a => a.GetLoadableTypes()));
foreach (Type type in configurationTypes)
{
registerAsTransient(ModelMetadataConfigurationType, type);
}
}
}
public abstract class IoCAwareScanner : DefaultModelMetadataScanner
{
internal static readonly Type ModelMetadataConfigurationType = typeof(IModelMetadataConfiguration);
public override IEnumerable<IModelMetadataConfiguration> GetConfigurations()
{
RegisterTypes();
return DependencyResolver.Current.GetServices<IModelMetadataConfiguration>();
}
protected abstract void RegisterAsTransient(Type implementationType, Type serviceType = null);
private void RegisterTypes()
{
IEnumerable<Assembly> referencedAssemblies = GetReferencedAssemblies();
var configurationTypes = FilterConfigurationTypes(referencedAssemblies.SelectMany(a => a.GetLoadableTypes()));
foreach (Type type in configurationTypes)
{
RegisterAsTransient(ModelMetadataConfigurationType, type);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment