如何编写模拟存储库来测试基于实体框架上下文构建的 WCF RIA 域服务

发布于 2024-08-16 21:08:55 字数 470 浏览 2 评论 0原文

我需要编写一个测试层来测试我的 WCF RIA 域服务层,该层构建在实体框架上下文之上。我遇到过一些模式,建议使用存储库,然后使用域服务工厂来使用存储库实例来初始化域服务。 Vijay 的博客 (http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services .aspx)。此实现的问题在于,它仅针对特定域对象(例如客户/产品)初始化存储库,但它无法提供创建可以返回我想要返回的任何对象的存储库的方法。

请建议这样做的正确方法是什么以及是否可行。

预先感谢,

马诺吉

I need to write a test layer to Test my WCF RIA Domain Service layer which is build on top of Entity Framework context. I have come across some patterns which suggest to use a repository and then use the Domain Service factory to intilize the domain service with a repository instance to use. One of the sample which fits the requirement is explained here on Vijay's blog(http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services.aspx). The problem with this implementation is that it initilize the repository only for a specific Domain Object e.g. Customer/Product but it provides no way to create a repository which can return any object which i would like to return.

Please suggest what is the right way of doing this and whether it is possible or not.

Thanks in advance,

Manoj

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

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

发布评论

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

评论(1

贪恋 2024-08-23 21:08:55

我通过使用 RepositoryCollection 对象扩展示例来解决这个问题,该对象根据需要自动实例化 LinqToSqlRepositories,并且还允许手动插入模拟/存根存储库以进行单元测试。

public class RepositoryCollection : IDisposable
{
    private Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
    private DataContext _context;

    public RepositoryCollection() { }

    public RepositoryCollection(DataContext context)
    {
        _context = context;
    }

    public IRepository<T> Get<T>() where T : class
    {
        if(!_repositories.ContainsKey(typeof(T)))
            _repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context));

        return _repositories[typeof(T)] as IRepository<T>;
    }

    public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class
    {
        _repositories[typeof(T)] = repository;
        return this;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void SubmitChanges()
    {
        if (_context != null)
            _context.SubmitChanges();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if(_context != null)
                _context.Dispose();
        }
    }
}

然后,在您的域服务中,您可以像这样使用它:

private RepositoryCollection _repositoryCollection;

public MyDomainService(RepositoryCollection repositoryCollection = null)
{
    _repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext());
}

public IQueryable<Customer> GetCustomers()
{
    return _repositoryCollection.Get<Customer>().Query();
}

public IQueryable<Product> GetProducts()
{
    return _repositoryCollection.Get<Product>().Query();
}

.. other methods go here ...

I got around this issue by extending the sample with a RepositoryCollection object, which automatically instantiates LinqToSqlRepositories as needed, and also allows the insertion of mock/stub repositories manually for unit testing.

public class RepositoryCollection : IDisposable
{
    private Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
    private DataContext _context;

    public RepositoryCollection() { }

    public RepositoryCollection(DataContext context)
    {
        _context = context;
    }

    public IRepository<T> Get<T>() where T : class
    {
        if(!_repositories.ContainsKey(typeof(T)))
            _repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context));

        return _repositories[typeof(T)] as IRepository<T>;
    }

    public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class
    {
        _repositories[typeof(T)] = repository;
        return this;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void SubmitChanges()
    {
        if (_context != null)
            _context.SubmitChanges();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if(_context != null)
                _context.Dispose();
        }
    }
}

Then, in your domain service, you use it like so:

private RepositoryCollection _repositoryCollection;

public MyDomainService(RepositoryCollection repositoryCollection = null)
{
    _repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext());
}

public IQueryable<Customer> GetCustomers()
{
    return _repositoryCollection.Get<Customer>().Query();
}

public IQueryable<Product> GetProducts()
{
    return _repositoryCollection.Get<Product>().Query();
}

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