实体框架 CTP5 和 Ninject 作为我的 IOC

发布于 2024-10-09 21:09:49 字数 144 浏览 3 评论 0原文

实体框架 CTP5 是否可以通过 IOC 容器构造检索的持久实体?

我正在使用 Ninject,它与 MVC 紧密结合,但在为某些业务规则构建域对象时,我需要将一些服务注入到域对象中。

我宁愿使用构造函数注入而不是方法或属性注入来执行此操作。

Is it possible in Entity Framework CTP5 to construct retrieved persisted entities via an IOC container?

I'm using Ninject and it's tied in fine with MVC, but I need to inject some services into my domain objects when they are constructed for some business rules.

I'd rather do this using constructor injection than method or property injections.

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

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

发布评论

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

评论(3

感情废物 2024-10-16 21:09:49

我不确定您到底想在这里完成什么,但 EF 几乎没有可扩展性点。您能做的最好的事情就是挂钩由 ObjectContext 触发的 ObjectMaterialized 事件。在 CTP5 中,您需要在 DbContext 的构造函数中像这样转换 DbContext:

((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += 
    this.ObjectContext_OnObjectMaterialized;

然后实现函数 ObjectContext_OnObjectMaterialized(object sender, ObjectMaterializedEventArgs e)。您将能够访问您的对象,不幸的是该对象已经具体化了。根据您的需求,您也许可以在这里尝试一些有趣的行为。

顺便说一句,这句话对我来说毫无意义:

当为某些业务规则构造域对象时,我需要将一些存储库注入到我的域对象中。

这是否违背了持久性无知域对象?

I'm not sure what, exactly, you're trying to accomplish here, but EF has almost no extensbility points. The best you can do is hook into the ObjectMaterialized event fired by ObjectContext. In CTP5, you need to cast your DbContext like so in the constructor for your DbContext:

((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += 
    this.ObjectContext_OnObjectMaterialized;

And then implement your function ObjectContext_OnObjectMaterialized(object sender, ObjectMaterializedEventArgs e). You will be able to access your object, which unfortunately has already been materialized. Depending on your needs, you might be able to hack in some interesting behavior here.

BTW, this sentence makes no sense to me:

I need to inject some repositories into my domain objects when they are constructed for some business rules.

Doesn't this go against Persistence Ignorant Domain Objects?

铃予 2024-10-16 21:09:49

我倾向于做与你想做的相反的事情。我让我的域对象尽可能无知(它们本质上是属性包)。当您需要执行某种操作(例如发送电子邮件)时,我将使用一项服务来执行该操作,并让该方法接受需要执行操作的域对象。在这种情况下,您只需将服务注入到应用程序的各个部分(使用 Ninject 可以更简单地完成)。

I tend to do the inverse of what you are trying to do. I make my domain objects as ignorant as possible (they are essentially property bags). When you need to perform some sort of action, like send an email, then I would use a service for that and have the method take in the domain object it needs to perform the action on. In this case, you would simply need to inject services into various parts of your application (which is much simpler to accomplish with Ninject).

執念 2024-10-16 21:09:49

我认为 EF Code First CTP 5 可以提供一些帮助。它遵循 IValidatableObject 接口,该接口将 ValidationContext 对象作为参数。 ValidationContext 是一个ServiceLocator,因此您应该能够使用validationContext 对象获取IoC 容器的实例。 (这只是我最初的想法,但我还没有尝试过)。抱歉,如果我的英语不太明白。

更新
抱歉,就在我发表此评论后,我意识到这个问题与我的理解完全不同。所以,我自己确实尝试了一些事情,经过一些点击和尝试以及更多的谷歌搜索后,我终于找到了一些东西。本来打算把答案贴在这里,但后来又考虑了一下,因为答案会很长。所以,我确实发布了这个博客。

http://nripendra-newa.blogspot.com /2011/02/entity-framework-ctp5-injecting-with.html

也许这可能会帮助一些搜索相同内容的谷歌用户。希望这次我答对了问题。

I think EF code first CTP 5 can be of some help. It honours IValidatableObject interface, which takes ValidationContext object as an argument. The ValidationContext is a ServiceLocator so you should be able to get the instance of the IoC container using the validationContext object. (This is just my initial thought, I haven't tried anything though). Sorry, if my English is not very understandable.

Update
Sorry, just after I posted this comment I realized that the question is quite different than what I understood. So, I did try few things myself, and after some hit and trial and much more googling I was able to get somewhere. I was planning to post the answer here, but then thought against it, since the answer would be very long. So, I did post this blog instead.

http://nripendra-newa.blogspot.com/2011/02/entity-framework-ctp5-injecting-with.html

May be this might help some googlers searching for the same. Hope I got the question right this time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文