IoC 容器可以自动注册所有内容(非通用),无需配置(程序集到程序集)

发布于 2024-10-08 01:14:43 字数 391 浏览 0 评论 0原文

我的想法是,我有一个带有大量接口的核心项目,还有带有实现的数据和服务项目(一切都是一对一),例如:

Core { IFooRepo, IBarRepo, IFooService, IBarService}
Data {FooRepo: IFooRepo, BarRepo : IBarRepo}
Service {FooService : IFooService, BarService : IBarService}

所以我想要

register(Core, Data);
register(Core, Service);

有很多 IoC 容器之类的东西,但我不知道他们中的哪一个可以做到这一点,或者更接近这个解决方案,有人知道吗?

the idea is that I have a Core project with lots of interfaces, also Data and Service project with implementations (everything 1-to-1), e.g.:

Core { IFooRepo, IBarRepo, IFooService, IBarService}
Data {FooRepo: IFooRepo, BarRepo : IBarRepo}
Service {FooService : IFooService, BarService : IBarService}

so I would like something like

register(Core, Data);
register(Core, Service);

there are lots of IoC containers and I don't know which one of them can do this, or is closer to this solution, anybody knows ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玻璃人 2024-10-15 01:14:43

您正在谈论自动注册。许多 IoC 容器都支持这一点。

结构图
http://structuralmap.net/structuralmap/ScanningAssemblies.htm

温莎城堡(参见第二部分底部文章页面)
http://www.code-magazine.com/article.aspx?quickid= 0906051

Autofac
http://code.google.com/p/autofac/wiki/Scanning

忍者
看起来你可以通过 Kernel.Scan() 来完成,尽管我找不到文档。 (服务器不可用。)
如何在不引用的情况下使用 Ninject Conventions 扩展程序集(或其中的类型)

最后我发现,Unity 不支持自动注册,尽管最近的版本可能有所改变。

更新:感谢毛里西奥注意到我错误地将所需的功能识别为自动接线。更正并更新了链接。

You are talking about auto-registration. Many IoC containers support this.

StructureMap
http://structuremap.net/structuremap/ScanningAssemblies.htm

Castle Windsor (see bottom of 2nd page of the article)
http://www.code-magazine.com/article.aspx?quickid=0906051

Autofac
http://code.google.com/p/autofac/wiki/Scanning

Ninject
Looks like you can do it via Kernel.Scan(), though I couldn't find docs. (Server was unavailable.)
How to use Ninject Conventions extension without referencing Assembly (or Types within it)

Last I looked, Unity did not support auto-registration, though that may have changed with a recent release.

UPDATE: Thanks to Mauricio for noticing that I incorrectly identified the desired feature as auto-wiring. Corrected and updated the links.

前事休说 2024-10-15 01:14:43

在 Autofac 中,实现此目的的最简单方法是:

var builder = new ContainerBuilder();

var data = typeof(BarRepo).Assembly();
builder.RegisterAssemblyTypes(data).AsImplementedInterfaces();

var service = typeof(BarService).Assembly();
builder.RegisterAssemblyTypes(service).AsImplementedInterfactes();

var container = builder.Build();

现在不会对来自核心程序集的服务接口进行选择。如果这真的很重要(可能不应该),那么按照以下方式改变上述注册:

var core = typeof(IBarRepo).Assembly();

builder.RegisterAssemblyTypes(data)
    .As(t => t.GetInterfaces()
         .Where(i => i.Assembly == core)
         .Select(i => new TypedService(i)));

干杯,
缺口

In Autofac the simplest way to achieve this is:

var builder = new ContainerBuilder();

var data = typeof(BarRepo).Assembly();
builder.RegisterAssemblyTypes(data).AsImplementedInterfaces();

var service = typeof(BarService).Assembly();
builder.RegisterAssemblyTypes(service).AsImplementedInterfactes();

var container = builder.Build();

Now this will not be selective about the service interfaces being from the Core assembly. If this really matters (it probably shouldn't) then vary the above registrations along these lines:

var core = typeof(IBarRepo).Assembly();

builder.RegisterAssemblyTypes(data)
    .As(t => t.GetInterfaces()
         .Where(i => i.Assembly == core)
         .Select(i => new TypedService(i)));

Cheers,
Nick

秋千易 2024-10-15 01:14:43

Windsor 使您可以轻松地将类注册为它们公开的接口,可以全部注册,也可以有选择地注册。 (请参阅文档)。

它不支持您的场景(过滤实现的接口以仅包含来自特定程序集的接口),但是(对于 Windsor 中的所有内容)您可以使用一个挂钩轻松实现这一点。

container.Register(
   AllTypes.FromAssemblyContaining<SomeClass>()
      WithService.Select((type, @base) =>
         type.GetAllInterfaces()
             .Where(i => i.Assembly == yourInterfacesAssembly)))
);

Windsor lets you easily register classes as interfaces they expose, either all of them or, selectively. (see the documentation).

It does not have OOTB support for your scenario (filtering implemented interfaces to only include those from specific assembly) but (as for everything in Windsor) there's a hook you can use to easily have that.

container.Register(
   AllTypes.FromAssemblyContaining<SomeClass>()
      WithService.Select((type, @base) =>
         type.GetAllInterfaces()
             .Where(i => i.Assembly == yourInterfacesAssembly)))
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文