局部方法和非局部类的继承 [英] Partial methods and inheritance from non-partial classes

查看:79
本文介绍了局部方法和非局部类的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用设计器从数据库中导入了一个表,然后编辑了相应的cs文件,为模型添加了额外的功能(出于这个原因,这只是部分原因).现在,我想出了一些有关如何使该功能更可重用并将其打包到基类中的想法(因此,在上下文代码中,我有partial class Foo : BaseClass).我让分部类继承了基类,并且一切都很好……除了分部方法.

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.

生成的部分类具有一些通常没有任何代码的部分方法(即OnCreated方法).我已经在基类中添加了一个OnCreated方法,并在其中添加了一个断点,但从未成功.

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.

我可以以某种方式使部分类从非部分父级中获取部分方法的代码吗?还是在这里做错了事?

背景:我有一个特定的结构(包含作者ID,最后一个修改记录的记录的用户ID和创建和更新日期的用户ID)出现在多个表格中,我正在尝试定义大多数代码以在我的项目中的一个地方进行处理.它涉及到对关联用户的统一访问,我很幸运通过在我的基类中定义关联来做到这一点(基本上是,但是少量修改).到目前为止,除了我应该在生成的类的构造函数内部将默认值分配给存储变量这一事实外,它似乎运行完美 (this._SomeVariable = default(EntityRef<SomeModel>)).但是,修改生成的代码毫无意义,因为当重新生成文件时,所有更改都将丢失.因此,第二件事是实现OnCreated局部方法,该方法在生成的类的末尾运行.我可以在模型的未生成的cs文件中实现该功能,但我宁愿将其放在与所有相似模型共享的基类中.

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:

生成的代码:

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

    partial void OnCreated();
}

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
}

基类:

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
    }
}

但是,这意味着我必须将这段代码添加到从我使用的BaseClass继承的每个模型中,而我宁愿避免使用它.

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.

推荐答案

基于Eugene Podskal发布的信息,我可以假设这无法完成,而我最好的选择是实现局部方法并在其中调用基本方法.

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();
    }
}

对其进行了测试,并且可以正常工作.

Tested it and it works.

这篇关于局部方法和非局部类的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆