我如何使用StructureMap使用扫描与一般的未封闭类型A"贪婪"构造函数 [英] How do I use StructureMap with generic unclosed types using Scan with a "greedy" constructor

查看:151
本文介绍了我如何使用StructureMap使用扫描与一般的未封闭类型A"贪婪"构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各种堆栈溢出的问题和博客之间有文档的开放泛型和StructureMap的话题pretty合理费用。不幸的是,我必须使用扫描与具有贪婪的构造还没有工作,一类实现沿执行配置失去了一些东西作为我的尝试。

Between various Stack Overflow questions and blog posts there is a pretty reasonable amount of documentation on the topic of open generics and StructureMap. Unfortunately, I must be missing something as my attempts at using scan to perform the configuration along with a class implementation that has a "greedy" constructor have yet work.

我要StructureMap通过它实现的接口引用抢下面类的一个实例。 ToCsvService 存在于一个名为基础设施未引用组装。 IToCsvService 存在于所谓的核心引用的程序集。如你看到的
ToCsvService 有贪婪的构造。

I want StructureMap to grab an instance of the below class via references to its implemented interface. ToCsvService exists in an unreferenced assembly called Infrastructure. IToCsvService exists in a referenced assembly called Core. As you can see ToCsvService has a "greedy" constructor.

public class ToCsvService<TSource> : IToCsvService<TSource>
{
    public ToCsvService(ICollection<TSource> collection)
    {
    }
}

我让StructureMap知道 ToCsvService 通过 ConnectImplementationsToTypesClosing 方法。

ObjectFactory.Initialize(cfg =>
{
    cfg.Scan(scan =>
    {
        scan.Assembly("Infrastructure");
        scan.WithDefaultConventions();

        // even with this call StructureMap cannot use ToCsvService
        // instance of IToCsvService (though wouldn't expect it to)
        scan.ConnectImplementationsToTypesClosing
            (typeof(IToCsvService<>));
    });
});

ObjectFactory.WhatDoIHave()方法似乎StructureMap意识到 ToCsvService

From the ObjectFactory.WhatDoIHave() method it appears that StructureMap is aware of ToCsvService.


PluginType                            Name                                                                                                            Description                                                                                                                          
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IToCsvService`1 (IToCsvService`1)                                                                                                                                                                                                                                      
Scoped as:  Transient                                                                                                                                                                                                                                                                                  
                                      6202a7ee-89a4-4edd-831d-4867b7dd1a7e                                                                            Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 
                                      Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null   Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                   

然而,当我指定 IToCsvService&LT; CustomerViewModel&GT; 在控制器的构造函数,它抛出该异常

However when I specify IToCsvService<CustomerViewModel> in a Controller constructor it throws the exception

StructureMap异常code:202
  对于PluginFamily Core.Services.IToCsvService`1 [UI.Models.MachineForm,用户界面​​,版本= 1.0.0.0,文化=中立,公钥=空],核心,版本= 1.0.0.0,文化=中性的定义没有默认实例公钥=空

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily Core.Services.IToCsvService`1[[UI.Models.MachineForm, UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

我想,这是因为StructureMap不知道该怎么到手的贪婪 ToCsvService 构造函数。有什么方法,我可以让StructureMap能发挥好这个构造?我需要从一个构造器开关,只需设置集合属性实例化之后?

I imagine that this is because StructureMap has no idea what to hand the "greedy" ToCsvService constructor. Is there someway that I can make StructureMap able to play nice with this constructor? Do I need to switch from a constructor and just set the collection property after instantiation?

问题 Structuremap和泛型类型详细信息有些我试图做的,但不使用扫描这样做。在<一个href=\"http://stackoverflow.com/questions/2644941/structuremap-and-generic-types/2645221#2645221\">answer由约书亚那根提供利用有关(X)。用(Y)配置,如果我不扫描组件,因为我没有一个参考可能工作 ToCsvService

The question Structuremap and generic types details somewhat I am trying to do but does not utilize a scan to do so. The answer provided by Joshua Flanagan utilizes the For(x).Use(y) configuration, which might work if I wasn't scanning the assembly because I don't have a reference ToCsvService.

修改

我想看看,如果使用不同的机制,让StructureMap识别 ToCsvService&LT的实例;&GT; 将产生效果。它改变了什么报告 ObjectFactory.WhatDoIHave()并抛出一个不同的异常。下面是使用的例子 AddAllTypesOf

I wanted to see if using a different mechanism to let StructureMap identify instances of ToCsvService<> would have an effect. It changes what's reported in ObjectFactory.WhatDoIHave() and throws a different exception. Here's an example of using AddAllTypesOf.

ObjectFactory.Initialize(cfg =>
{
    cfg.Scan(scan =>
    {
        scan.Assembly("Infrastructure");
        scan.WithDefaultConventions();

        scan.AddAllTypesOf(typeof(IToCsvService<>));
    });
});

使用转储从以上 ObjectFactory.WhatDoIHave后()


PluginType                           Name                                                                                                                                 Description                                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IToCsvService`1 (IToCsvService`1)    Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                        Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                   
Scoped as:  Transient

                                     Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                        Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                   
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IToCsvService`1 (IToCsvService`1)    Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                        Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                   
Scoped as:  Transient

                                     Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null                        Configured Instance of Infrastructure.Services.ToCsvService`1, Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

使用这个配置我抛出此异常:

With this configuration I throw this exception:

StructureMap异常code:202
  对于PluginFamily System.Collections.Generic.ICollection`1 [UI.Models.MachineForm,用户界面​​,版本= 1.0.0.0,文化=中立,公钥=空],mscorlib程序,版本= 4.0.0.0,文化定义没有默认实例=中性公钥= b77a5c561934e089

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily System.Collections.Generic.ICollection`1[[UI.Models.MachineForm, UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

要我的异常指示StructureMap知道它需要一个的ICollection&LT; MachineForm&GT; 实例 ToCsvService 但不知道从哪里得到它。肚里从下面吉米的评论有关使用StructureMap和的构造setter注入。不过,这似乎并不可能没有添加明确提及的基础设施组件。

To me the exception indicates that StructureMap knows it needs an ICollection<MachineForm> to instantiate ToCsvService but does not know where to get it from. Which goes to Jimmy's comment from below about using StructureMap and Constructor setter injection. However, this doesn't seem possible without adding an explicit reference to the Infrastructure assembly.

  • Structuremap and generic types
  • StructureMap setter injection in open generic type?
  • StructureMap Auto registration for generic types using Scan
  • Advanced StructureMap: connecting implementations to open generic types
  • Advanced StructureMap: custom registration conventions for partially closed types
  • Open Generic Types in StructureMap

推荐答案

是否有IToCsvService任何具体的实现?或者只开放式ToCsvService?

Are there any concrete implementations of IToCsvService? Or just the open type ToCsvService?

ConnectImplementationsToTypesClosing是像一个具体CustomerViewModelToCsvService连接到IToCsvService&LT;>。如果你想连接公开课开放的接口,你需要:

ConnectImplementationsToTypesClosing is for connecting something like a concrete CustomerViewModelToCsvService to IToCsvService<>. If you want to connect open classes to open interfaces, you'll need:

For(typeof(IToCsvService<>)).Use(typeof(ToCsvService<>));

在这里,我的开放接口类型连接到公开课的类型。

Here I'm connecting the open interface type to the open class type.

这篇关于我如何使用StructureMap使用扫描与一般的未封闭类型A&QUOT;贪婪&QUOT;构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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