13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Partial types187code generator can own the file where it will work, and completely overwrite thewhole file every time it wants to.Some code generators may even choose not to generate a <strong>C#</strong> file at all until thebuild is well under way. For <strong>in</strong>stance, the W<strong>in</strong>dows Presentation Foundation version ofthe Snippy application has Extensible Application Markup Language (XAML) filesthat describe the user <strong>in</strong>terface. When the project is built, each XAML file is converted<strong>in</strong>to a <strong>C#</strong> file <strong>in</strong> the obj directory (the filenames end with “.g.cs” to show they’ve beengenerated) and compiled along with the partial class provid<strong>in</strong>g extra code for thattype (typically event handlers and extra construction code). This completely preventsdevelopers from tweak<strong>in</strong>g the generated code, at least without go<strong>in</strong>g to extremelengths of hack<strong>in</strong>g the build file.I’ve been careful to use the phrase code generator <strong>in</strong>stead of just designer becausethere are plenty of code generators around besides designers. For <strong>in</strong>stance, <strong>in</strong> VisualStudio 2005 web service proxies are generated as partial classes, and you may wellhave your own tools that generate code based on other data sources. One reasonablycommon example of this is Object Relational Mapp<strong>in</strong>g (ORM)—some ORM tools usedatabase entity descriptions from a configuration file (or straight from the database)and generate partial classes represent<strong>in</strong>g those entities.This makes it very straightforward to add behavior to the type: overrid<strong>in</strong>g virtualmethods of the base class, add<strong>in</strong>g new members with bus<strong>in</strong>ess logic, and so forth. It’s agreat way of lett<strong>in</strong>g the developer and the tool work together, rather than constantlysquabbl<strong>in</strong>g about who’s <strong>in</strong> charge.One scenario that is occasionally useful is for one file to be generated conta<strong>in</strong><strong>in</strong>gmultiple partial types, and then some of those types are enhanced <strong>in</strong> other files, onemanually generated file per type. To return to the ORM example, the tool could generatea s<strong>in</strong>gle file conta<strong>in</strong><strong>in</strong>g all the entity def<strong>in</strong>itions, and some of those entities couldhave extra code provided by the developer, us<strong>in</strong>g one file per entity. This keeps thenumber of automatically generated files low, but still provides good visibility of themanual code <strong>in</strong>volved.Figure 7.2 shows how the uses of partial types for XAML and entities are similar, butwith slightly different tim<strong>in</strong>g <strong>in</strong>volved when it comes to creat<strong>in</strong>g the autogenerated <strong>C#</strong>code.A somewhat different use of partial types is as an aid to refactor<strong>in</strong>g. Sometimes atype gets too big and assumes too many responsibilities. One first step to divid<strong>in</strong>g thebloated type <strong>in</strong>to smaller, more coherent ones can be to first split it <strong>in</strong>to a partial typeover two or more files. This can be done with no risk and <strong>in</strong> an experimental manner,mov<strong>in</strong>g methods between files until each file only addresses a particular concern.Although the next step of splitt<strong>in</strong>g 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 <strong>in</strong> <strong>C#</strong> 2, no one knew exactly how they’d be used.One feature that was almost immediately requested was a way to provide optional“extra” code for generated methods to call. This need has been addressed by <strong>C#</strong> 3 withpartial methods.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!