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,


MyClass.generated.cs


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


MyClass.custom.cs


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.

Comments (5)
  1. I just walked past Don’s office and noticed him laughing hysterically at the "cubicle song" on YouTube….

  2. As noted per Tom Hollander, Don Smith has made available at the gotdotnet patterns & practices site…

  3. The link to the service factory does not work. Neither the link to the hands-on-lab. Can you fix this please? Thank You!

  4. Don says:

    Hey Thomas, unfortunately gotdotnet has been experiencing intermitent issues with regard to links. The only advice I can give you (since the links haven’t changed) is just to keep trying them. I apologize for the inconvenience we’re definitely looking at ways to improve our community experience in the future. Thanks for the heads up, I appreciate it!

  5. Batu says:

    I have not been able to find any documentation on generating DataTypes for array of objects, essential for returning any list from a query. Also the transformation objects for such arrays. The reference implementation seems to have some code generated by a tool.

    What is the recommended way to return the results of the query to the client ?

Comments are closed.

Skip to main content