Partial Classes in C#

2019-04-04 10:16发布

Are there are good uses of Partial Classes outside the webforms/winforms generated code scenarios? Or is this feature basically to support that?

20条回答
smile是对你的礼貌
2楼-- · 2019-04-04 10:35

On UserControls which are relatively complicated, I put the event handling stuff in one file and the painting and properties in another file. Partial classes work great for this, Usually these parts of the class are relatively independent and it's nice to be able to edit painting and event handling side by side.

查看更多
The star\"
3楼-- · 2019-04-04 10:35

I am late in the game... but just my 2 cents...

One use could be to refactor an existing god class in an existing legacy code base to multiple partial classes. It could improve the discoverability of code - if proper naming convention is being followed for the file names containing the partial classes. This could also reduce the source code repository - resolve and merge to an extent.

Ideally, a god class should be broken down into multiple small classes - each having single responsibility. Sometimes it is disruptive to perform medium to large refactorings. In such cases partial classes could provide a temporary relief.

查看更多
趁早两清
4楼-- · 2019-04-04 10:36

Generally, I consider it a code smell.

If your class is that complicated then it can probably be broken up into smaller reusable components.

Or it means that theres no inheritance hierarchy where there should be one.

For code generation scenarios it's good but I think code generation is another code smell.

查看更多
趁早两清
5楼-- · 2019-04-04 10:39

Code generation was the driving force behind partial classes. The need comes from having a code-generated class that is constantly changing, but allow developers to supply custom code as part of the class that will not be overridden everytime changes are made that force the class to be regenerated.

Take WinForms or Typed-DataSets for example (or any designer for that matter). Everytime you make a change to the designer it serializes the corresponding code to a file. Let's say you need to provide a few additional methods that the generator doesn't know anything about. If you added it to the generated file your changes would be lost the next time it was generated.

A project that I'm currently working on uses code-generation for all the DAL, BLL, and business entities. However, the generator only get's us 75% of the information. The remaining portion has to be hand coded (custom business logic for instance). I can assume that every BLL class has a SelectAll method, so that's easy to generate. However My customer BLL also needs to have a SelectAllByLocation method. I can't put this in my generator because it's not generic to all BLL classes. Therefore I generate all of my classes as partial classes, and then in a separate file I define my custom methods. Now down the road when my structure changes, or I need to regenerate my BLL for some reason, my custom code won't get wiped out.

查看更多
劫难
6楼-- · 2019-04-04 10:41

Anywhere you'd have used #region sections before probably makes more sense as separate files in partial classes.

I personally use partial classes for large classes where static members go in one file and instance members go in the other one.

查看更多
Emotional °昔
7楼-- · 2019-04-04 10:43

I find partial classes to be extremely helpful. Usually they are used to be able to extend autogenerated classes. I used them in one project with heavy unit tests. My UT classes had complex dependencies and it was not very practical to separate code across multiple classes.Of course it is better to use inheritance\composition but in some cases partial classes can be rally helpful.

查看更多
登录 后发表回答