简体   繁体   English

如何最好地重构由构造函数中创建的其他私有对象组成的代码

[英]How best to Refactor code that is composed of other private objects that are created in the Constructor

I have the following code that was written without Tests but is actually quite well designed and loosly coupled. 我有以下代码,这些代码是在没有测试的情况下编写的,但实际上设计得很好,耦合程度很差。 The CachedBindingListView constructs a number of objects namely the page provider and the Cache. CachedBindingListView构造许多对象,即页面提供程序和Cache。 As Follows. 如下。

    /// <summary>
    /// Inner data cache
    /// </summary>
    private Cache<T> InnerCache { get; set; }

    /// <summary>
    /// Page provider
    /// </summary>
    private PageProvider<T> PageProvider { get; set; }

    /// <summary>
    /// the Request object that is built with a filter, sort, no of pages etc.
    /// </summary>
    private BindingListRequest<T> BindingListRequest { get; set; }

    #endregion

    #region Constructor

    /// <summary>
    /// New object
    /// </summary>
    /// <param name="bindingListRequest">Request for a particular set of Business Objects</param>
    public CachedBindingListView(BindingListRequest<T> bindingListRequest)
    {
        BindingListRequest = bindingListRequest;
        PageProvider = new PageProvider<T>(BindingListRequest);
        InnerCache = new Cache<T>(PageProvider);
    }

I have written tests for the BindingListRequest, PageProvider and InnerCache. 我已经为BindingListRequest,PageProvider和InnerCache编写了测试。 but now want to start writing Tests for the CachedBindingList. 但现在想开始为CachedBindingList编写测试。 The constructor currently takes a BindingListRequest and constructs a page provider and inner cache based on this. 构造函数当前使用BindingListRequest,并基于此构造页面提供程序和内部缓存。

For test purposes I want to be able to provide at TestDouble for PageProvider and Cache. 出于测试目的,我希望能够在TestDouble中提供PageProvider和Cache。

What are the best ways of doing this. 这样做的最佳方法是什么? I have considered the following options. 我考虑了以下选项。

1) Add the PageProvider and InnerCache to the Constructor and inject the appropriate type in when required. 1)将PageProvider和InnerCache添加到构造函数中,并在需要时注入适当的类型。

2) Use A DI framework to Resolve the PageProvider and Cache within the CachedBindingListView constructor. 2)使用DI框架解析CachedBindingListView构造函数中的PageProvider和Cache。 This would involve either injecting the DI container into the CachedBindingListView constructor or making it global. 这将涉及将DI容器注入CachedBindingListView构造函数或使其成为全局容器。

3) Creating a factory that creates PageProvider and Cache and possibly even the CachedBindingListView and using this factory to create the PageProvider and InnerCache in the CachedBingingListView's constructor. 3)创建一个创建PageProvider和Cache甚至可能创建CachedBindingListView的工厂,并使用该工厂在CachedBingingListView的构造函数中创建PageProvider和InnerCache。 This would again involve either making the factory global or injecting it into the CachedBingingListView's Constructor. 这将再次涉及使工厂成为全局工厂,或者将其注入CachedBingingListView的构造方法中。

4) Making the PageProvider and InnerCache Properties Public and use a DI framework to create the CachedBindingListView and inject the PageProvider and InnerCache properties via property injection. 4)公开PageProvider和InnerCache属性,并使用DI框架创建CachedBindingListView并通过属性注入注入PageProvider和InnerCache属性。 Thus allowing me to override the Cache and PageProvider being injected during testing. 因此,允许我覆盖测试期间注入的Cache和PageProvider。

I can see how any of these can work but all of them seem a bit unwieldly and forced. 我可以看到其中的任何一个如何工作,但它们似乎都显得有些笨拙和被迫。 What are the opinions out there. 那里有什么意见。 What advice does anyone have on the best way of doing this. 对于最好的方法,任何人都有什么建议。

thanks GD 谢谢GD

If you just want to be able to test it - one option is to make a protected constructor with all three properties injected. 如果您只想测试它-一种选择是制作一个注入了所有三个属性的受保护的构造函数。 Then in your test make a FakeCachedBindingListView that inherits from the CachedBindingListView and inject the needed stubs, mocks (pick your poison). 然后在您的测试中创建一个从CachedBindingListView继承的FakeCachedBindingListView,并注入所需的存根,模拟(选择您的毒药)。

That's the one I'd go with, anyway. 无论如何,那是我会去的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM