C#的部分类是否设计不好? [英] Are C#'s partial classes bad design?

查看:123
本文介绍了C#的部分类是否设计不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么部分类的概念甚至存在于C#/ VB.NET中。我正在开发一个应用程序,我们正在阅读一本(实际上很好)的书,我们正在实施的开发平台。在本书中,作者围绕平台API提供了大量的代码库/包装,并解释了他如何开发它,因为他教授了关于平台开发的不同主题。



长篇小说 - 他使用部分课程,遍布各地,作为在C#(IMO)中伪造多重继承的一种方式。为什么他不把这些课分成多个,使用组合超出了我。他将有3个部分类文件来组成他的基类,每个都有3-500行代码...并在他的API中多次这样做。



你觉得这是否合理? 如果是我,我会遵循SRP并创建了多个类来处理不同的所需行为,然后创建了一个具有这些类的实例作为成员(例如组合)的基类。为甚么MS甚至将部分类放入框架?他们删除了在C#中的每个范围级别扩展/压缩所有代码的能力(这在C ++中允许),因为它显然只是允许不良习惯 - 部分类是IMO,同样的事情。 我想我的问题是:当有合理的理由使用部分课程时,可以向我解释一下吗?


$ b $编辑:我知道对于Web / WinForms,没有别的选择。但在这之外呢?为什么MS只是把一些不同的关键字粘在一起,代码编组的类呢?还是真的有一个合适的设计方案,值得吗?



我不是说这是一个咆哮/战线。我老实说要在这里学东西。什么时候应该在代码设计中使用部分类?简单的问题,不需要关闭



谢谢

解决方案


如果有合法的理由使用部分课程,可以向我解释一下吗?


最合理和最有用的原因之一是鼓励将自动生成的代码和您自己的自定义扩展分开。例如,通常有一种自动生成的表单代码从某种设计器,但你通常想要添加自己的具体行为。这样,如果您重新生成自动代码部分,则不会触及具有特定扩展程序的部分。



这样说,很有可能有太多一件好事一些提示:




  • 不要让你的课程 partial 为了 partial


  • 除了彼此之外,不要将部分类放在任何地方。如果你必须跳到项目的一个完全不相关的部分,看看课程的另一半,你可能会做错了。


  • 使用 partial 作为一种掩盖类的大小的技术。如果你用 partial 分解你的课程,因为它们太大,你应该重新访问 单一责任原则


  • 如果您有三个以上<$相同类的c $ c> partial 片段,几乎可以保证你滥用部分。两个是合理性的典型上限,它通常用于从手写代码中分割自动生成的代码。





无论如何,长篇小说 - 他使用局部类,在所有地方,作为一种在C#(IMO)中伪造多重继承的方式。为什么他不把课分成多个,使用组合超出了我。他将有3个部分类文件来组成他的基础课程,每个都有3-500行代码...并在他的API中多次这样做。


是的,这绝对是一个明确的滥用 partial


I'm wondering why the 'partial class' concept even exists in C#/VB.NET. I'm working on an application and we are reading a (actually very good) book relavant to the development platform we are implementing at work. In the book, the author provides a large code base/wrapper around the platform API and explains how he developed it as he teaches different topics about the platform development.

Anyway, long story short - he uses partial classes, all over the place, as a way to fake multiple inheritance in C# (IMO). Why he didn't just split the classes up into multiple ones and use composition is beyond me. He will have 3 'partial class' files to make up his base class, each w/ 3-500 lines of code... And does this several times in his API.

Do you find this justifiable? If it were me, I'd have followed the S.R.P. and created multiple classes to handle different required behaviors, then created a base class that has instances of these classes as members (e.g. composition). Why did MS even put partial class into the framework? They removed the ability to expand/collapse all code at each scope level in C# (this was allowed in C++) because it was obviously just allowing bad habits - partial class is, IMO, the same thing. I guess my question is: Can you explain to me when there would be a legitimate reason ever to use a partial class?

EDIT: I'm aware that for Web/WinForms there is no other choice. But outside of this? Why didn't MS just put some different keyword for gluing code-genn'ed classes together? Or is there really a legit design scenario that merits it?

I do not mean this to be a rant / war thread. I'm honestly looking to learn something here. When should partial classes be used in code design? Simple question, no need to close

Thanks

解决方案

Can you explain to me when there would be a legitimate reason to ever use a partial class?

One of the most legitimate and useful reasons is to encourage the separation of automatically generated code and your own custom extensions to it. For instance, it's common to have an automatically generated form code from some kind of designer, but you usually want to add your own specific behavior to it. This way, if you regenerate the automatic-code portion, you're not touching the part that has your specific extensions.

That said, it's quite possible to have too much of a good thing. Some tips:

  • Don't make your classes partial for the sake of being partial.

  • Don't put partial classes anywhere except besides one another. If you have to jump to a completely unrelated section of the project to see the other half of the class, you're probably doing it wrong.

  • Don't use partial as a technique to obscure the size of the class. If you're breaking up your classes with partial because they're too big, you should revisit the Single Responsibility Principle.

  • If you have three or more partial fragments for the same class, it's almost a guarantee that you're abusing partial. Two is the typical upper bound of reasonableness, and it's generally used to segment automatically-generated code from handwritten code.

Anyway, long story short - he uses partial classes, all over the place, as a way to fake multiple inheritance in C# (IMO). Why he didnt just split the classes up into multiple ones and use composition is beyond me. He will have 3 'partial class' files to make up his base class, each w/ 3-500 lines of code... And does this several times in his API.

Yes, that's definitely a clear abuse of partial!

这篇关于C#的部分类是否设计不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆