Sign in to follow this  
DaWanderer

[.net] Best way to extend a template class

Recommended Posts

I'm using Python to generate C# classes for the data layer in my latest game. My goal is to be able to have the base code in one or more files per class that can be overwritten if I change the template and to extend the generated class in another file to keep the clutter to a minimum. My first solution was simple; just have 2 files like:
// GameObject.cs
// Autogenerated using a template
public partial class GameObject
{
    public static GameObject Load(int id)
    {
        ....
    }
}

// GameObjectExtensions.cs
// The place for my own additions to GameObject
public partial class GameObject
{
    public void MyCustomMethod()
    {
    }
}

The problem occurs when I want to add some special handling after loading or saving. Usually, this would mean overriding a base class method like AfterLoad that would get called in the generated Load method from above. My new solution became this tangled mess with 3 files:
// GameObjectBase.cs
// Autogenerated base class with methods to override
public class GameObjectBase
{
    protected virtual void AfterLoad()
    {
    }
}

// GameObject.cs
public partial class GameObject : GameObjectBase
{
    // Load now calls the base class AfterLoad method
    public static GameObject Load(int id)
    {
        GameObject obj = ... // load object
        obj.AfterLoad();

        return (obj);
    }
}

// GameObjectExtensions.cs
public partial class GameObject
{
    // The extension class overrides the base class method
    // to customize loading behavior
    protected override void AfterLoad()
    {
        MyCustomMethod();
    }

    public void MyCustomMethod()
    {
    }
}

Any ideas on how I could avoid having the base class but still be able to hook into the loading behavior of the generated class?

Share this post


Link to post
Share on other sites
Unless I'm not understanding, you don't need the base class if you make sure every 'GameObject' class has an AfterLoad method defined. Just assume it's there, and it just won't compile if it isn't defined.

That being said, I don't know what you're trying to achieve. Having a base class for each class generated this way seems like a good idea to me...

Share this post


Link to post
Share on other sites
Maybe I'm thinking about this the wrong way. Instead of having a base class for each type of database object, I could have a single base class for all database objects with the virtual AfterLoad and AfterSave methods.

In my head, I was thinking in a Python manner with the ability to declare and replace a method definition within the same class. Obviously in C# I can't declare an AfterLoad method in one partial class file and re-declare it in another.

Share this post


Link to post
Share on other sites
Quote:
Original post by DaWanderer
Maybe I'm thinking about this the wrong way. Instead of having a base class for each type of database object, I could have a single base class for all database objects with the virtual AfterLoad and AfterSave methods.


Yeah sorry, that's what I meant. Obviously a base class for each type would be very silly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this