Cutting the fluff from Service registration with StructureMap revisited
This is just a quick update of an older post of mine. Since StructureMap’s convention API has changed quite a bit, here is the updated version of the code used in the post using the new APIs introduced in StructureMap 2.5.4.
The new code is actually easier. It should look something like this . . . .
public class ServicesAreSingletonsAndProxies : IRegistrationConvention
{
#region IRegistrationConvention Members
public void Process(Type type, Registry registry)
{
if (!type.IsConcrete() || !IsService(type) || !Constructor.HasConstructors(type))
{
return;
}
Type pluginType = FindPluginType(type);
if (pluginType == null)
{
return;
}
registry
.For(pluginType)
.Singleton()
.Use(new ConfiguredInstance(type)
{
Interceptor = new DynamicProxyInterceptor(pluginType)
});
}
#endregion
private static bool IsService(Type type)
{
return type.Name.EndsWith("Service");
}
private static Type FindPluginType(Type concreteType)
{
string interfaceName = "I" + concreteType.Name;
return concreteType
.GetInterfaces()
.Where(t => string.Equals(t.Name, interfaceName, StringComparison.Ordinal))
.FirstOrDefault();
}
}

