结构图 - 我不想使用最贪婪的构造函数! [英] Structure Map - I dont want to use the greediest constructor!

查看:21
本文介绍了结构图 - 我不想使用最贪婪的构造函数!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to configure the NCommon NHRepository in my project with Structure Map. How do I stop it from choosing the greediest constructor?

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

    public NHRepository () {}


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

    ...
}

我的结构图配置

ForRequestedType(typeof (IRepository<>))
                .TheDefaultIsConcreteType(typeof(NHRepository<>))

干杯杰克

推荐答案

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

You can set the [DefaultConstructor] Attribute for the constructor you wish as a default. In your case, setting it on the NHRepository() constructor would make it the default constuctor for StructureMap to initialize.

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

Update: well, in the latest version of StructureMap, using .NET 3.5 you can also specify it using the SelectConstructor method:

var container = new Container(x =>
{
  x.SelectConstructor<NHRepository>(()=>new NHRepository());
});

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

Finally, I'm sure you would be able to define it in the XML configuration of StructureMap, but I haven't used that. You could do a little search on it. For more information on the above method, see: http://structuremap.sourceforge.net/ConstructorAndSetterInjection.htm#section3

这篇关于结构图 - 我不想使用最贪婪的构造函数!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆