Refactoring large constructors

后端 未结 3 960
情深已故
情深已故 2021-01-18 04:04

We have a few objects in our domain model with what you would comically term offensively large constructors, so large that IntelliSense gives up tr

3条回答
  •  庸人自扰
    2021-01-18 04:45

    I would go with the container types and use the immediate properties assignment of C# 4.0. This way one could easily use Intellisense on the resulting type, while still retaining a decent decoupling from the original type.

    For example:

    public class MyLegacyType
    {
        public MyLegacyType(MyConfiguration configuration) // etc
        {
          // ...
        }
    }
    
    public class MyConfiguration
    {
       public int Value1 { get; set; }
       public int Value2 { get; set; }
       // ...
    }
    

    And then:

    var myInstance = new MyLegacyType(new MyConfiguration
    {
      Value1 = 123,
      Value2 = 456
    });
    

提交回复
热议问题