Partial methods and inheritance from non-partial c

2019-08-09 06:40发布

问题:

I imported a table from the database using designer, then I've edited the corresponding cs file to add extra functionality to the model (it's a partial exactly for this reason). Now I came up with some ideas on how to make that functionality more reusable and packed that into a base class (so in the context code I have partial class Foo : BaseClass). I made the partial class inherit the base class and all is fine... except for partial methods.

The generated partial class has some partial methods that normally don't have any code (namely the OnCreated method). I've added an OnCreated method to the base class and put a breakpoint in it, but it's never hit.

Can I somehow make a partial class take the code for partial method from a non-partial parent or am I doing something wrong here?

Background: I have a certain structure (columns containing author's id, id of user who was the last one to modify the record and dates of create and update dates) that is appearing in multiple tables and I'm trying to define most code for handling that in a single place in my project. It involves having uniform access to associated users and I had some luck doing that by defining associations in my base class (basically this, but with few modifications). So far it appears to works perfectly, except for the fact that I should be assigning default values to storage variables inside of the constructor of the generated class (this._SomeVariable = default(EntityRef<SomeModel>)). However modifying the generated code is pointless, since all changes will be lost when the file is generated anew. So the next best thing would be to implement OnCreated partial method which is run at the end of the generated class. I can implement that in the non-generated cs file for my model, but I'd rather put it inside the base class which is shared with all similar models.

Here is some minimal code to make it more clear:

Generated code:

partial class Foo
{
    public Foo()
    {
        // does some initialization here
        this.OnCreated();
    }

    partial void OnCreated();
}

Extended code for Foo:

partial class Foo : BaseClass // Thanks to this I can use the uniform JustSomeModel association
{
    // This code here would run if it was uncommented
    // partial void OnCreated() {}

    // However I'd rather just have the code from base.OnCreated()
    // run without explicitly calling it
}

Base class:

public class BaseClass
{
    protected EntityRef<SomeModel> _SomeVariable;

    [Association(Name = "FK_SomeModel", Storage = "_SomeVariable", ThisKey = "SomeModelId", OtherKey = "Id", IsForeignKey = true)]
    public SomeMode JustSomeModel
    {
        get
        {
            return this._SomeVariable.Entity;
        }
    }

    // This never runs
    public void OnCreated()
    {
        this._SomeVariable = default(EntityRef<SomeModel>)
    }
}

The best solution I can think of right now would be to do this:

partial class Foo : BaseClass
{
    partial void OnCreated()
    {
        base.OnCreated(); // Haven't really tested this yet
    }
}

However this means I will have to add this piece of code to every model inheriting from BaseClass that I use and I'd rather avoid it.

回答1:

Based on information posted by Eugene Podskal I can assume this cannot be done and my best bet is implementing the partial method and calling base method inside it.

partial class Foo : BaseClass
{
    partial void OnCreated()
    {
        base.OnCreated();
    }
}

Edit: Tested it and it works.