What is the Humble Object pattern and when is it useful?

前端 未结 3 1227
清歌不尽
清歌不尽 2020-12-15 04:22

I was reading an article by Dino Esposito on how to test AsyncConrollers in ASP.NET MVC and in there he uses the \"Humble Object\" pattern, without going into much detail.

3条回答
  •  有刺的猬
    2020-12-15 05:00

    I'd usually implement this kind of this as an Interface - then you can use a mocking framework to stub it for testing, and an IoC framework to inject the correct implementation at runtime.

    Here's an example from my current project:

    public interface IUserInterface
    {
        string AskUserWhereToSaveFile(
            string title, 
            FileType defaultFileType, 
            string defaultFileName = null, 
            params FileType[] otherOptions
        );
    
        string AskUserToSelectFileToLoad(
           string title, 
           FileType defaultFileType, 
           params FileType[] fileTypes
        );
    
        void ShowError(string title, string details);
        bool AskUserIfTheyWantToRetryAfter(string errorMessage);
    }
    

    My Controller then has a dependancy on IUserInterface rather than a concrete view, which allow me to replace user interactions with a stub for testing.

提交回复
热议问题