Partial Classes in C#

后端 未结 20 2898
广开言路
广开言路 2021-02-19 08:25

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

相关标签:
20条回答
  • 2021-02-19 08:52

    Sometimes you might find terribly old code at work that may make it close to impossible to refactor out into distinct elements without breaking existing code.

    When you aren't given the option or the time to create a more genuine architecture, partial classes make it incredibly easy to separate logic where its needed. This allows existing code to continue using the same architecture while you gain a step closer to a more concrete architecture.

    0 讨论(0)
  • 2021-02-19 08:52

    I worked on a project a couple years ago where we had a typed DataSet class that had a ton of code in it: Methods in the DataTables, methods in the TableAdapters, declarations of TableAdapter instances, you name it. It was a massive central point of the project that everyone had to work on often, and there was a lot of source-control contention over the partial class code file.

    So I split the code file into fix or six partial class files, grouped by function, so that we could work on smaller pieces and not have to lock the whole file every time we had to change some little thing.

    (Of course, we could also have solved the problem by not using an exclusively-locking source-control system, but that's another issue.)

    0 讨论(0)
  • 2021-02-19 08:54

    Where I'm at we have a program that handles incoming files from clients. It's set up so that each client's code is in it's own class library project, which knows how to handle whatever format that client chooses to use.

    The main code uses the libraries by defining a fairly extensive interface that a class in the library must implement (probably should be a few distinct interfaces, but it's too late to change it now). Sometimes that involves a lot more code in the same class than we'd normally think prudent. Partial classes allow us to break them up somewhat.

    0 讨论(0)
  • 2021-02-19 08:55

    I use it in a data access layer. The generated classes like the mapper and queries a partial. If I need to add a mapper method for example to do a fancy load that's not generated I add it to the custom class.

    At the end the programmer that uses the data layer in the business layer only sees one class with all the functionality he or she needs. And if the data source changes the generic parts can easily be generated without overwriting custom stuff.

    0 讨论(0)
  • 2021-02-19 08:55

    I use partial classes as a means of separating out the different sub elements of custom controls that I write. Also, when used with entity creation software, it allows products like LLBLGen to create generated versions of classes, as well as a custom, user edited version, that won't get replaced if the entities need to be regenerated.

    0 讨论(0)
  • 2021-02-19 08:59

    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.

    0 讨论(0)
提交回复
热议问题