注释基于DI与生产商 [英] annotation based DI with Producers

查看:132
本文介绍了注释基于DI与生产商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在.NET世界中的简单,不需要对如过多的元数据配置任何DI框架。使用XML等?

Are there any DI frameworks in the .Net world that is simple and which does not require too much meta-data configuration for eg. using xml etc?

从Java世界的一个很好的例子是JBoss的焊接。随着焊接就可以有生产者方法返回的对象(方法,可以打上自定义注解)。上述制得的目的可以在需要的地方被注入。这节省了大量的元数据结构。 Ofcourse焊接需要一个XML太多,但它并不要求广泛的配置。

A very good example from the Java world would be JBoss Weld. With Weld it is possible to have producer methods (methods that can be marked with custom annotation) that return objects. The objects produced above can be injected where needed. This saves a lot of meta-data configuration. Ofcourse Weld needs an xml too, but it does not mandate extensive configurations.

推荐答案

ninject 让你绑定你的类型像这样在code:

There's ninject lets you bind your types like this in code:

Bind<IWarrior>().To<Ninja>();

有关语境与ninject结合使用属性看看

for contextual binding with ninject that uses attributes have a look at this

创建一个属性与装饰你的类

Create an attribute to decorate your class with

class ClimberNeeded : Attribute{}

绑定接口与一个给定的范围内实施 WhenClassHas

Bind<IWarrior>().To<Samurai>().WhenClassHas<ClimberNeeded>();

与属性装饰你的类。

Decorate your class with the attribute.

[ClimberNeeded]
class MountainousAttack {
    [Inject]
    IWarrior StandardMountainWarrior { get; set; }
}

在这里 StandardMountainWarrior 将成为武士,而不是忍者

此外,还有 TinyIOC 这是更好的有autoRegister的:),或者你可以这样做:

Then there's TinyIOC which is even better it has autoregister :) or you can do:

container.Register<IMyInterface, MyImplementation>();


funq ,它可以让你写聪明的代表来解决你的类型。这是真正缺乏的文档。但是,有一个很好的 vidcast


And funq which lets you write clever delegates to resolve your types. which is really lacking documentation. But there's a nice vidcast.

和其它更多的课程。例如团结,的 structuremap 和<一href="http://docs.castleproject.org/Default.aspx?Page=MainPage&NS=Windsor&AspxAutoDetectCookieSupport=1"相对=nofollow>温莎在code,这都具有的配置。

and lots more of course. for example unity, structuremap and windsor which all have "in code" configuration.

这篇关于注释基于DI与生产商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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