Partial Classes & the Service Factory

I just posted the Service Factory Hands-on Labs to the community workspace and I learned something while I was putting them together. So, I'm going to do 2 things in this post. First, I'm going to do a little cheerleading for partial classes, then I'll talk about an issue Service Factory currently has since it is related to partial classses.

As you may already know, one of the most compelling uses of partial classes is when code is being generated. The code generator will create a class with the partial keyword and any modifications will take place in a different file so the generated file can be regenerated at any time without loosing changes. What I didn't know was that you can also establish an inheritance relationship in that hand-written partial class and you can leave the other one completely untouched. For example,


public partial class MyClass : IMyInterface
private int foo;
public int Foo
get { return foo; }
set { foo = value; }


public partial class MyClass : MyBase, IYourInterface
private string bar;
public string Bar
get { return bar; }
set { bar = value; }

I wouldn't be surprised to find that this capability explained in the C# docs, but it was way cool to find out about it on my own. It's totally fitting when it comes to the wave of modelling, guidance automation, and related efforts and technologies coming out now. If you knew all that already and found my epiphany boring ... sorry, I just had to convey my delight.

Now I get to convey my sadness. As it turns out, we (or anyone else) didn't find this until after we ship. Don't get me wrong, it's not the end of the world ... it's not like it's going to cause your spouse to leave you ... it just forces you to write more code by hand ... until we fix it :) Basically, when you go to create the repository classes, the recipe doesn't reflect on the business entities - it's just looking at the files ... so it thinks there are 2 different classes instead of just one, which also tends to hide the members on the base class. This is what you see:

Known issue Service Factory has with business entities using multiple parts of a partial class

A little extra coding won't hurt anyone ... heck, if you check out the hands-on labs, I even show you what the extra mapping for in and out parameters looks like. We'll also be posting a fix to this issue to the community eventually.