StructureMap, Assemblies, and Scope

How can I add some areas when I view my assemblies? Google doesn't seem quite happy with "cachemap scanbase": /

ObjectFactory.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType(typeof(IRepository<>));
        x.With<DefaultConventionScanner>();
    });
}
+3
source share
2 answers

The way I communicated with this was to create a custom conditional 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
}
+2
source

Here you can do the work with the new 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 as follows:

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

Source: https://habr.com/ru/post/1703520/


All Articles