EF4.0,存储库和Ninject 2 [英] EF4.0, repositories, and Ninject 2

查看:75
本文介绍了EF4.0,存储库和Ninject 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我面临的两个持续问题的延续: EF4.0-是否有办法查看附加了哪些实体在调试过程中使用什么ObjectContext?我正在使用这个空间来提出另一个稍微复杂的问题,并且我不想在其他线程中提出一个巨大的,超长的问题.

This is in continuation of two ongoing problems I'm facing: Problems trying to attach a new EF4 entity to ObjectContext while its entity collection entities are already attached and EF4.0 - Is there a way to see what entities are attached to what ObjectContext during debugging? I'm using this space to ask another somewhat complicated question, and I don't want to make a huge, ultra long question out of my other threads.

因此,简要介绍一下:

我有绑定到DTO的传入表单数据.我想将DTO映射到实体(游戏实体).问题在于游戏包含一个EntityCollection,我必须基于DTO中的int []创建EntityCollection并将其添加到游戏(每个整数代表平台的ID).而且,自然地,EF4之所以令人窒息,部分原因是它与多对多的关系,而且,我认为,因为有一些恶作剧正在影响着多少个ObjectContext对象在起作用.我不断收到一个例外,声称我无法将检索到的Platform实体添加到新的Game实体中,因为它们属于两个不同的ObjectContext.根据目前的设置,我看不到这是怎么可能的,但是我不确定问题可能是什么.

I have incoming form data which is bound to a DTO. I want to map the DTO to an Entity (a Game entity). The wrinkle is that the Game contains a EntityCollection which I must create and Add() to the Game based on a int[] in the DTO (each integer represents the ID of a Platform). And, naturally, EF4 is choking in part because it's a many-to-many relationship, but also, I think, because there's some shenanigans going on with how many ObjectContext objects are in play. I keep getting an exception claiming I can't add my retrieved Platform entities to my new Game entity because they belong to two different ObjectContexts. I can't see how that is possible given my current set up, but I'm not sure what else the problem could be.

好的,所以我有三个存储库,它们是通过Ninject接口注入注入到控制器中的.我像这样分别创建ObjectContexts:

Okay, so I have three repositories which I inject into my controller via Ninject interface injection. I create the ObjectContexts in each like so:

public class HGGameRepository : IGameRepository
{
    private HGEntities _siteDB = new HGEntities();

    // rest of repo
}

其他两个存储库以相同的方式构建.

The other two repositories are built the same way.

我的Ninject DI代码非常简单:

My Ninject DI code is fairly simple:

private class HandiGamerServices : NinjectModule
{
    public override void Load()
    {
        Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope();
        Bind<IGameRepository>().To<HGGameRepository>().InRequestScope();
        Bind<INewsRepository>().To<HGNewsRepository>().InRequestScope();
        Bind<ErrorController>().ToSelf().InRequestScope();
    }
}

据我了解,这应该为每个HTTP请求创建一次这些绑定.

From what I've read, this should create these bindings once per HTTP request.

我想做的是让我的HGEntities对象的一个​​实例在所有存储库之间共享,以确保我在玩一个并且只有一个ObjectContext.我只是不确定该怎么做.

What I'd like to do is have one instance of my HGEntities object be shared among all repositories in order to ensure I have one and only one ObjectContext in play. I'm just unsure how to do it.

有标准的方法吗?

推荐答案

这里是一个选择:

更改存储库,以在其构造函数中引入接口IHGEntities,并将HGEntities挂接到NinjectModule中,就像处理存储库一样.这样,当您的控制器需要IArticleRepository实例时,Ninject将实例化HGEntities实例以传递到存储库中,或者使用在当前HTTP上下文中已经处于活动状态的实例.

Change your repositories to take in an interface, IHGEntities, in their constructor and hook up HGEntities into your NinjectModule the same you did with your repositories. That way, when your controllers need an instance of IArticleRepository, Ninject will either instantiate an instance of HGEntities to pass into the repositories or use the instance that is already active in the current HTTP context.

然后,在您的存储库类中,您只需将IHGEntities转换为HGEntities.

Then, inside your repository classes you can simply cast IHGEntities to HGEntities.

这篇关于EF4.0,存储库和Ninject 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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