Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap, scan assemblies and scoping

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>();
    });
}
like image 650
mathieu Avatar asked Dec 13 '25 19:12

mathieu


2 Answers

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>();
    });
});
like image 199
Mark Seemann Avatar answered Dec 16 '25 10:12

Mark Seemann


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
}
like image 22
Rogeclub Avatar answered Dec 16 '25 11:12

Rogeclub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!