简体   繁体   English

在 class 库中调用主项目方法

[英]invoke main project method in class library

There is a class library project that I use in many asp.net web application solutions.我在许多 asp.net web 应用解决方案中使用了一个 class 库项目。 In this project i have a class that i use for database operations.在这个项目中,我有一个用于数据库操作的 class。 Very simply it looks like as follows非常简单,如下所示

(all codes are pseudo) (所有代码都是伪代码)

CoreDb.cs
{
  public class cCoreDb()
  {
    string TableName;
    Hashtable htFieldAndValue;
    Hashtable htConditionFieldAndValue;
    
    public int Insert()
    {
      ...
    }

    public int Update()
    {
      ...
    }
    
    public int Delete()
    {
      ...
    }
  }
}

I use this class in solution's main project like as follows我在解决方案的主项目中使用这个 class 如下

WebForm1.cs
{
  btnInsert_Click()
  {
    cCoreDb CoreDb = new cCoreDb();
    CoreDb.TableName = "Table1";
    CoreDb.htFieldAndValue.Add("Field1", "Value1");
    CoreDb.htFieldAndValue.Add("Field2", "Value2");
    CoreDb.Insert();
  }

  btnUpdate_Click()
  {
    cCoreDb CoreDb = new cCoreDb();
    CoreDb.TableName = "Table1";
    CoreDb.htFieldAndValue.Add("Field1", "Value1");
    CoreDb.htFieldAndValue.Add("Field2", "Value2");
    CoreDb.htConditionFieldAndValue.Add("ID", "3");
    CoreDb.Update();
  }

  btnDelete_Click()
  {
    cCoreDb CoreDb = new cCoreDb();
    CoreDb.TableName = "Table1";
    CoreDb.htConditionFieldAndValue.Add("ID", "3");
    CoreDb.Delete();
  }
}

WebForm2.cs
{
  btnInsert1_Click()
  {
    cCoreDb CoreDb = new cCoreDb();
    CoreDb.TableName = "Table1";
    CoreDb.htFieldAndValue.Add("Field1", "Value1");
    CoreDb.htFieldAndValue.Add("Field2", "Value2");
    CoreDb.Insert();
  }

  btnInsert2_Click()
  {
    cCoreDb CoreDb = new cCoreDb();
    CoreDb.TableName = "Table2";
    CoreDb.htFieldAndValue.Add("Field1", "Value1");
    CoreDb.htFieldAndValue.Add("Field2", "Value2");
    CoreDb.Insert();
  }
}

WebForm3.cs
...

In all solutions which i use this class I want to catch these class methods calls (like a db trigger) in a central place (like global.asax or master page OR listen messages like WndProc)在我使用此 class 的所有解决方案中,我想在中心位置(如 global.asax 或母版页或收听 WndProc 等消息)捕获这些 class 方法调用(如数据库触发器)

Global.asax.cs OR Site.Master.cs OR another place in project
{
  public class cCoreDbTrigger(cCoreDb CodeDb)
  {
    bool BeforeInsert()
    {
      if (CoreDb.TableName = "PRODUCT")
      {
        if (CoreDb.htFieldAndValue["CODE"] == already exists)
          throw exception "product code already exists";
      }      
      else if (CoreDb.TableName = "STOCK")
      {
        if (CoreDb.htFieldAndValue["NEW_STOCK"] < CriticalStock)
          throw exception "invalid stock value";
      }    
      else if (CoreDb.TableName = "XXX")
      {
        insert log_table;
      }    
      ...  
    }
  }

  bool AfterInsert()
  {
    ...
  }

  bool BeforeUpdate()
  {
    ...
  }

  bool AfterUpdate()
  {
    ...
  }

  bool BeforeDelete()
  {
    ...
  }

  bool AfterDelete()
  {
    ...
  }
}

How can I invoke before and after methods from coredb class for this like as follows我如何从 coredb class 调用之前和之后的方法,如下所示

CoreDb.cs
{
  public class cCoreDb()
  {
    public int Insert()
    {
      invoke cCoreDbTrigger.BeforeInsert(); //this message will be processed by the main project
      do something;
      invoke cCoreDbTrigger.AfterInsert(); //this message will be processed by the main project
    }
  }
}

notes: i know that there may be better solutions for these scenarios.笔记:我知道这些场景可能有更好的解决方案。 (like DB trigger, using entity objects, etc) But i am trying to find a solution for the structure of my own application. (如数据库触发器,使用实体对象等)但我试图为我自己的应用程序的结构找到一个解决方案。

You could for example use events :例如,您可以使用事件

public class cCoreDb()
  {
    public event EventHandler BeforeInsert;
    public event EventHandler AfterInsert;
    public int Insert(Hashtable htFieldAndValue)
    {
      BeforeInsert?.Invoke();
      do something;
      AfterInsert?.Invoke();
    }
  }
...

public class cCoreDbTrigger(cCoreDb codeDb){
  pubic cCoreDbTrigger(){
  {
     // attach event handlers
     coreDb.BeforeInsert += BeforeInsert;
     coreDb.EfterInsert += EfterInsert;
  }
  void BeforeInsert(object? sender, EventArgs e){
   ...
  }
  void EfterInsert(object? sender, EventArgs e){
   ...
  }
}

Events allow for some other class to be informed when the event is triggered.事件允许在触发事件时通知其他一些 class。 You can also add parameters to the event if you so want.如果需要,您还可以向事件添加参数。

Note that whenever you are using event handlers you should also keep in mind when they should be removed.请注意,每当您使用事件处理程序时,您还应该记住何时应该删除它们。 A common cause of memory leaks is if you always add but never remove event handlers. memory 泄漏的一个常见原因是您总是添加但从不删除事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM