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.

188 CHAPTER 7 Conclud<strong>in</strong>g <strong>C#</strong> 2: the f<strong>in</strong>al featuresGuiPage.xaml.cs(Handwritten <strong>C#</strong>)GuiPage.xaml(XAML)Schema/model(Database, XML, etc)XAML to <strong>C#</strong>converter(Build time)Code generator(Prebuild)GuiPage.g.cs(<strong>C#</strong>)Customer.cs(Handwritten <strong>C#</strong>)GeneratedEntities.cs(<strong>C#</strong> - <strong>in</strong>cludes partialCustomer class)<strong>C#</strong> compilation<strong>C#</strong> compilationGuiPage type(Part of an assembly)Customer type(Part of an assembly)Us<strong>in</strong>g XAML for declarative UI designPrebuild<strong>in</strong>g partial classes for database entitiesFigure 7.2Comparison between XAML precompilation and autogenerated entity classes7.1.3 Partial methods—<strong>C#</strong> 3 only!Just to reiterate my previous explanation, I realize that the rest of this part of the bookhas just been deal<strong>in</strong>g with <strong>C#</strong> 2 features—but partial methods don’t fit with any of theother <strong>C#</strong> 3 features and they do fit <strong>in</strong> very well when describ<strong>in</strong>g partial types. Apologiesfor any confusion this may cause.Back to the feature: sometimes we want to be able to specify behavior <strong>in</strong> a manuallycreated file and use that behavior from an automatically generated file. For <strong>in</strong>stance,<strong>in</strong> a class that has lots of automatically generated properties, we might want to be ableto specify code to be executed as validation of a new value for some of those properties.Another common scenario is for a code-generation tool to <strong>in</strong>clude constructors—manuallywritten code often wants to hook <strong>in</strong>to object construction to setdefault values, perform some logg<strong>in</strong>g, and so forth.In <strong>C#</strong> 2, these requirements could only be met either by us<strong>in</strong>g events that the manuallygenerated code could subscribe to, or by mak<strong>in</strong>g the automatically generatedcode assume that the handwritten code will <strong>in</strong>clude methods of a particular name—mak<strong>in</strong>g the whole code fail to compile unless the relevant methods are provided.Alternatively, the generated code can provide a base class with virtual methods that donoth<strong>in</strong>g by default. The manually generated code can then derive from the class andoverride some or all of the methods.All of these solutions are somewhat messy. <strong>C#</strong> 3’s partial methods effectively provideoptional hooks that have no cost whatsoever if they’re not implemented—any callsto the unimplemented partial methods are removed by the compiler. It’s easiest tounderstand this with an example. List<strong>in</strong>g 7.2 shows a partial type specified <strong>in</strong> two files,with the constructor <strong>in</strong> the automatically generated code call<strong>in</strong>g two partial methods,one of which is implemented <strong>in</strong> the manually generated code.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!