为ObjectContext的创建界面 [英] Creating Interface for ObjectContext

查看:117
本文介绍了为ObjectContext的创建界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建的ObjectContext 一个抽象层。我的理解是OC工作单元,但我只是不完全怎么写它良好的界面。理想情况下,我想能够换出我的'RealDataContext实现 IDataContext 的东西就像一个'FakeDataContext这将是完全在内存中。

I'm trying to create an abstracted layer for ObjectContext. I understand OC is a Unit of Work, but I'm just not entirely how to write a good interface for it. Ideally I'd like to be able to swap out my 'RealDataContext' that implements IDataContext for something like a 'FakeDataContext' that would be fully in-memory.

这样做的原因是,我希望能够测试我的库针对内存数据库。

The reason for this is that I want to be able to test my repositories against an in-memory database.

谢谢!

推荐答案

您应该能够创建一个从ObjectContext的派生,并实现了IDataContext接口的扩展类。要真正能够模拟ObjectContext中,你IDataContext接口将需要包括ObjectContext中的任何成员正在使用并希望嘲笑匹配签名(或属性)。像下面的东西应该足够了:

You should be able to create an extended class that derives from ObjectContext, and implements an IDataContext interface. To truly be able to mock the ObjectContext, your IDataContext interface would need to include matching signatures (or properties) for any member of ObjectContext that you are using and wish to mock. Something like the following should suffice:

interface IDataContext, IDisposable
{
    void AddObject(string entitySetName, object entity);
    void Attach(IEntityWithKey entity);
    void Detach(object entity);
    void DeleteObject(object entity);
    int SaveChanges();
    int SaveChanges(bool acceptChangesDuringSave);
    int SaveChanges(SaveOptions options);

    // Any other members you wish to be mockable
}

class DataContext: ObjectContext, IDataContext
{
    // nothing here
}

从技术上来说,因为DataContext的继承一切从ObjectContect,IDataContext实施取通过ObjectContext的照顾。你不应该需要在DataContext类的任何附加的实现。只要你总是注入(或使用一个工厂来创建)IDataContext的实例,而不是ObjectContext的,你应该能够嘲笑IDataContext当测试:

Technically speaking, since DataContext inherits everything from ObjectContect, the implementation of IDataContext is taken care of by ObjectContext. You should not need any additional implementation in the DataContext class. So long as you always inject (or use a factory to create) instances of IDataContext rather than ObjectContext, you should be able to mock IDataContext when testing:

class SomeEntityRepository: IRepository<SomeEntity>
{
    public SomeEntityRepository(IDataContext context)
    {
        m_context = context;
    }

    private readonly IDataContext m_context;

    public SomeEntity GetById(long id)
    {
        // implementation
    }
}

// xUnit.NET & Moq
class SomeEntityRepositoryTests
{
    [Fact]
    public void GetById_returns_entity_when_valid_id_is_passed()
    {
        // state and context
        var mockContext = new Mock<IDataContext>();

        // arrangement
        mockContext.Setup(/* configure mock context here */);
        var repo = new SomeEntityRepository(mockContext.Object);

        // activity
        var entity = repo.GetById(100);

        // assertions
    }
}

这篇关于为ObjectContext的创建界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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