Home > Mobile >  invoke main project method in class library
invoke main project method in class library

Time:09-15

There is a class library project that I use in many asp.net web application solutions. In this project i have a class that i use for database operations. 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

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)

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

CodePudding user response:

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

  • Related