One scenario that is occasionally useful is for one file to be generated containing
multiple partial types, and then some of those types are enhanced in other files, one
manually generated file per type. To return to the ORM example, the tool could generate
a single file containing all the entity definitions, and some of those entities could
have extra code provided by the developer, using one file per entity. This keeps the
number of automatically generated files low, but still provides good visibility of the
manual code involved.
Figure 7.2 shows how the uses of partial types for XAML and entities are similar, but
with slightly different timing involved when it comes to creating the autogenerated C#
code.
A somewhat different use of partial types is as an aid to refactoring. Sometimes a
type gets too big and assumes too many responsibilities. One first step to dividing the
bloated type into smaller, more coherent ones can be to first split it into a partial type
over two or more files. This can be done with no risk and in an experimental manner,
moving methods between files until each file only addresses a particular concern.
Although the next step of splitting the type up is still far from automatic at that stage,
it should be a lot easier to see the end goal.
When partial types first appeared in C# 2, no one knew exactly how they??™d be used.
Pages:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370