C# 的部分类是糟糕的设计吗? [英] Are C#'s partial classes bad design?

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

问题描述

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

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.

无论如何,长话短说 - 他到处使用部分类,作为在 C# (IMO) 中伪造多重继承的一种方式.为什么他不只是将类分成多个类并使用组合,这超出了我的理解.他将有 3 个部分类"文件来组成他的基类,每个文件包含 3-500 行代码......并且在他的 API 中多次这样做.

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.

你觉得这合理吗?如果是我,我会遵循 S.R.P.并创建了多个类来处理不同的所需行为,然后创建一个基类,该基类将这些类的实例作为成员(例如组合).为什么 MS 甚至将部分类放入框架中?他们取消了在 C# 中每个范围级别展开/折叠所有代码的能力(这在 C++ 中是允许的),因为这显然只是允许坏习惯 - 部分类是,IMO,同样的事情.我想我的问题是:你能向我解释什么时候有使用部分类的合法理由吗?

我知道对于 Web/WinForms 没有其他选择.但除此之外呢?为什么 MS 不只是放置一些不同的关键字来将代码生成的类粘合在一起?或者真的有值得它的合法设计场景吗?

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

谢谢

推荐答案

你能向我解释一下什么时候有使用部分类的正当理由吗?

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:

  • 不要为了partial而让你的类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.

不要使用 partial 作为掩盖类大小的技术.如果您使用 partial 拆分类,因为它们太大,您应该重新访问 单一职责原则.

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.

如果你有三个或更多的partial 片段用于同一个类,几乎可以保证你在滥用partial.二是典型的合理性上限,一般用于从手写代码中分割出自动生成的代码.

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.

无论如何,长话短说 - 他到处使用部分类,作为在 C# (IMO) 中伪造多重继承的一种方式.为什么他不只是将类分成多个类并使用组合,这超出了我的理解.他将有 3 个部分类"文件来组成他的基类,每个文件包含 3-500 行代码......并且在他的 API 中多次这样做.

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.

是的,这绝对是对partial的明显滥用!

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

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

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