重写派生类中的基类方法并触发其事件

本文关键字:事件 类方法 基类 派生 重写 | 更新日期: 2023-09-27 18:09:08

我想重写派生类中的基类方法,然后在派生类中执行某些操作。 因此,基类方法应使用其泛型类型调用。然后我的目标是触发被重写的派生类方法。

我有以下代码:

    public class Service<T> : Interface.IService<T> where T : class
    {
    public virtual event System.EventHandler<EntitySavingEventArgs<T>> BeforeSavingRecord;
    public Service()
    {
    }
    public virtual void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<T> e)
    {
    }
    private readonly DbContext _dbContext;
    public Service(DbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public virtual void Create(T item)
    {
        if (item == null)
            throw new ArgumentNullException("item");
        BeforeSavingRecord?.Invoke(this, new EntitySavingEventArgs<T>() { SavedEntity = item });
        _dbContext.Set(typeof(T)).Add(item);
        _dbContext.SaveChanges();
    }
}

在其派生类中,我有这样的东西:

[Service]
public partial class BankBusiness : Service<Bank>, IBankBusiness
{
    public BankBusiness()
        : base(ContainerManager.Container.Resolve<MyContext>())
    {
    }

    public override void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<Bank> e)
    {
        //Do something with entity item before saving
        base.OnBeforeSavingRecord(sender, e);
    }
}

然后在我打电话时在我的控制器中

bankBiz.Create(new Bank() { ... });

我想触发 bankBiz(派生类(覆盖的方法(OnBeforeSavingRecord(,该方法已注册到 BeforeSavingRecord 事件。

我不知道我的方案是否正确,如果正确,我该如何触发它。

如果不正确,我该怎么办。

重写派生类中的基类方法并触发其事件

我在基类中实现了类似的模式,我以这样的方式做到了:

基础:

public virtual void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<T> e)
{    }

在派生类中,我完全使用了您使用的调用:

德里维德:

public override void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<Bank> e)
{
    //Do something with entity item before saving
    base.OnBeforeSavingRecord(sender, e);
}

在我的情况下,要触发事件就足够

OnBeforeSavingRecord(this, new EntitySavingEventArgs<T>() { SavedEntity = item });

整个场景对我有用。

编辑:

如果从派生类的实例调用派生类,则调用OnBeforeSavingRecord将在派生类的重写方法中执行代码!

如果我理解正确,您希望在方法Create中触发事件,但希望执行派生类的事件代码。如果事件像您的情况一样被覆盖,那将是这样。您可以使用以下简单的控制台应用程序对其进行测试:(只需复制粘贴并运行(

public class Service 
{
    public virtual event System.EventHandler<EventArgs> BeforeSavingRecord;
    public virtual void OnBeforeSavingRecord(object sender, EventArgs e)
    {
        Console.WriteLine("Base: OnBeforeSavingRecord method call");
    }
    public virtual void Create(object item)
    {
        Console.WriteLine("Base: Create method call");
        // this will call the method of the derived class! if you call it from an instance of the derived class
        OnBeforeSavingRecord(this, new EventArgs());
    }
}
public partial class BankBusiness : Service
{
    public override void OnBeforeSavingRecord(object sender, EventArgs e)
    {
        //Do something with entity item before saving
        Console.WriteLine("Derived Class OnBeforeSavingRecord CALL");
        base.OnBeforeSavingRecord(sender, e);                
    }
}
static void Main(string[] args)
{        
    BankBusiness bankBiz = new BankBusiness();
    bankBiz.Create(new object());
    Console.ReadKey();
}

@Mong朱你的解决方案有效,但不适用于我的方案.我想出了以下解决方案

public class Service<T> : Interface.IService<T> where T : class
{
    Interface.IService<T> implementation;
    public virtual event System.EventHandler<EntitySavingEventArgs<T>> BeforeSavingRecord;
    public virtual event System.EventHandler<EntitySavingEventArgs<T>> SavingRecord;
    public virtual event System.EventHandler<EntitySavingEventArgs<T>> RecordSaved;
    public void PopulateEvents(Interface.IService<T> _implementation)
    {
        implementation = _implementation;
        implementation.BeforeSavingRecord += new System.EventHandler<EntitySavingEventArgs<T>>(this.OnBeforeSavingRecord);
        implementation.SavingRecord += new System.EventHandler<EntitySavingEventArgs<T>>(this.OnSavingRecord);
        implementation.RecordSaved += new System.EventHandler<EntitySavingEventArgs<T>>(this.OnRecordSaved);
    }
    public virtual void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<T> e)
    {
    }
    public virtual void OnSavingRecord(object sender, EntitySavingEventArgs<T> e)
    {
    }
    public virtual void OnRecordSaved(object sender, EntitySavingEventArgs<T> e)
    {
    }
    private readonly DbContext _dbContext;
    public Service(DbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public virtual void Create(T item)
    {
        if (item == null)
            throw new ArgumentNullException("item");
        BeforeSavingRecord?.Invoke(this, new EntitySavingEventArgs<T>() { SavedEntity = item });
        _dbContext.Set(typeof(T)).Add(item);
        SavingRecord?.Invoke(this, new EntitySavingEventArgs<T>() { SavedEntity = item });
        _dbContext.SaveChanges();
        RecordSaved?.Invoke(this, new EntitySavingEventArgs<T>() { SavedEntity = item });
    }
}

和派生类:

    [Service]
public partial class BankBusiness : Service<Bank>, IBankBusiness
{
    public BankBusiness()
        : base(ContainerManager.Container.Resolve<MyContext>())
    {
        base.PopulateEvents(this);
    }

    public override void OnBeforeSavingRecord(object sender, EntitySavingEventArgs<Bank> e)
    {
        base.OnBeforeSavingRecord(sender, e);
    }
}

重点是

base.PopulateEvents(this);