结构图 - 我不想使用最贪婪的构造函数!

sup*_*cal 7 structuremap

我正在尝试使用Structure Map在我的项目中配置NCommon NHRepository.如何阻止它选择最贪婪的构造函数?

 public class NHRepository<TEntity> : RepositoryBase<TEntity>
 {

    public NHRepository () {}


    public NHRepository(ISession session)
    {
        _privateSession = session; 
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

我的结构图配置

ForRequestedType(typeof (IRepository<>))
                .TheDefaultIsConcreteType(typeof(NHRepository<>))
Run Code Online (Sandbox Code Playgroud)

干杯杰克

Raz*_*zie 8

您可以将所需[DefaultConstructor]构造函数的属性设置为默认值.在您的情况下,在NHRepository()构造函数上设置它将使其成为StructureMap初始化的默认构造函数.

更新:嗯,在最新版本的StructureMap中,使用.NET 3.5,您也可以使用SelectConstructor方法指定它:

var container = new Container(x =>
{
  x.SelectConstructor<NHRepository>(()=>new NHRepository());
});
Run Code Online (Sandbox Code Playgroud)

最后,我确信您可以在StructureMap的XML配置中定义它,但我还没有使用它.你可以做一点搜索.有关上述方法的更多信息,请参阅:http://structuremap.sourceforge.net/ConstructorAndSetterInjection.htm#section3