如何定义使用StructureMap由code默认构造函数? [英] How to define a default constructor by code using StructureMap?

查看:204
本文介绍了如何定义使用StructureMap由code默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何定义默认构造函数由code(当它存在过载)在StructureMap(2.5版)的一种类型。

我希望得到一个服务的一个实例和容器必须注入LINQ2SQL数据上下文实例进去了。

我在引导程序的方法写的:

<$p$p><$c$c>ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>();

当我跑我的应用程序,我得到这个错误:


  

StructureMap异常code:202结果
  对于PluginFamily MyNamespace.Data.SqlRepository.MyDataContext,MyNamespace.Data,版本= 1.0.0.0,文化=中立,公钥=空

定义没有默认实例

如果我注释掉所有LINQ2SQL生成我不需要contructors,它工作正常。

更新:哦,我忘了说,我不会使用 [StructureMap.DefaultConstructor] 属性


解决方案

您可以用ConstructedBy指定一个构造函数()。请试试这个:

  ForRequestedType&LT; MyDataContext&GT;()TheDefault。
Is.ConstructedBy(()=&gt;新建MyDataContext());

这为我工作。

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.

I want to get an instance of a service and the container has to inject a Linq2Sql data context instance into it.

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

If I comment out all Linq2Sql generated contructors that I don't need, it works fine.

Update : Oh, and I forgot to say that I would not use the [StructureMap.DefaultConstructor] attribute.

解决方案

You can specify a constructor with the ConstructedBy(). Please try this:

ForRequestedType<MyDataContext>().TheDefault.
Is.ConstructedBy(() => new MyDataContext());

This worked for me.

这篇关于如何定义使用StructureMap由code默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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