views:

378

answers:

2

How can I add some scoping when I scan my assemblies ? Google doesn't seem quite happy with "structuremap scan cacheby" :/

ObjectFactory.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType(typeof(IRepository<>));
        x.With<DefaultConventionScanner>();
    });
}
+1  A: 

Hi,

The way I got around this was to build a custom convention scanner:

public class CustomScanner : ITypeScanner
{
    #region ITypeScanner Members

    public void Process(Type type, PluginGraph graph)
    {                                   
        graph.AddType(type);
        var family = graph.FindFamily(type);
        family.AddType(type);
        family.SetScopeTo(InstanceScope.Hybrid);
    }

    #endregion
}
Rogeclub
A: 

Here's a way to make it work with the newer IRegistrationConvention API:

public class SingletonConvention : IRegistrationConvention
{
    #region IRegistrationConvention Members

    public void Process(Type type, Registry registry)
    {
        registry.For(type).Singleton();
    }

    #endregion
}

It can be used like this:

container.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType<Foo>();
        x.AddAllTypesOf<IFoo>();
        x.Convention<SingletonConvention>();
    });
});
Mark Seemann