C# 如何使用 StructureMap 通过代码定义默认构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/289512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a default constructor by code using StructureMap?
提问by Cyril Bioley
I can't figure out how to define the default constructor (when it exists overloads) for a type in StructureMap (version 2.5) by code.
我无法弄清楚如何通过代码为 StructureMap(2.5 版)中的类型定义默认构造函数(当它存在重载时)。
I want to get an instance of a service and the container has to inject a Linq2Sql data context instance into it.
我想获取一个服务实例,并且容器必须向其中注入一个 Linq2Sql 数据上下文实例。
I wrote this in my 'bootstrapper' method :
我在我的“引导程序”方法中写了这个:
ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>();
When I run my app, I got this error :
当我运行我的应用程序时,出现此错误:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily MyNamespace.Data.SqlRepository.MyDataContext, MyNamespace.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
StructureMap 异常代码:202
没有为 PluginFamily MyNamespace.Data.SqlRepository.MyDataContext、MyNamespace.Data、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null 定义默认实例
If I comment out all Linq2Sql generated contructors that I don't need, it works fine.
如果我注释掉所有我不需要的 Linq2Sql 生成的构造函数,它就可以正常工作。
Update : Oh, and I forgot to say that I would not use the [StructureMap.DefaultConstructor]
attribute.
更新:哦,我忘了说我不会使用该[StructureMap.DefaultConstructor]
属性。
采纳答案by PeterFromCologne
You can specify a constructor with the ConstructedBy(). Please try this:
您可以使用 ConstructedBy() 指定构造函数。请试试这个:
ForRequestedType<MyDataContext>().TheDefault.
Is.ConstructedBy(() => new MyDataContext());
This worked for me.
这对我有用。
回答by Ed Blackburn
I'm not sure how / if it can be done with the fluent interface / internal DSL. You can however use an attribute, if you're not fussed about polluting your domain?
我不确定如何/是否可以使用流畅的接口/内部 DSL 来完成。但是,如果您不担心污染域,您可以使用属性?
Tag your preferred constructor with [DefaultConstructor] StructureMap defaults to the greediest constructor by convention (constructor with the most parameters).
使用 [DefaultConstructor] 标记您的首选构造函数 StructureMap 按照约定默认为最贪婪的构造函数(具有最多参数的构造函数)。
回答by njappboy
I'm assuming you'd also need to set the object lifetime (InstanceScope) if you are using Linq2Sql. I'd suggest using this code because it give you a little more flexibility.
如果您使用的是 Linq2Sql,我假设您还需要设置对象生存期 (InstanceScope)。我建议使用此代码,因为它为您提供了更多的灵活性。
ForRequestedType< MyDataContext >()
.CacheBy( InstanceScope.PerRequest )
.TheDefault.Is.OfConcreteType< MyDataContext >()
SelectConstructor< MyDataContext >( () => new MyDataContext());
With this code you can also further inject interfaces definitions into the MyDataContext constructor like this
使用此代码,您还可以像这样将接口定义进一步注入 MyDataContext 构造函数中
SelectConstructor< MyDataContext >( () => new MyDataContext((IDatabaseFactory)null ));
Just remember to define the concrete type with StructureMap for your IDatabaseFactory instance.
请记住使用 StructureMap 为您的 IDatabaseFactory 实例定义具体类型。