如何使用实体框架范围内使用依赖注入? [英] How to use Entity Framework context with dependency injection?

查看:147
本文介绍了如何使用实体框架范围内使用依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置,可以使用实体框架EDMX模型上下文中的基础信息库类。我遇到的问题是,我需要找到该EF EDMX对象上下文实现这样我就可以传递给通过依赖注射构造函数的接口。我已经通过创建它,并将其存储在HttpContext的,但杀死的能力,单元测试的DataFactory解决此之前了。任何帮助将是AP preciated。谢谢!

 公共抽象类BaseRepository< T>其中T:EntityObject
{
        私人MyDataModelContext _dataContext;
        私人对象集< T> dbset;        保护BaseRepository(IObjectContext的DataContext的)
        {
            _dataContext = DataContext的;
            dbset = _dataContext.CreateObjectSet< T>();
        }    .....


解决方案

我一直在传递我自己的接口的背景下DataContextFactory,并通过了我的资料库,像这样:

上下文接口:

 公共IMyDataContext {
    在数据库//每一个表
    IDbSet< 1级> Class1s {获取;设置; }
    //等    //从EF标准的方法,您将使用
    无效添加(对象实体);
    空连接(对象实体);
    无效删除(对象实体);
    无效的SaveChanges();
}

上下文工厂:

 公共类MyDataContextFactory:IMyDataContextFactory {
    公共IMyDataContext的getContext(){
        // TODO:使用Service Locator模式,以避免直接instanciation
        返回新MyDataContext();
    }
}

上下文工厂接口:

 公共接口IMyDataContextFactory {
    IMyDataContext的getContext();
}

存储库:

 公共类MyClass1Repository {
    私人只读IMyDataContextFactory厂;
    公共MyClass1Repository(IMyDataContextFactory厂){
        // TODO:检查null
        this.factory =厂;
    }
    公开名单< MyClass1的>得到所有() {
        使用(IMyDataContext DB = this.factory.GetContext()){
            返回db.Class1s.ToList();
        }
    }
    // TODO:该得到的东西其他方法
}

然后,当我想测试存储库中,我通过在一个假的 IMyDataContextFactory 从返回假 IMyDataContext 的getContext()

在一次我注意到库重复,并且可以将某些方法到基本资料库: GETALL()保存() GetById()有时候,如果我有一致的主键,等等。

I'm trying to setup a base Repository class that can use the Entity Framework edmx model context. The problem I'm having is that I need to find an interface that the EF EDMX object context implements so I can pass to the constructor via dependency injections. I've got around this before by using a DataFactory that creates it and stores it in the HttpContext but that kills the ability to unit test. Any help would be appreciated. Thanks!

public abstract class BaseRepository<T> where T : EntityObject
{
        private MyDataModelContext _dataContext;
        private ObjectSet<T> dbset;

        protected BaseRepository(IObjectContext dataContext)
        {
            _dataContext = dataContext;
            dbset = _dataContext.CreateObjectSet<T>();
        }

    .....

解决方案

I've always created a DataContextFactory that passes my own interface to the Context, and passed that to my repositories like so:

The context interface:

public IMyDataContext {
    // One per table in the database
    IDbSet<Class1> Class1s { get;set; }
    // etc

    // The standard methods from EF you'll use
    void Add( object Entity );
    void Attach( object Entity );
    void Delete( object Entity );
    void SaveChanges();
}

The context factory:

public class MyDataContextFactory : IMyDataContextFactory {
    public IMyDataContext GetContext() {
        // TODO: Use the service locator pattern to avoid the direct instanciation
        return new MyDataContext();
    }
}

The context factory interface:

public interface IMyDataContextFactory {
    IMyDataContext GetContext();
}

The repository:

public class MyClass1Repository {
    private readonly IMyDataContextFactory factory;
    public MyClass1Repository( IMyDataContextFactory Factory ) {
        // TODO: check for null
        this.factory = Factory;
    }
    public List<MyClass1> GetAll() {
        using ( IMyDataContext db = this.factory.GetContext() ) {
            return db.Class1s.ToList();
        }
    }
    // TODO: Other methods that get stuff
}

Then when I want to test the repository, I pass in a fake IMyDataContextFactory that returns a fake IMyDataContext from GetContext().

In time I notice duplication in repositories, and can push certain methods into the base repository: GetAll(), Save(), GetById() sometimes if I have consistent primary keys, etc.

这篇关于如何使用实体框架范围内使用依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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