Welcome to MSDN Blogs Sign in | Join | Help

partial classes

We use partial classes a lot in the DSL Tools. We need them to help compose code that's generated from different places, and generated with handwritten code.

If you're not familiar with it, a partial class definition is just a set of methods, properties etc that gets combined with another partial definition at link time, to form a complete class. The parts can be in different source files, which makes it easier to manage them separately: different generators can output to different files. So in the DSL Tools Use Case template, there are two generated files Designer.dsldm.cs and Shapes.cs that each contain aspects of every shape you have defined in your language. You can add a separate file with your own definitions.

There is a particularly useful pattern for providing a hook for optional manually defined features.  Each generated class - let's take Container.cs in the Use Case template - has a framework superclass in which default values are defined as overridable properties. For example, HasBackgroundGradient is defined in the framework superclass OutlinedShape. This enables you to override HasBackGroundGradient if you wish in a partial Container class, but to leave it to default to the OutlinedShape value otherwise.

Generated:

public partial class ContainerShape : Microsoft.VisualStudio.Modeling.Utilities.OutlinedShape {...

Framework:

 public class OutlinedShape { protected virtual bool HasBackgroundGradient { get { return true; } } ...

Optional manual override:

public class OutlinedShape { protected override bool HasBackgroundGradient { get { return false; } } ...

It isn't as sophisticated or flexible as delegation or aspect weaving, but does the trick.

Unfortunately, it only works for program code! If we're generating tables, configuration files, or reports, we need a way of merging them together. One way is to get the generator to read the file and only replace particular bits, corresponding to one view. Another, cleaner, way is to have each source generating to a separate file, and then weave them together in a final step.

 

Published Thursday, November 03, 2005 6:11 AM by Alan Cameron Wills
Filed under:

Comments

# re: partial classes

The partial classes addition to VS05 is a good example of a simple feature that makes sense and will have immediate benefit to the userbase.

Not sure about a bunch of other 'features' that have been added, but partial classes are definitely something I'll use and promote. Makes perfect sense for codegen tools too.
Thursday, November 03, 2005 4:23 AM by Simon Stewart

# re: partial classes

In AOP-land, they have intertype declarations, which does a similar thing. However, they don't defer until link-time: You can program with the code fragments. I think that is useful to do role modeling/collaboration-based design on a code level. Is there something like a composition function for partial classes that is explicit code? E.g. to resolve conflicts or map stuff?
Thursday, December 29, 2005 6:22 AM by Dirk Riehle

# partial classes

Monday, November 26, 2007 1:00 PM by partial classes

# Dynamic connectionstring with Dataset Wizards ? (VS2005) | keyongtech

New Comments to this post are disabled
 
Page view tracker