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

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

问题描述

我想知道为什么局部类的概念,甚至存在于C#/ VB.NET。我工作的一个应用程序,我们正在读一(其实是很好的)书初步认识到,我们正在实施工作的开发平台。在这本书中,他提供了一个大code基/周围的平台API的包装,并解释他是如何开发它,因为他教这个平台开发不同的主题。

不管怎样,长话短说 - 他使用部分类,所有的地方,作为一种假的多重继承在C#中(IMO)。为什么他没有只是分裂类成多个的和使用的成分是超越我。他将有3个局部类文件,以弥补自己的基类,每一瓦特/ 3-500行code ...而且确实在他的API几次。

你觉得这合理?如果是我的话,我会跟着SRP并创建了多个类来处理不同的要求的行为,然后创建具有这些类的成员(如组成)的实例的基类。为什么MS甚至把部分类到框架中?它们拆下来展开/折叠所有code在C#中的每个作用域级别的能力(这是允许在C + +),因为它显然只是让坏习惯 - 局部类是国际海事组织同样的事情。 我想我的quetion是:你能向我解释的时候就不会有永远使用部分类有正当的理由

编辑:我知道,为Web /的WinForms没有其他的选择。但是,这外面?为什么不MS只是把一些不同的关键字进行胶合code-genned类togeather?还是真的有一个合法的设计方案,值得吗?

我并不是说这是一个夸夸其谈/战争线程。我honeslty希望在这里学到一些东西。当应部分类可用于在code设计 - 简单的问题,无需关闭

感谢

解决方案
  

您可以给我解释一下什么时候就永远使用部分类有正当的理由?

其中最正当的和有益的理由是鼓励自动生成的code分离和自己的自定义扩展它。举例来说,这是常见的有从某种设计的自动生成的表单code,但你通常希望在自己的特定的行为添加到它。这样,如果你重新生成自动 - code部分,你不动它具有特定扩展名的部分。

这是说,它很可能有太多的好东西。一些提示:

  • 不要让你的类部分的缘故是部分

  • 不要把部分类,除了除了彼此的任何地方。如果你要跳转到该项目以查看该类的另一半一个完全无关的部分,你可能就错了。

  • 不要使用部分作为一种技术来掩盖类的大小。如果你打破了你的类与部分因为他们太大了,你应该重新审视在单一职责原则

  • 如果您有三个或更多的部分片段为同一类,这几乎是你滥用局部的保证。二是典型的上限合理性,而且它通常用于从手写code段自动生成的code。

  

不管怎样,长话短说 - 他使用部分类,所有的地方,作为一种假的多重继承在C#中(IMO)。为什么他没有只是分裂类成多个的和使用的成分是超越我。他将有3个局部类文件,以弥补自己的基类,每一瓦特/ 3-500行code ...而且确实在他的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 he 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 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.

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 create 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 quetion is: Can you explain to me when there would be a legitimate reason to ever 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 glueing code-genned classes togeather? Or is there really a legit design scenario that merits it?

I do not mean this to be a rant / war thread. I'm honeslty 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天全站免登陆