×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

i think you are really talking about DI constructor, honestly

I am never a big fan of using constructor injection, even though it's really convenient in many cases.

Personally, I lean to have property injection or a wrapper injection; however, that doesn't mean I am against using constructor injection. You just need to be careful.

The bottom line is, if you have too many parameters in whatever (either constructor or a public method), it's going to reduce the readability.

If an object requires a lot of input to become useful, it is possibly this object has too many responsibilities, or it could become a base abstract object, or it could benefit from a wrapper, or it could rely on a dedicated factory, and so on...

For myself, if I have more than 10 parameters in the constructor, I will think twice of adding more and try to refactor the object. But just my 2 cents
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / C#的构造函数, 有0个参数和n个参数的区别很大吗? 比如n=50
    • 基本上超过5个就要重构
    • 如果参数有50个,你为啥不用一个class 给封装一下呢?否则 你传参数的时候,你眼睛不花吗? 如果把这50个参数都写进构造函数里面,你眼睛能对焦过来吗? 那么写代码是纯粹的打铁的写代码方式! :-)
      • 我记得以前调用office 2007的api,那参数没有50,10-20总该有了,看的我是老眼昏花
        • You should use VB.NET to call Office API and pass only the parameters required.
          • 现在开发Office Application可以用NetOffice, 在NuGet上可以看到,很轻量、很方便
      • 小嫩孩,玩过DI 吗?
        • 你说的是 依赖注入? ^_^ 都是framwork帮你自动做的东西。 我自己都可以手动实现依赖注入。 有啥不会玩的啊? :-)
    • i think you are really talking about DI constructor, honestly
      I am never a big fan of using constructor injection, even though it's really convenient in many cases.

      Personally, I lean to have property injection or a wrapper injection; however, that doesn't mean I am against using constructor injection. You just need to be careful.

      The bottom line is, if you have too many parameters in whatever (either constructor or a public method), it's going to reduce the readability.

      If an object requires a lot of input to become useful, it is possibly this object has too many responsibilities, or it could become a base abstract object, or it could benefit from a wrapper, or it could rely on a dedicated factory, and so on...

      For myself, if I have more than 10 parameters in the constructor, I will think twice of adding more and try to refactor the object. But just my 2 cents
      • 可以通过factory来解决。
        • factory 只是 creation pattern 里面的一种,你建Object,要根据情况选其中一种或多种, 不明白跟DI有什么关系。
          • factory 可以玩的很花。 慢慢学去吧。
            • 上下文接不上.
              • 他就在那里装老。 哈哈 你说的factory pattern 的实质非常正确。人家问题是如何处理多参数的构造函数问题,他只是照本宣科的知道几个pattern 就在这里装老人!笑死人了。design pattern背后的核心思想就是封装。估计他还理解不到这个层次!
            • design pattern 就是玩花活儿 在我看来。你再玩的怎么花, 还不是跑不出那23个pattern? design pattern的东西都是事后打补丁的东西,有啥可了不起的呢? 还是C来的直接方便,向伟大的C语言致敬!^_^
              • 不求上进的小盆友。
                • :)
                • 你已经被面向对象的语言模式深深的洗脑了!小盆友,多看看其他语言吧! 多去了解 了解 functional languages 比如: scala Haskell Erlang etc. 然后再回头审视你的OO吧! :-)
                  • 木有冲突的,一个是状态转换,一个是属性包装,合起来用刚刚好,学了用了好十几年OOP,总觉得缺了点啥,现在明白了。语言,环境,工具,缺一不可啊。
            • 你玩的花的话,就有可能转到其他的pattern上去了。
              本身就不是factory pattern了。记住 pattern 是要活学活用的。要做到手中无刀,胸中有刀。这才是pattern灵活运用的最高境界。如果就是背几个pattern,然后生搬硬套上去用,很容易就用错!明白不? 小盆友?讲到pattern,我能喷晕你!^_^
              • 够臭!