2009-07-17 58 views
5

如何在结构化地图中注册通用接口的所有实例?如何在StructureMap中注册通用接口

我知道如何为没有通用接口做到这一点:

internal class MVCDemoRegistry : Registry 
    { 
     public MVCDemoRegistry() 
     { 
      Scan(x => 
      { 
       x.Assembly("MVCDemo"); 
       x.Assembly("MVCDemo.Infrastructure"); 
       x.Assembly("MVCDemo.Services"); 

       x.AddAllTypesOf<ISupplyView>(); 
      }); 
     } 
    } 

回答

10

我会的东西去像

// in IToaster.cs 
public interface IToaster<T> {} 

// in your StructureMap registry 
Scan(x => 
{ 
    x.Assembly("MVCDemo"); 
    x.Assembly("MVCDemo.Infrastructure"); 
    x.Assembly("MVCDemo.Services"); 

    x.AddAllTypesOf(typeof(IToaster<>)) 
}); 

这里的关键是,这种方法使用的非泛型重载AddAllTypesOf()。否则,这确实会变成一个粘性小部件。

请参阅本SO线程围绕这些问题好好讨论:StructureMap Auto registration for generic types using Scan

这应该做的伎俩,除非有一些关于你的方法,我很想念 - 随时若有更新。

相关问题