重构因类型不同的重复代码

时间:2016-03-25 01:33:28

标签: c# oop generics inheritance refactoring

我构建了一些测试辅助方法,为实体框架构建一个模拟DBContext。这些方法包含重复的代码,我假设必须有更好的方法来执行此操作。我无法弄清楚如何。我尝试创建一个MockDbContextBuilder<>类,但失败了,因为我没有DbSet<>。有任何想法吗?以下是我试图重构为一种方法的三个例子:

    private Mock<IPSNContext> BuildMockDbContext(List<TestEntity> list)
    {
        _mockDbSet = new Mock<DbSet<TestEntity>>();
        _mockDbSet.As<IQueryable<TestEntity>>().Setup(m => m.Provider).Returns(list.AsQueryable().Provider);
        _mockDbSet.As<IQueryable<TestEntity>>().Setup(m => m.Expression).Returns(list.AsQueryable().Expression);
        _mockDbSet.As<IQueryable<TestEntity>>().Setup(m => m.ElementType).Returns(list.AsQueryable().ElementType);
        _mockDbSet.As<IQueryable<TestEntity>>().Setup(m => m.GetEnumerator()).Returns(list.AsQueryable().GetEnumerator());

        foreach (var item in list)
        {
            _mockDbSet.Setup(m => m.Find(item.Id)).Returns(item);
        }

        var mockContext = new Mock<IPSNContext>();
        mockContext.Setup(c => c.Set<TestEntity>())
                    .Returns(_mockDbSet.Object);
        return mockContext;
    }

    private Mock<IPSNContext> BuildMockDbContext(List<MessageBoardTopic> list)
    {
        _mockDbSet = new Mock<DbSet<MessageBoardTopic>>();
        _mockDbSet.As<IQueryable<MessageBoardTopic>>().Setup(m => m.Provider).Returns(list.AsQueryable().Provider);
        _mockDbSet.As<IQueryable<MessageBoardTopic>>().Setup(m => m.Expression).Returns(list.AsQueryable().Expression);
        _mockDbSet.As<IQueryable<MessageBoardTopic>>().Setup(m => m.ElementType).Returns(list.AsQueryable().ElementType);
        _mockDbSet.As<IQueryable<MessageBoardTopic>>().Setup(m => m.GetEnumerator()).Returns(list.AsQueryable().GetEnumerator());

        foreach (var item in list)
        {
            _mockDbSet.Setup(m => m.Find(item.Id)).Returns(item);
        }

        var mockContext = new Mock<IPSNContext>();
        mockContext.Setup(c => c.Set<MessageBoardTopic>())
                    .Returns(_mockDbSet.Object);        
        return mockContext;
    }

    private Mock<IPSNContext> BuildMockDbContext(List<MessageBoardReply> list)
    {
        _mockDbSet = new Mock<DbSet<MessageBoardReply>>();
        _mockDbSet.As<IQueryable<MessageBoardReply>>().Setup(m => m.Provider).Returns(list.AsQueryable().Provider);
        _mockDbSet.As<IQueryable<MessageBoardReply>>().Setup(m => m.Expression).Returns(list.AsQueryable().Expression);
        _mockDbSet.As<IQueryable<MessageBoardReply>>().Setup(m => m.ElementType).Returns(list.AsQueryable().ElementType);
        _mockDbSet.As<IQueryable<MessageBoardReply>>().Setup(m => m.GetEnumerator()).Returns(list.AsQueryable().GetEnumerator());

        foreach (var item in list)
        {
            _mockDbSet.Setup(m => m.Find(item.Id)).Returns(item);
        }

        var mockContext = new Mock<IPSNContext>();
        mockContext.Setup(c => c.Set<MessageBoardReply>())
                    .Returns(_mockDbSet.Object);
        return mockContext;
    }

1 个答案:

答案 0 :(得分:2)

您可以使用泛型类型的方法,如:

    private Mock<IPSNContext> BuildMockDbContext<T>(List<T> list)
    {
                _mockDbSet = new Mock<DbSet<T>>();
                _mockDbSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(list.AsQueryable().Provider);
                _mockDbSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(list.AsQueryable().Expression);
                _mockDbSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(list.AsQueryable().ElementType);
                _mockDbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(list.AsQueryable().GetEnumerator());

                foreach (var item in list)            
                    _mockDbSet.Setup(m => m.Find(item.Id)).Returns(item);            

                var mockContext = new Mock<IPSNContext>();
                mockContext.Setup(c => c.Set<MessageBoardTopic>())
                            .Returns(_mockDbSet.Object);

                return mockContext;
   }