StructureMap自动注册使用扫描泛型类型 [英] StructureMap Auto registration for generic types using Scan

查看:471
本文介绍了StructureMap自动注册使用扫描泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口:

IRepository<T> where T : IEntity

而即时通讯使用一些假信息库实现,只是返回任何旧的数据敲了我的UI IM。

while im knocking up my UI im using some fake repository implementations that just return any old data.

他们是这样的:

public class FakeClientRepository : IRepository<Client>

目前,即时通讯做这样的:

At the moment im doing this:

ForRequestedType<IRepository<Client>>()
   .TheDefaultIsConcreteType<FakeRepositories.FakeClientRepository>();

但时间为我所有IEntities负载。是否有可能使用扫描到自动注册我的都是假资料库为其各自IRepository?

but loads of times for all my IEntities. Is it possible to use Scan to auto register all my fake repositories for its respective IRepository?

编辑:这是据我得到的,但我得到的错误说法请求类型心不是注册:(

this is as far as I got, but i get errors saying the requested type isnt registered :(

Scan(x =>
{
    x.TheCallingAssembly();
    x.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
    x.AddAllTypesOf(typeof(IRepository<>));
    x.WithDefaultConventions();
});

感谢

安德鲁

推荐答案

还有就是要做到这一点更简单的方法。详情请参阅此博客中:<一href=\"http://lostechies.com/jimmybogard/2009/12/18/advanced-structuremap-connecting-implementations-to-open-generic-types/\"相对=nofollow>高级StructureMap:连接实现开放泛型类型

There is an easier way to do this. Please see this blog posting for details: Advanced StructureMap: connecting implementations to open generic types

public class HandlerRegistry : Registry
{
    public HandlerRegistry()
    {
        Scan(cfg =>
        {
            cfg.TheCallingAssembly();
            cfg.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
            cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
        });
    }
}

这样做,这样避免了创建自己的 ITypeScanner 或约定。

这篇关于StructureMap自动注册使用扫描泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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