0

I wish for structuremap to scan my assemblies and register classes as singletons. I'd further restrict this to factories, services etc.

For now, however, the challenge is to mark the found registrations as singletons.

I've found out that one way is through conventions.

I've found an example that marks a single, specific registration as singleton, but I want to do it for all registrations.

The example code does not compile; first of all because the IsConcrete etc is not available.

Does anyone have a way forward?

using StructureMap;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace planner_gui
{
    public class SingletonConvention : IRegistrationConvention
    {
        public void Process(Type t, Registry registry)
        {
            if (!(t.IsConcrete()) || !(t.CanBeCreated()) ) return;

            registry.For( ...  ).Singleton().Use(t);
        }
    }

    public class GuiRegistry : Registry
    {
        public GuiRegistry()
        {
            Scan(x =>
            {
                x.AssemblyContainingType<IExecutionContext>();

                x.With(new SingletonConvention());
            });
        }
    }
}
DLeh
  • 23,806
  • 16
  • 84
  • 128
Anders Juul
  • 2,407
  • 3
  • 34
  • 56

1 Answers1

0

IsConcrete() method is an extension method in StructureMap.TypeRules namespace so adding

using StructureMap.TypeRules

would do the trick.

LetMeCodeThis
  • 591
  • 6
  • 10